简体   繁体   English

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

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

I have this data model that I cant make it work.我有这个数据模型,我无法让它工作。
One User can be associated to a Company AND multiple at the same time.一个User可以同时关联到一个Company多个。
However a Company has multiple Users但是一个Company有多个Users
Image example below:下面的图片示例: 在此处输入图片说明 I have tried to set relations on the modelBuilder like this:我试图像这样在模型构建器上设置关系:

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

But i get an error in the migration但我在迁移中遇到错误

Value cannot be null.值不能为空。 (Parameter 'key') (参数'键')

Even researched about InverseProperty but no luck there either.甚至研究过 InverseProperty 但也没有运气。
My question, is it possible to achieve this or should I try a different approach?我的问题是,是否有可能实现这一目标,还是应该尝试不同的方法?

From the relationshipn between User and Company , you could design the model as shown:UserCompany之间的关系,您可以设计如下所示的模型:

Model模型

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

Controller控制器

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

Result结果在此处输入图片说明

For many-to-many relationship in EF Core , you could refer to the official doc.对于 EF Core 中的多对多关系,可以参考官方文档。

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

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