实体框架6 - 类型已经被配置为实体类型,无法重新配置为复杂类型。

5

我有一个实体,做了一点改动,将外键设为非空和必填项。当我创建Entity Framework迁移时,遇到了以下错误:

类型“Payment”已被配置为实体类型,无法重新配置为复合类型。

我不知道为什么会这样。我没有将任何类定义为复合类型。我正在使用枚举,但它一直存在,之前从未出现过问题,并且在EF6中得到支持。

项目本身仍然可以正常编译。

这是导致问题的Payment类。

public partial class Payment
{
    public int payment_ID { get; set; }
    public int order_ID { get; set; }
    public PaymentTypeEnum paymentType_ID { get; set; }
    public string cc_response_ReceiptNo { get; set; }
    public string cc_response_QSIResponseCode { get; set; }
    public string cc_response_QSIResponseDescription { get; set; }
    public string cc_response_TransactionNo { get; set; }
    public string cc_response_BatchNo { get; set; }
    public DateTime? cc_response_expected { get; set; }
    public bool? cc_response_checked { get; set; }
    public DateTime? paymentReceived { get; set; }
    public int? paymentAmountUnits { get; set; }
    public string paymentNotes { get; set; }
    public string cc_GatewayIdent { get; set; }
    public virtual Order Order { get; set; }
    public virtual PaymentType PaymentType { get; set; }
    public virtual ICollection<CreditNote> CreditNotes { get; set; }
}

映射信息

 public class tbl_paymentMap : EntityTypeConfiguration<Payment>
{
    public tbl_paymentMap()
    {
        // Primary Key
        this.HasKey(t => t.payment_ID);

        // Properties
        this.Property(t => t.cc_response_ReceiptNo)
            .HasMaxLength(12);

        this.Property(t => t.cc_response_QSIResponseCode)
            .HasMaxLength(2);

        this.Property(t => t.cc_response_QSIResponseDescription)
            .HasMaxLength(150);

        this.Property(t => t.cc_response_TransactionNo)
            .HasMaxLength(21);

        this.Property(t => t.cc_response_BatchNo)
            .HasMaxLength(21);

        this.Property(t => t.paymentNotes)
            .HasMaxLength(100);

        this.Property(t => t.cc_GatewayIdent)
            .HasMaxLength(50);

        // Table & Column Mappings
        this.ToTable("tbl_payment");
        this.Property(t => t.payment_ID).HasColumnName("payment_ID");
        this.Property(t => t.order_ID).HasColumnName("order_ID");
        this.Property(t => t.paymentType_ID).HasColumnName("paymentType_ID");
        this.Property(t => t.cc_response_ReceiptNo).HasColumnName("cc_response_ReceiptNo");
        this.Property(t => t.cc_response_QSIResponseCode).HasColumnName("cc_response_QSIResponseCode");
        this.Property(t => t.cc_response_QSIResponseDescription).HasColumnName("cc_response_QSIResponseDescription");
        this.Property(t => t.cc_response_TransactionNo).HasColumnName("cc_response_TransactionNo");
        this.Property(t => t.cc_response_BatchNo).HasColumnName("cc_response_BatchNo");
        this.Property(t => t.cc_response_expected).HasColumnName("cc_response_expected");
        this.Property(t => t.cc_response_checked).HasColumnName("cc_response_checked");
        this.Property(t => t.paymentReceived).HasColumnName("paymentReceived");
        this.Property(t => t.paymentAmountUnits).HasColumnName("paymentAmountUnits");
        this.Property(t => t.paymentNotes).HasColumnName("paymentNotes");
        this.Property(t => t.cc_GatewayIdent).HasColumnName("cc_GatewayIdent");

        // Relationships
        this.HasRequired(t => t.Order)
            .WithMany(t => t.Payments)
            .HasForeignKey(d => d.order_ID);
        this.HasRequired(t => t.PaymentType)
            .WithMany(t => t.Payments)
            .HasForeignKey(d => d.paymentType_ID);
   }
}

你是否正在使用流畅的API进行配置? - Hamid Pourjam
我是。映射信息已添加。 - Chris Nevill
你的迁移脚本是什么样子的? - 3dd
没有迁移脚本。它不允许我创建一个。 - Chris Nevill
2个回答

1

好的,这个问题其实是我自己制造的一个奇怪的问题。在我的支付类旁边有一个信用票据类。您可以在我的原始问题中看到,一个支付可能有多个信用票据。

除此之外,支付和信用票据分别引用了一个订单。

在执行一些常规化操作时,我从信用票据类中删除了与订单类的链接。在修复随之而来的错误时,我使用了查找和替换,并不经意地在信用票据映射中执行了它。

因此

    public tbl_creditnoteMap()
    {
       this.Property(t => t.order_ID).HasColumnName("order_ID");

成为
    public tbl_creditnoteMap()
    {
       this.Property(t => t.Payment.order_ID).HasColumnName("order_ID");

实际上,它本应该被彻底删除。这显然会在幕后造成问题。

1
我知道这很老,但是Fluent API不需要包含[this]关键字,因此它可以简化。 - Hernan Demczuk

0

我认为在OnModelCreating中添加以下内容可以解决你的问题:

modelBuilder.Ignore<YourClassNameorEntityName>();  

值得一试,但并没有成功。出现了相同的错误。 - Chris Nevill

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接