繁体   English   中英

EF CORE 映射到同一个表的多个属性

[英]EF CORE multiple properties that map to the same table

我有这个数据模型,我无法让它工作。
一个User可以同时关联到一个Company多个。
但是一个Company有多个Users
下面的图片示例: 在此处输入图片说明 我试图像这样在模型构建器上设置关系:

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

但我在迁移中遇到错误

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

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

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

数据库上下文

 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 中的多对多关系,可以参考官方文档。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM