且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

自动的EntityFramework创造了许多一对多的关系

更新时间:2022-12-30 18:49:24


  

用户有一栏TEAM_ID


块引用>

下面是问题所在。 EF创建用户表中的列,因为你告诉EF这样做。你应该让许多到很多。

 公共类用户
{
   公众诠释用户ID {集;获取;}
   公众的ICollection<团队及GT;小组{设置;得到; }
}
公共类运动
{
    公众诠释标识{设置;得到; }
    公共字符串名称{设置;得到; }
    公共IEnumerable的<团队及GT;小组{设置;得到; }
}公共类团队
{
    公众诠释TeamId {搞定;组; }
    公开名单<使用者>用户{搞定;组; }
    公众诠释SportId {设置;得到; }
}

现在使用流利的配置,可以指示EF创造许多结合表对许多问题的研究。你可以在你的DbContext类的 OnModelCreating 办法做到这一点。

 保护覆盖无效OnModelCreating(DbModelBuilder模型构建器)
{
    modelBuilder.Entity<使用者>()
                .HasMany(D => d.Teams)
                .WithMany(F => f.Users)
                .MAP(W => w.ToTable(UserTeam)
                           .MapLeftKey(用户ID)
                           .MapRightKey(TeamId));}

这将建立一个多对一到用户和团队之间有很多关系,并创建具有用户名和TeamId列第四个表。

Does Entity Framework create Many-Many relationships automatically and if so how?

I am trying this below:

public DbSet<User> Users { get; set; }
public DbSet<Sport> Sports { get; set; }
public DbSet<Team> Teams { get; set; }

  • I have many Sports
  • I have many Teams in a Sport
  • I have many Users in a Team

Classes:

Sport

public int ID { get; set; }
public List<Team> Teams { get; set; }

Team

public int ID { get; set; }
public List<User> Members { get; set; }
public int SportID { get; set; }

User

 public int UserID { get; set; }

Issue:

So it creates the tables Users, Sports, Teams fine with their foreign key associations to each other. The problem is that these tables are not normalized and have redundant data. Also, when trying to query for members in the Teams table, I always get a null Members List!

For Example, Users has a column Team_ID so it creates a new User record when added to a team which is bad!

Shouldn't Entity Framework create a Many-Many relationship table automatically? So that it has columns |UserID|SportID|TeamID|

Users has a column Team_ID

Here is the problem. EF created a column in User table because you told EF to do so. You should make it many-to-many.

public class User
{
   public int UserId { set;get;}
   public ICollection<Team> Teams { set; get; }
}
public class Sport
{
    public int Id { set; get; }
    public string Name { set; get; }
    public IEnumerable<Team> Teams { set; get; }
}

public class Team
{
    public int TeamId { get; set; }
    public List<User> Users { get; set; }
    public int SportId { set; get; }
}

Now using fluent configuration, you can instruct EF to create a junction table for the many to many relation ship. You can do this in the OnModelCreating method of your dbcontext class.

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<User>()
                .HasMany(d => d.Teams)
                .WithMany(f => f.Users)
                .Map(w => w.ToTable("UserTeam")
                           .MapLeftKey("UserId")
                           .MapRightKey("TeamId"));

}

This will establish a many- to many relationship between User and Team and create a fourth table which has a UserId and TeamId column.