温馨提示:本文翻译自stackoverflow.com,查看原文请点击:其他 - .NET CORE 3.1 DATABASE FİRST How to access the name of Id received with foreign key
asp.net-core asp.net-core-mvc foreign-keys asp.net-core-3.1 .net-core-3.1

其他 - .NET CORE 3.1 DATABASE FRST如何使用外键访问收到的ID名称

发布于 2020-04-03 23:59:39

我正在使用.NET core 3.1首先使用数据库进行项目开发。当我访问通过外键接收的ID的列信息时,它为null。

用户模型

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; }
    }
}

AdventureContext

 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 => item.UserName)@ Html.DisplayFor(modelItem => item.UserSurname)@ Html.DisplayFor(modelItem => item.Password)@ Html.DisplayFor( modelItem => item.UserEmail)@ Html.DisplayFor(modelItem => item.Rol.RoleName)}

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

查看更多

提问者
Harun
被浏览
43
hujtomi 2020-02-05 07:11

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

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

您可以在Microsoft Docs中了解有关如何加载相关实体的更多信息:https : //docs.microsoft.com/zh-cn/ef/core/querying/related-data

最新帖子

最新文章