Alpha
Alpha

Reputation: 7868

Primary key as foreign key throws duplicate definition exception

I'm using Enterprise Framework 4.3.1 with its Fluent API to set up my entities mapping to an existing database.

I have a very special case with a associative table which has it's primary key to be, at the same time, two foreign keys of their parent tables.

The error I'm getting is:

Schema specified is not valid. Errors: 
(68,6) : error 0019: Each property name in a type must be unique. Property name 'ProductId' was already defined.
(69,6) : error 0019: Each property name in a type must be unique. Property name 'PropertyId' was already defined.

My tables would be something like this:

Products (ProductId, ...)
ProductProperties (ProductPropertyId, ...) // does not depend on Product!
DefaultPropertyValues (ProductId (FK1, PK), ProductPropertyId (FK2, PK), DefaultValue)

And this is my code that sets up that specific entity:

//table mapping
modelBuilder.Entity<DefaultPropertyValue>().ToTable("DefaultPropertyValues", "dbo");

//not null value
modelBuilder.Entity<DefaultPropertyValue>().Property(d => d.DefaultValue).IsRequired();
//primary key
modelBuilder.Entity<DefaultPropertyValue>().HasKey(d => new { d.ProductId, d.ProductPropertyId });

//foreign key 1 -- see helper method
SetupGenericOneToManyForeignKey<DefaultPropertyValue, Product>(modelBuilder, d => d.Product, "ProductId");
//foreing key 2 -- see helper method
SetupGenericOneToManyForeignKey<DefaultPropertyValue, ProductProperty>(modelBuilder, d => d.ProductProperty, "ProductPropertyId");

//helper method
private CascadableNavigationPropertyConfiguration SetupGenericOneToManyForeignKey<TDependent, TParent>(DbModelBuilder modelBuilder, Expression<Func<TDependent, TParent>> foreignKeyField, string dbForeignKeyField) where TDependent: class where TParent: class
{
    return modelBuilder.Entity<TDependent>().HasRequired<TParent>(foreignKeyField).WithMany().Map((m) => m.MapKey(dbForeignKeyField));
}

So, my question is... what am I doing wrong?

Upvotes: 0

Views: 502

Answers (1)

NSGaga
NSGaga

Reputation: 14312

...if I get it right, what you're trying to do, should be something like this..

public class Product
{
    public int ProductId { get; set; }
    public virtual ICollection<DefaultPropertyValue> DefaultPropertyValues { get; set; }
}
public class ProductProperty 
{
    public int ProductPropertyId { get; set; }
    public virtual ICollection<DefaultPropertyValue> DefaultPropertyValues { get; set; }
}
public class DefaultPropertyValue 
{
    public int ProductId { get; set; }
    public int ProductPropertyId { get; set; }
    public Product Product { get; set; }
    public ProductProperty ProductProperty { get; set; }
}
...
modelBuilder.Entity<DefaultPropertyValue>()
    .HasKey(i => new { i.ProductId, i.ProductPropertyId });

modelBuilder.Entity<DefaultPropertyValue>()
    .HasRequired(i => i.Product)
    .WithMany(u => u.DefaultPropertyValues)
    .HasForeignKey(i => i.ProductId)
    .WillCascadeOnDelete(false);

modelBuilder.Entity<DefaultPropertyValue>()
    .HasRequired(i => i.ProductProperty)
    .WithMany(u => u.DefaultPropertyValues)
    .HasForeignKey(i => i.ProductPropertyId)
    .WillCascadeOnDelete(false);

...the key is IMO in the HasForeignKey,
hope this helps

NOTE: WillCascadeOnDelete is optional of course and WithMany could be empty but I usually map all parts similarly.

Upvotes: 2

Related Questions