Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 多个表/实体的实体框架外键_C#_Entity Framework_Foreign Key Relationship - Fatal编程技术网

C# 多个表/实体的实体框架外键

C# 多个表/实体的实体框架外键,c#,entity-framework,foreign-key-relationship,C#,Entity Framework,Foreign Key Relationship,我需要使用实体框架在多个数据表上实现实体属性值功能。假设我有一个属性值EF类,如下所示: public class EntityAttributeValue { // Not important to my question. public virtual Entity ParentEntity { get; set; } public virtual EntityAttribute ParentEntityAttribute { get; set; } //

我需要使用实体框架在多个数据表上实现实体属性值功能。假设我有一个属性值EF类,如下所示:

public class EntityAttributeValue
{
    // Not important to my question.
    public virtual Entity ParentEntity { get; set; }
    public virtual EntityAttribute ParentEntityAttribute { get; set; }

    // Field in question.
    public Guid ParentSurrogateKey { get; set; }

    public string Value { get; set; }

    ...
}
然后我有多个实体,它们具有与之关联的补充EAV值:

public class Entity1
{
    // Key.  EntityAttributeBalue.ParentSurrogateKey maps to this.
    [Key]
    public Guid SurrogateKey { get; set; }

    // Standard properties.
    public string Property1 { get; set; }
    public string Property2 { get; set; }

    // Collection of EAV values associated with this entity/table.
    [ForeignKey("ParentSurrogateKey")]
    public virtual IList<EntityAttributeValue> EntityAttributeValues { get; set; }
}

public class Entity2
{
    // Key.  EntityAttributeBalue.ParentSurrogateKey maps to this.
    [Key]
    public Guid SurrogateKey { get; set; }

    // Standard properties.
    public string OtherProperty1 { get; set; }
    public string OtherProperty2 { get; set; }

    // Collection of EAV values associated with this entity/table.
    [ForeignKey("ParentSurrogateKey")]
    public virtual IList<EntityAttributeValue> EntityAttributeValues { get; set; }
}
公共类实体1
{
//Key.EntityAttributeValue.ParentSurrogateKey映射到此。
[关键]
公共Guid代理密钥{get;set;}
//标准属性。
公共字符串属性1{get;set;}
公共字符串属性2{get;set;}
//与此实体/表关联的EAV值的集合。
[外侨(“父母代理”)]
公共虚拟IList EntityAttributeValue{get;set;}
}
公共类实体2
{
//Key.EntityAttributeValue.ParentSurrogateKey映射到此。
[关键]
公共Guid代理密钥{get;set;}
//标准属性。
公共字符串OtherProperty1{get;set;}
公共字符串OtherProperty2{get;set;}
//与此实体/表关联的EAV值的集合。
[外侨(“父母代理”)]
公共虚拟IList EntityAttributeValue{get;set;}
}
我的问题是Entity1和Entity2都有与之关联的EntityAttributeValue对象。代码优先迁移尝试在ParentProgregateKey上创建一个从EntityAttributeValue返回Entity1的外键,另一个返回Entity2的外键。任何单个给定EntityAttributeValue的代理项键仅与一个Entity1或一个Entity2(或扩展为一个EntityN…)关联,而不是两者/全部关联

我在这里有一个多对多关系,但一侧不仅映射到多行,还映射到共享GUID列上的多个实体/表


我应该如何处理这个问题?我是否应该从自动迁移中将EntityAttributeValue外键移回Entity1和Entity2(这将是一个长期的难题)?我是否应该手动检索给定EAV实体的EntityAttributeValue列表,而不是依赖EF来为我检索这些值?

好吧,答案显然很简单。我需要定义与FluentAPI的多对多关系。在OnModelCreating中,我刚刚添加了:

            modelBuilder.Entity<Entity1>()
                .HasMany(m => m.EntityAttributeValues)
                .WithMany();

            modelBuilder.Entity<Entity2>()
                .HasMany(m => m.EntityAttributeValues)
                .WithMany();
modelBuilder.Entity()
.HasMany(m=>m.EntityAttributeValue)
.有许多();
modelBuilder.Entity()
.HasMany(m=>m.EntityAttributeValue)
.有许多();

我以为我已经试过了,但我想我没有。因为多对多关系为每个实体创建了一个中间表,并且外键位于该中间表上(当给定的EntityAttributeValue应用于给定的实体时,中间表中只有一行),所以没有外键问题。

我认为这是一个基本的EF新手问题。如果实际设置为多对多,则自动创建的联接表将处理这种情况。我以为我试过了,但显然没有。如果这一切顺利进行,我将回答我自己的问题。我感到困惑的一个原因是给定的EntityAttributeValues对象总是只有一个EntityN对象。但是,由于不同的EntityAttributeValue实例可以映射到不同类型(类)的EntityN对象,因此表示多对多关系是避免问题的权宜之计。或许,这一战略有可能被视为“拙劣”。