[英]Creating a one to many with an association table in Entity Framework
I have an association table called SportTeams
:我有一个名为
SportTeams
的关联表:
public class SportTeam
{
int SportId;
int TeamId;
Sport Sport;
Team Team
}
public class Sport
{
ICollection<SportTeam> SportTeams;
}
public class Team
{
ICollection<SportTeam> SportTeams;
}
I mark this as an association table via fluent api我通过 fluent api 将此标记为关联表
modelBuilder.Entity<SportTeam>().HasKey(q => new { q.SportId,q.TeamId }); // set the primary key of the table
modelBuilder.Entity<SportTeam>().HasRequired(s => s.Team).WithMany(t => t.SportTeams).HasForeignKey(s => s.TeamId);
modelBuilder.Entity<SportTeam>().HasRequired(s => s.Sport).WithMany(s => s.SportTeams).HasForeignKey(s => s.SportId);
Now I need to create a one to many with the association table SportTeams
.现在我需要使用关联表
SportTeams
创建SportTeams
。 Let's call that table Matches
.我们称该表为
Matches
。
public class Matches
{
int Id;
int SportTeamId;
SportTeam SportTeam;
}
public class SportTeam
{
int SportId ;
int TeamId;
Sport Sport;
Team Team;
ICollection<Match> Matches;
}
I go back to the fluent api to make the changes for this one to many.我回到 fluent api 对这个一对多进行更改。
I say我说
modelBuilder.Entity<SportTeam>().HasMany(st => st.Matches).WithRequired(matches => matches.SportTeam).HasForeignKey(m => m.SportTeamId).WillCascadeOnDelete(false);
I get an error:我收到一个错误:
The number of properties in the Dependent and Principal Roles in a relationship constraint must be identical.
关系约束中的从属角色和主要角色中的属性数量必须相同。
I believe this error states that my sportTeam PK is a composite key and in my HasForeignKey section I just specify one FK to connect with.我相信这个错误表明我的运动队 PK 是一个复合键,在我的 HasForeignKey 部分我只指定了一个 FK 来连接。
How should I go about this situation?我该如何处理这种情况?
You're configuring the relashionship between SportTeam
and Matches
incorrectly.您错误地配置了
SportTeam
和Matches
之间的SportTeam
。 So you say that SportTeam
entity can have many Matches
then it logic that Matches
entity to have a foreign key that reference the SportTeam
entity.所以你说
SportTeam
实体可以有很多Matches
然后它逻辑Matches
实体有一个引用SportTeam
实体的外键。
But if your look at your SportTeam
entity configuration, you say it has a composite keys as primary key ( SportId
, TeamId
).但是,如果您查看您的
SportTeam
实体配置,您会说它有一个复合键作为主键( SportId
、 TeamId
)。
You get this error:您收到此错误:
The number of properties in the Dependent and Principal Roles in a relationship constraint must be identical.
关系约束中的从属角色和主要角色中的属性数量必须相同。
Because if you have composite key as primary key then the foreign key that refers to the primary key of SportTeam
should also have both the properties implied in the composite key.因为如果您将复合键作为主键,那么引用
SportTeam
主键的外键也应该具有复合键中隐含的两个属性。
So to solve this, your Matches
entity should look like this:因此,要解决此问题,您的
Matches
实体应如下所示:
public class Matches
{
public int Id { get; set; }
// these two properties below represent the foreign key that refers to SportTeam entity.
public int SportId { get; set; }
public int TeamId { get; set; }
public SportTeam SportTeam { get; set; };
}
In the OnModelCreating
method you should have this line:在
OnModelCreating
方法中,您应该有这一行:
modelBuilder.Entity<SportTeam>()
.HasMany(st => st.Matches)
.WithRequired(matches => matches.SportTeam)
.HasForeignKey(m => new { m.SportId, m.TeamId }) // <-- the composite foreign keys.
.WillCascadeOnDelete(false);
Instead of:代替:
modelBuilder.Entity<SportTeam>()
.HasMany(st => st.Matches)
.WithRequired(matches => matches.SportTeam)
.HasForeignKey(m => m.SportTeamId)
.WillCascadeOnDelete(false);
I always avoid using composite foreign key.我总是避免使用复合外键。 If I end up with things like you have in your sample I just put a primary key property
Id
in SportTeam
entity and make the couple of properties SportId
and TeamId
with unique constraint in database.如果我在您的示例中得到了类似的结果,我只是在
SportTeam
实体中放置了一个主键属性Id
,并在数据库中使用唯一的约束创建了两个属性SportId
和TeamId
。
I don't know if you code like this in your real project but please use properties and make them public if necessary.我不知道您是否在实际项目中这样编码,但请使用属性并在必要时将它们公开。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.