0
더 깨끗한 데이터 모델을 위해 데이터 주석 대신 유창한 API 구성을 사용하도록 EF 프로젝트를 다시 구성하려고합니다. 우리는 기존 테이블을 가지고 있으며, 이제는 내 노력을 검증하려고합니다. 여기 Fluent API 중복 열 생성
이전 (부분) EF 모델의 선언은 다음과 같습니다[Table("CustomerLocation")]
internal class CustomerLocationEF
{
[Column(Order = 0), Key]
public int CustomerID { get; set; }
[Column(Order = 1), Key]
[Required]
[MaxLength(ModelConstants.MaxLength128)]
[Index("IX_Customer_LocationReference", IsUnique = true)]
public string LocationReference { get; set; }
public virtual CustomerEF Customer { get; set; }
}
[Table("Customer")]
internal class CustomerEF
{
[Key]
public int CustomerID { get; set; }
public virtual ICollection<CustomerLocationEF> Locations { get; set; }
}
이 잘 작동하고 예상 스키마를 생산하고 있습니다.
public class CustomerLocationModel
{
public int CustomerId { get; set; }
public string LocationReference { get; set; }
public virtual CustomerModel Customer { get; set; }
}
public class CustomerModel
{
public int Id { get; set; }
public virtual ICollection<CustomerLocationModel> Locations { get; set; }
}
internal sealed class CustomerLocationTypeConfiguration : EntityTypeConfiguration<CustomerLocationModel>
{
public CustomerLocationTypeConfiguration()
{
ToTable("CustomerLocation");
HasKey(x => new {x.CustomerId, x.LocationReference});
Property(x => x.CustomerId)
.HasColumnName("CustomerID");
HasRequired(x => x.Customer).WithMany().WillCascadeOnDelete(false);
}
}
그러나, 문제는이 같은 테이블을 생성하려고 :
CreateTable(
"dbo.CustomerLocation",
c => new
{
CustomerID = c.Int(nullable: false),
LocationReference = c.String(nullable: false, maxLength: 128),
CustomerModel_Id = c.Int(),
})
.PrimaryKey(t => new { t.CustomerID, t.LocationReference })
.ForeignKey("dbo.Customer", t => t.CustomerID)
.ForeignKey("dbo.Customer", t => t.CustomerModel_Id)
.Index(t => new { t.CustomerID, t.LocationReference }, unique: true, name: "IX_Customer_LocationReference")
.Index(t => t.CustomerModel_Id);
공지 중복 열 CustomerModel_Id
및 관련 외래 키 그러나, 여기에 새로운 모델과 구성이다. 데이터 주석을 사용하기 전에 이와 같은 문제가 발생하여 [ForeignKey]
으로 해결되었지만 Fluent API를 처음 접했고 여기에서 잘못된 부분을 잘 모릅니다. Fluent에서 내 탐색 속성/외래 키를 올바르게 가져올 수 있도록이 문제를 어떻게 해결합니까?