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

.NET CORE 3.1 DATABASE FİRST How to access the name of Id received with foreign key

我正在做一个首先使用.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 (var item in Model) { @Html.DisplayFor(modelItem => item.UserName) @Html.DisplayFor(modelItem => item.UserSurname) @Html.DisplayFor(modelItem => item.Password) @Html.DisplayFor(modelItem => item.UserEmail) @Html.DisplayFor(modelItem => item.Rol.RoleName) }

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

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

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

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