简体   繁体   English

实体框架,M:M关系,无需引用完整性

[英]Entity Framework, M:M relationship WITHOUT referential Integrity

What I am trying to do is I have two tables which can be linked many to many, but the object on either side may not exist yet. 我想做的是有两个表,它们可以链接很多,但是两侧的对象可能还不存在。 Whilst this gives me chills just thinking about it, it is required for what I am working on. 尽管这让我不寒而栗,但我正在从事的工作却是必需的。

I've never had to do this before with the Entity Framework Code First, I have no problem creating the many to many relationship but only when both sides exist, so I'm not 100% certain that this is even possible but hopefully someone will be able to identify how I can achieve this. 我从来没有用Entity Framework Code做到过这一点。首先,我没有问题,但要建立多对多关系,只有当双方都存在时,我才100%不确定这样做是否可行,但希望有人能做到能够确定我如何实现这一目标。

So far as entities on both sides can be optional, you can do this using third entity: 只要双方的实体都是可选的,就可以使用第三个实体:

public class Entity1
{
    public int Id { get; set; }
}

public class Entity2
{
    public int Id { get; set; }
}

public class Many2ManyRelationEntity
{
    public int Id { get; set; }
    public int? Entity1Id { get; set; }
    public int? Entity2Id { get; set; }
}

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

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