实体框架6 - 类型已被配置为实体类型。它不能重新配置为复杂类型 [英] Entity Framework 6 - The type has already been configured as an entity type. It cannot be reconfigured as a complex type

查看:195
本文介绍了实体框架6 - 类型已被配置为实体类型。它不能重新配置为复杂类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实体,我做了一个小的改动。我使外键不可空,需要。
当我来创建实体框架迁移时,我收到一个错误:


类型付款已经被配置为实体类型。
不能重新配置为复杂类型。


我不明白为什么会这样。我没有将任何类定义为复杂类型。
我正在使用一个枚举,但是一直都在这里,从来没有遇到过问题,并且在EF6下支持。



项目本身仍然编译完好。



这是导致问题的付款类。

 公共部分类付款
{
public int payment_ID {get;组; }
public int order_ID {get;组; }
public PaymentTypeEnum paymentType_ID {get;组; }
public string cc_response_ReceiptNo {get;组; }
public string cc_response_QSIResponseCode {get;组; }
public string cc_response_QSIResponseDescription {get;组; }
public string cc_response_TransactionNo {get;组; }
public string cc_response_BatchNo {get;组; }
public DateTime? cc_response_expected {get;组; }
public bool? cc_response_checked {get;组; }
public DateTime? paymentReceived {get;组; }
public int? paymentAmountUnits {get;组; }
public string paymentNotes {get;组; }
public string cc_GatewayIdent {get;组; }
public virtual Order Order {get;组; }
public virtual PaymentType PaymentType {get;组; }
public virtual ICollection< CreditNote> CreditNotes {get;组;
}

映射信息

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

//属性
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&列映射
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);

//关系
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);
}
}


解决方案

这原来是我自己做的一个奇怪的事情。
沿着我的付款类有一个CreditNote类。
您可以在我的原始问题中看到一个付款可能有许多信用票据。



除此之外,付款和信用票据单独引用了一个订单。



在执行一些规范化过程中,我从信用票据类中删除了订单类的链接。虽然修复了随后发生的错误,但我发现和替换了一些诡计,并在信用记录映射中无意中执行。



因此

  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);

实际上应该完全删除。
这显然破坏了幕后的事情。


I have an entity which I've made a minor change to. I've made the foreign key non nullable and required. When I've come to create the Entity Framework migration I'm receiving an error:

The type 'Payment' has already been configured as an entity type. It cannot be reconfigured as a complex type.

I can't see why this is the case. I've not defined any class as a complex type. I'm using an Enum, but that has always been there and never been a problem before and supported under EF6.

The project itself still compiles fine.

Here is the Payment class which is causing the problem.

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; }
}

Mapping information

 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);
   }
}

解决方案

OK this turned out to be a strange one of my own doing. Along side my payments class there is a CreditNote class. You can see in my original question that one Payment may have many Credit Notes.

On top of this both the Payment and the Credit notes individually referenced an order.

Whilst performing some normalisation I removed the link to the order class from the credit note class. Whilst fixing the bugs that ensued I went a bit beserk with find and replace and inadvertently performed it within the credit note mapping.

thus

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

became

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

When in reality it should have been deleted altogether. This obviously broke things break behind the scenes.

这篇关于实体框架6 - 类型已被配置为实体类型。它不能重新配置为复杂类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆