Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/314.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

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_Entity Framework 4_Fluent - Fatal编程技术网

C# 主键作为外键引发重复定义异常

C# 主键作为外键引发重复定义异常,c#,entity-framework,entity-framework-4,fluent,C#,Entity Framework,Entity Framework 4,Fluent,我正在使用EnterpriseFramework4.3.1及其FluentAPI设置实体到现有数据库的映射 我有一个非常特殊的关联表,它的主键同时是其父表的两个外键 我得到的错误是: 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

我正在使用EnterpriseFramework4.3.1及其FluentAPI设置实体到现有数据库的映射

我有一个非常特殊的关联表,它的主键同时是其父表的两个外键

我得到的错误是:

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.
我的桌子是这样的:

Products (ProductId, ...)
ProductProperties (ProductPropertyId, ...) // does not depend on Product!
DefaultPropertyValues (ProductId (FK1, PK), ProductPropertyId (FK2, PK), DefaultValue)
这是我设置特定实体的代码:

//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));
}
//表映射
modelBuilder.Entity().ToTable(“DefaultPropertyValues”、“dbo”);
//非空值
modelBuilder.Entity().Property(d=>d.DefaultValue).IsRequired();
//主键
modelBuilder.Entity().HasKey(d=>new{d.ProductId,d.ProductPropertyId});
//外键1——请参见帮助器方法
SetupGenericOneToManyForeignKey(modelBuilder,d=>d.Product,“ProductId”);
//外置键2——请参见帮助器方法
SetupGenericOneToManyForeignKey(modelBuilder,d=>d.ProductProperty,“ProductPropertyId”);
//辅助方法
私有CascadableNavigationProperty配置SetupGenericOneToManyForeignKey(DbModelBuilder modelBuilder,Expression foreignKeyField,string dbForeignKeyField),其中TDependent:class其中TParent:class
{
返回modelBuilder.Entity();
}

所以,我的问题是。。。我做错了什么?

如果我做对了,你想做的应该是这样的

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);
公共类产品
{
public int ProductId{get;set;}
公共虚拟ICollection DefaultPropertyValue{get;set;}
}
公共类ProductProperty
{
public int ProductPropertyId{get;set;}
公共虚拟ICollection DefaultPropertyValue{get;set;}
}
公共类DefaultPropertyValue
{
public int ProductId{get;set;}
public int ProductPropertyId{get;set;}
公共产品产品{get;set;}
公共产品属性ProductProperty{get;set;}
}
...
modelBuilder.Entity()
.HasKey(i=>new{i.ProductId,i.ProductPropertyId});
modelBuilder.Entity()
.has必需(i=>i.Product)
.具有多个(u=>u.DefaultPropertyValue)
.HasForeignKey(i=>i.ProductId)
.WillCascadeOnDelete(假);
modelBuilder.Entity()
.HasRequired(i=>i.ProductProperty)
.具有多个(u=>u.DefaultPropertyValue)
.HasForeignKey(i=>i.ProductPropertyId)
.WillCascadeOnDelete(假);
…钥匙在HasForeignKey中,
希望这有帮助


注意:
WillCascadeOnDelete
当然是可选的,
WithMany
可以是空的,但我通常会以类似的方式映射所有部分。

先生,您做对了!还有一件事我不太明白:
HasForeignKey
Map
有什么不同?我的意思是,如果我想从模型中取出我的
id
s属性,并将其映射到数据库列,我将返回到我的原始代码,它将失败。本质上的区别是什么?…
列不“存在”
-就像“它们只存在于数据库中,作为FK-s的临时列”-而不是将这些列映射到类中的属性-只是为了澄清。呵呵,谢谢,我想我们仍然有疑问。尽管如此,再次感谢你的解决方案。学会接受它:)-EF现在接近完美状态,更糟糕了-顺便说一句,你也可以投票