简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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