简体   繁体   中英

Entity Framework foreign key to multiple tables/entities

I need to implement Entity-Attribute-Value functionality on multiple data tables using Entity Framework. Let's say I have an attribute value EF class that looks like this:

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

    ...
}

Then I have multiple entities that have supplementary EAV values associated with them:

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

My problem is that both Entity1 and Entity2 have EntityAttributeValue objects associated with them. Code first migrations tries to create a foreign key from EntityAttributeValue back to Entity1 and another one back to Entity2 on ParentSurrogateKey. The surrogate key for any single given EntityAttributeValue is only associated with either one Entity1 or one Entity2 (or, expanding out, one EntityN...), not both/all.

I have a many to many relationship here, but one side not only maps to multiple rows, but multiple entities/tables over a shared GUID column.

How should I be approaching this? Should I just remove the EntityAttributeValue foreign keys back to Entity1 and Entity2 from the automatic migration (which would be a long term pain)? Should I be manually retrieving the list of EntityAttributeValues for a given EAV entity instead of relying on EF to do it for me?

Well, the answer turned out to be obvious and simple. I needed to define a many-to-many relationship with FluentAPI. In OnModelCreating, I just added:

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

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

I thought I had tried this, but I guess I hadn't. Because the many-to-many relationship creates an intermediate table for each entity and the foreign keys are on that intermediate table (and there is only a row in the intermediate table when a given EntityAttributeValue applies to a given Entity), no foreign key issues.

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