简体   繁体   English

代码优先实体框架-多对多关系

[英]Code-first Entity Framework - many-to-many relationships

I have the following 我有以下

class Human
  int id
  string gender
  List<PhysicalAttribute> attributes

class PhysicalAttributes
  int id
  string description
  string type
  List<Human> humans

When I add the first human with attributes, the tables are created and the data is populated properly. 当我添加具有属性的第一个人员时,将创建表并正确填充数据。

The problem is when I add the next humans with similar attributes. 问题是当我添加具有相似属性的下一个人类时。 Let's say I have attributes 假设我有属性

type:"body"
description:"slim"

for both the first and second human. 对于第一个人和第二个人。 When I create new and add the second human, another entry with the same type and description is added to the PhysicalAttributes table. 创建新人员并添加第二个人时,具有相同类型和描述的另一个条目将添加到PhysicalAttributes表中。

Is there some way of doing this so that the existing entry will be used? 有某种方法可以使用现有条目吗?

Do I have to do a lookup on the PhysicalAttributes table first to see that the entry has been created? 我是否必须先在PhysicalAttributes表上进行查找才能看到条目已创建?

Is there some way of doing this so that the existing entry will be used? 有某种方法可以使用现有条目吗?

Yes. 是。 Make (type,description) the Entity Key for PhysicalAttributes. 使(类型,描述)为PhysicalAttributes的实体键。 You seem to have introduced a meaningless ID key property that allows multiple PhysicalAttributes to have the same type and description. 您似乎引入了一个无意义的ID密钥属性,该属性使多个PhysicalAttributes具有相同的类型和描述。

It is also possible to not fix the model, and fetch the existing PhysicalAttributes from the database to discover if any of the ones you need already exist. 可以不修复模型,而从数据库中获取现有的PhysicalAttributes,以发现所需的任何一个是否已经存在。 But that's just a tedious workaround for having the wrong entity key structure. 但这只是一个错误的实体密钥结构的乏味的解决方法。

If you are loading from JSON it's going to be inconventient to attach existing entities, and instead you can do the following. 如果您是从JSON加载,则附加现有实体会很不方便,相反,您可以执行以下操作。

Override SaveChanges and fetch all the PhyscialAttribute values from the database. 覆盖SaveChanges并从数据库中获取所有PhyscialAttribute值。 Then for the PhysicalAttribute entities that already exist in the database, Detach them from the DbContext, and EF won't attempt to insert them. 然后,对于数据库中已经存在的PhysicalAttribute实体,将它们从DbContext中分离出来,EF不会尝试插入它们。

After you call PhysicalAttributes.Load(), you will have two entries in the ChangeTracker for every PhysicalAttribute on a new Entity that already exists in the database. 调用PhysicalAttributes.Load()之后,对于数据库中已存在的新实体上的每个PhysicalAttribute,在ChangeTracker中将有两个条目。

EG 例如

    public override int SaveChanges()
    {

        PhysicalAttributes.Load();

        var entryGroup = from entry in ChangeTracker.Entries<PhysicalAttribute>()
                      group entry by new { entry.Entity.Description, entry.Entity.Type } into byKey
                      where byKey.Any( e => e.State == EntityState.Unchanged)
                      select byKey;

        foreach (var eg in entryGroup)
        {
            foreach (var e in eg )
            {
                if (e.State == EntityState.Added)
                {
                    e.State = EntityState.Detached;
                }
            }
        }

        return base.SaveChanges();
    }

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

相关问题 与代码优先实体框架的一对多关系 - One-to-many relationships with code-first Entity Framework 在Entity Framework 6中更新多对多关系的问题(代码优先) - Problems with updating many-to-many relationships in Entity Framework 6 (Code First) 覆盖多对多关系的代码优先命名约定 - Override code-first naming convention for many-to-many relationships 通过具有多对多关系的实体框架代码优先方法为 SQL Server 播种 - Seeding SQL Server by Entity Framework code-first approach with many-to-many relationship 实体框架核心代码优先:级联删除多对多关系 - Entity Framework Core Code-First: Cascade delete on a many-to-many relationship 实体框架,代码优先,如何从多对多关系映射中向表添加主键? - Entity Framework, code-first, how to add primary key to table out of many-to-many relationship mapping? 实体框架5代码优先多对多表重新命名与继承类(TPC) - Entity Framework 5 Code-First Many-to-Many Table Re-naming with inherited classes (TPC) 如何使用Entity Framework代码优先对“共享”进行建模-多对多是对的吗? - How to model “sharing” with Entity Framework code-first - is many-to-many right? 连接表中的实体框架核心代码优先多对多属性 - Entity Framework Core code-first many-to-many properties in join table 实体框架代码优先的多对多表外键 - Entity Framework code-first foreign key to many to many table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM