Warm tip: This article is reproduced from stackoverflow.com, please click
asp.net-core asp.net-core-mvc foreign-keys asp.net-core-3.1 .net-core-3.1

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

发布于 2020-04-03 23:37:19

I'm working on a project using database first with .NET core 3.1 .When I access the column information of the ID received with the Foreign key, it is null.

User Model

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

Role Model

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

View

@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) }

I want to get rolename. Why not coming ? [UserIndex List View][1] [1]: https://i.stack.imgur.com/2f2CZ.png

Questioner
Harun
Viewed
41
hujtomi 2020-02-05 07:11

You should load the related entities in your controller action method. Something like this:

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

You can read more about how to load related entities in the Microsoft Docs: https://docs.microsoft.com/en-us/ef/core/querying/related-data