0
Fluent API와 함께 Enterprise Framework 4.3.1을 사용하여 기존 데이터베이스에 매핑하는 엔티티를 설정하고 있습니다.외래 키로 기본 키가 중복 정의 예외를 던졌습니다
나는 부모 테이블의 두 외래 키가되는 기본 키를 가지고있는 연관 테이블의 매우 특별한 경우가 있습니다.
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));
}
내 질문은 ... 내가 뭘 잘못하고 있니? 내가 바로 당신이 뭘하려는 건지, 이런 식으로 뭔가해야 얻을 수 있다면 ..
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);
당신, 선생님, 맞습니다! Stil, 내가 이해하지 못하는 부분이 있습니다 : 어떻게'HasForeignKey'가'Map'과 아주 다른가요? 내 id 속성을 모델에서 꺼내 데이터베이스 열에 매핑하려면 원래 코드로 돌아가서 실패합니다. 실질적인 차이점은 무엇입니까? – Alpha
...'열이 존재하지 않습니다. - '열이 FK-s의 임시 열에 만 존재합니다'- 클래스의 속성에 해당 열을 매핑하는 것과는 대조적으로 - 명확히하기 위해. – NSGaga
hehe, 고마워, 나는 우리가 여전히 의심 할 것 같아. 하지만 다시 한번 고맙습니다. – Alpha