简体   繁体   English

实体框架中多对多关系中的多个实体

[英]Mutiple entities in a many to many relationship in Entity Framework

Lets say i have 3 classes: 可以说我有3个班级:

public class Tag {
    public string Name { get; set; }
}

public class Post {
     public virtual IList<Tag> Tags { get; set; }
}

public class User {
     public virtual IList<Tag> Tags { get; set; }
}

Now, there's a many to many relations between both users and tags, and also posts and tags. 现在,用户和标签以及帖子和标签之间存在多对多的关系。

Simplified: I have a list of tags, i want to be able to tag both users and posts, while the tags remain independent. 简化:我有一个标签列表,我希望能够标记用户和帖子,而标签保持独立。

Now, i could just create a map and map a double many to many, creating two tables: Users_Tags and Posts_Tags, but I need them to be in the same table. 现在,我可以创建一个地图并将多个映射到多个,创建两个表:Users_Tags和Posts_Tags,但我需要它们在同一个表中。

I need a: Entities_Tags: Entity_Id, Tag_Id, Descriminator 我需要一个:Entities_Tags:Entity_Id,Tag_Id,Descriminator

Usually i would make a base class, that both Tag and Post inherit from... "TagContainer". 通常我会创建一个基类,Tag和Post都继承自......“TagContainer”。 Creating a: TagContainers_Tags: TagContainer_Id, Tag_Id 创建一个:TagContainers_Tags:TagContainer_Id,Tag_Id

The problem now is that in my situation, in the entities i have in mind it doesn't make sense that they share the same base class. 现在的问题是,在我的情况下,在我想到的实体中,它们共享相同的基类是没有意义的。 This is because i have another base class doing the same thing that will conflict. 这是因为我有另一个基类做同样的事情会发生冲突。 And having that class inherit one of these base classes wouldn't make sense application wise. 并且让该类继承其中一个基类对应用程序没有意义。

Had EF had support for interfaces i would of course solved this by having my classes inheriting the necessary interfaces to support both the first and the second many-to-many entity. 如果EF支持接口,我当然会通过让我的类继承必要的接口来支持第一个和第二个多对多实体来解决这个问题。

I'm pretty much looking for a way to have EF support Discriminator based many-to-many tables without having to supply base classes to my entities. 我正在寻找一种方法让EF支持基于多对多表的Discriminator,而不必为我的实体提供基类。

Any suggestions? 有什么建议么?

So, you cannot use the table in two different many-to-many relationships. 因此,您不能在两个不同的多对多关系中使用该表。 if you really don't want to make any kind of generalization in both model and database tables(although it's most common in normalizing big database design) you can act like this: 如果你真的不想在模型和数据库表中进行任何类型的泛化(尽管它在规范化大数据库设计中最常见),你可以这样做:

public class MyTrust
    {
        public MyTrust()
        {
            MyLocations = new List<MyLocation>();
        }
        public int MyTrustID { get; set; }
        public string MyTrustName { get; set; }
        public virtual ICollection<MyLocation> MyLocations { get; set; }
    }
    public class MyLocation
    {

        public int MyLocationID { get; set; }
        public string MyLocationName { get; set; }
        public virtual MyTrust MyTrust { get; set; }
}

public class TaskDbContext : DbContext
    {
        public DbSet<MyTrust> MyTrusts { get; set; }
        public DbSet<MyLocation> MyLocations { get; set; }
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<MyTrust>().HasMany(t => t.MyLocations).WithRequired();
        }
    }

see the source of the sample here 这里查看样本的来源

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

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