自动的EntityFramework创造了许多一对多的关系 [英] EntityFramework creating many-to-many relationships automatically

查看:225
本文介绍了自动的EntityFramework创造了许多一对多的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

确实实体框架自动创建多对多的关系,如果又如何?

我想这个如下:

 公共DbSet<使用者>用户{搞定;组; }
公共DbSet<体育GT;体育{搞定;组; }
公共DbSet<团队及GT;小组{搞定;组; }


  • 我有很多体育

  • 我有很多球队在运动

  • 我有很多用户在一个团队

类:

体育

 公众诠释ID {搞定;组; }
公开名单<团队及GT;小组{搞定;组; }

团队

 公众诠释ID {搞定;组; }
公开名单<使用者>成员{搞定;组; }
公众诠释SportID {搞定;组; }

用户

 公众诠释用户名{搞定;组; }

问题:

因此​​,它创建了自己的外键关联彼此表用户,体育,团队罚款。问题是,这些表并不是标准化并具有冗余数据。此外,试图查询在团队成员表的时候,我总是得到一个空会员列表!

例如,用户有一栏TEAM_ID所以当加入一个团队这是坏它创建一个新的用户记录!

应该不是实体框架自动创建一个多对多的关系表?因此,它具有列|用户名| SportID | TeamID |


解决方案

  

用户有一栏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.

这篇关于自动的EntityFramework创造了许多一对多的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆