【问题标题】:EF CORE multiple properties that map to the same tableEF CORE 映射到同一个表的多个属性
【发布时间】:2020-06-25 09:02:27
【问题描述】:

我有这个数据模型,但我无法让它工作。
一个User 可以同时关联到一个Company AND
然而一个Company 有多个Users
下图示例: 我试图像这样在模型构建器上设置关系:

 builder
        .HasMany(c => c.Users)
        .WithOne(u => u.CurrentAssociatedCompany)            
        .HasForeignKey(u => u.CurrentAssociatedCompanyId)
        .IsRequired();

但我在迁移时遇到错误

值不能为空。 (参数'key')

甚至研究过 InverseProperty,但也没有运气。
我的问题是,是否有可能实现这一目标,还是我应该尝试不同的方法?

【问题讨论】:

    标签: c# .net database asp.net-core entity-framework-core


    【解决方案1】:

    根据UserCompany 之间的关系,您可以设计模型如下:

    模型

    public class User
    {
        public int Id { get; set; }
        public string UserName { get; set; }
        public int Age { get; set; }
    
        public int CurrentAssociatedCompanyId { get; set; }
        public Company CurrentAssociatedCompany { get; set; }
    
        public ICollection<UserCompany> UserCompanies { get; set; }
    }
    public class Company
    {
        public int Id { get; set; }
        public string CompanyName { get; set; }
    
        public ICollection<UserCompany> UserCompanies { get; set; }
    }
    public class UserCompany
    {
        public int UserId { get; set; }
        public User User { get; set; }
    
        public int CompanyId { get; set; }
        public Company Company { get; set; }
    }
    

    DbContext

     public class MVCDbContext : DbContext
    {
        public MVCDbContext(DbContextOptions<MVCDbContext> options) : base(options)
        { }
    
        public DbSet<User> User { get; set; }
        public DbSet<Company> Company { get; set; }
    
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<UserCompany>()
                .HasKey(uc => new { uc.UserId,uc.CompanyId });
    
            modelBuilder.Entity<UserCompany>()
                .HasOne(uc=>uc.Company)
                .WithMany(c => c.UserCompanies)
                .HasForeignKey(uc=>uc.CompanyId)
                .OnDelete(DeleteBehavior.Restrict);
    
            modelBuilder.Entity<UserCompany>()
                 .HasOne(uc => uc.User)
                 .WithMany(u => u.UserCompanies)
                 .HasForeignKey(uc => uc.UserId)
                 .OnDelete(DeleteBehavior.Restrict);
        }
    }
    

    控制器

    public IActionResult GetUserData(int? id)
        {
            var userData = _context.User
                        .Include(u => u.CurrentAssociatedCompany)
                        .Include(u => u.UserCompanies)
                            .ThenInclude(uc => uc.Company)
                        .Where(u=>u.Id==id)
                        .Select(u => new
                        {
                            UserId = u.Id,
                            UserName = u.UserName,
                            Age = u.Age,
                            CurrentAssociatedCompany = u.CurrentAssociatedCompany.CompanyName,
                            AssociatedCompanies = u.UserCompanies.Select(uc => new {
                                Id=uc.Company.Id,
                                CompanyName=uc.Company.CompanyName
                            }).ToList()
                        });
    
            return Json(userData);
        }
    

    结果

    EF Core中的多对多关系可以参考the official doc.

    【讨论】:

    • 哇!谢谢你这么详细的回答,我很感激!我看到了你的逻辑,基本上只是在连接表中添加多对多关系。再次感谢您:)
    • 在这种情况下,您是如何将数据插入表中的,我在添加数据时遵循此操作并出现错误 PostgresException:23502:关系“AspNetUsers”的“CompanyId”列中的空值不违反-null 约束
    猜你喜欢
    • 2019-02-04
    • 1970-01-01
    • 2023-02-10
    • 1970-01-01
    • 1970-01-01
    • 2014-03-31
    • 2020-06-12
    • 2015-01-11
    • 1970-01-01
    相关资源
    最近更新 更多