.NET CORE 3.1 DATABASE FİRST 如何访问使用外键接收的 Id 的名称



我正在处理一个使用数据库的项目,首先使用.NET core 3.1。当我访问用外键接收的ID的列信息时,它是空的。

用户模型

public partial class User
{
public User()
{
Article = new HashSet<Article>();
}
[Key]
public int UserId { get; set; }
[StringLength(50)]
public string UserName { get; set; }
[StringLength(50)]
public string UserSurname { get; set; }
[StringLength(50)]
public string Password { get; set; }
[StringLength(50)]
public string UserEmail { get; set; }
public int? RolId { get; set; }
[ForeignKey(nameof(RolId))]
[InverseProperty(nameof(Role.User))]
public virtual Role Rol { get; set; }
[InverseProperty("User")]
public virtual ICollection<Article> Article { get; set; }
}
}

榜样

public partial class Role
{
public Role()
{
User = new HashSet<User>();
}
[Key]
public int RoleId { get; set; }
[StringLength(50)]
public string RoleName { get; set; }
[InverseProperty("Rol")]
public virtual ICollection<User> User { get; set; }
}
}

冒险背景

public partial class AdventureContext : DbContext
{
public AdventureContext()
{
}
public AdventureContext(DbContextOptions<AdventureContext> options)
: base(options)
{
}
public virtual DbSet<Article> Article { get; set; }
public virtual DbSet<Category> Category { get; set; }
public virtual DbSet<Comment> Comment { get; set; }
public virtual DbSet<Images> Images { get; set; }
public virtual DbSet<Role> Role { get; set; }
public virtual DbSet<User> User { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlServer("Server=DESKTOP-Q779BF0\SQLEXPRESS;Database=cmsData;Trusted_Connection=True;");
}
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Article>(entity =>
{
entity.HasOne(d => d.Category)
.WithMany(p => p.Article)
.HasForeignKey(d => d.CategoryId)
.HasConstraintName("FK_Article_Category");
entity.HasOne(d => d.Image)
.WithMany(p => p.Article)
.HasForeignKey(d => d.ImageId)
.HasConstraintName("FK_Article_Images");
entity.HasOne(d => d.User)
.WithMany(p => p.Article)
.HasForeignKey(d => d.UserId)
.HasConstraintName("FK_Article_User");
});
modelBuilder.Entity<Comment>(entity =>
{
entity.HasOne(d => d.Article)
.WithMany(p => p.Comment)
.HasForeignKey(d => d.ArticleId)
.HasConstraintName("FK_Comment_Article");
});
modelBuilder.Entity<User>(entity =>
{
entity.HasOne(d => d.Rol)
.WithMany(p => p.User)
.HasForeignKey(d => d.RolId)
.HasConstraintName("FK_User_Role");
});
OnModelCreatingPartial(modelBuilder);
}
partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
}
}

视图

@foreach(模型中的变量项( { @Html.DisplayFor(modelItem => 项。用户名( @Html.DisplayFor(modelItem => 项。用户姓氏( @Html.DisplayFor(modelItem => 项。密码( @Html.DisplayFor(modelItem => 项。用户电子邮件( @Html.DisplayFor(modelItem => 项。Rol.RoleName( }

我想获取角色名称。为什么不来? [用户索引列表视图][1] [1]: https://i.stack.imgur.com/2f2CZ.png

应在控制器操作方法中加载相关实体。像这样:

var list = db.User.Include(u => u.Rol).ToList();

您可以在Microsoft文档中阅读有关如何加载相关实体的更多信息:https://learn.microsoft.com/en-us/ef/core/querying/related-data

最新更新