类型{1}上不存在必需的属性{0}。添加FK时的实体框架(模型优先)

时间:2015-09-22 07:39:29

标签: c# .net entity-framework entity-framework-6

我正在尝试将外键添加到数据库中,然后我正在更新模型。更新模型后,应用程序会出现以下错误:

System.Data.Entity.Core.MetadataException was unhandled
  HResult=-2146232007
  Message=Schema specified is not valid. Errors: 
The relationship 'Accounting.Data.Repository.FK_np_DocumentStatuses_DocumentsTracking_StateId' was not loaded because the type 'Accounting.Data.Repository.DocumentsTracking' is not available.
The following information may be useful in resolving the previous error:
The required property 'DocumentsTrackingChildDocuments' does not exist on the type 'Accounting.Entity.DocumentsTracking'.


The relationship 'Accounting.Data.Repository.FK_np_DocumentsTracking_DocumentsTrackingChildDocuments_DocumentsTrackingId' was not loaded because the type 'Accounting.Data.Repository.DocumentsTracking' is not available.
The following information may be useful in resolving the previous error:
The required property 'DocumentsTrackingChildDocuments' does not exist on the type 'Accounting.Entity.DocumentsTracking'.


  Source=EntityFramework
  StackTrace:
       at System.Data.Entity.Core.Metadata.Edm.ObjectItemCollection.LoadAssemblyFromCache(Assembly assembly, Boolean loadReferencedAssemblies, EdmItemCollection edmItemCollection, Action`1 logLoadMessage)
       at System.Data.Entity.Core.Metadata.Edm.ObjectItemCollection.ExplicitLoadFromAssembly(Assembly assembly, EdmItemCollection edmItemCollection, Action`1 logLoadMessage)
       at System.Data.Entity.Core.Metadata.Edm.MetadataWorkspace.ExplicitLoadFromAssembly(Assembly assembly, ObjectItemCollection collection, Action`1 logLoadMessage)
       at System.Data.Entity.Core.Metadata.Edm.MetadataWorkspace.LoadFromAssembly(Assembly assembly, Action`1 logLoadMessage)
       at System.Data.Entity.Core.Metadata.Edm.MetadataWorkspace.LoadFromAssembly(Assembly assembly)
       at System.Data.Entity.Core.Metadata.Edm.MetadataOptimization.TryUpdateEntitySetMappingsForType(Type entityType)
       at System.Data.Entity.Internal.InternalContext.TryUpdateEntitySetMappingsForType(Type entityType)
       at System.Data.Entity.Internal.InternalContext.UpdateEntitySetMappingsForType(Type entityType)
       at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
       at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
       at System.Data.Entity.Internal.Linq.InternalSet`1.GetEnumerator()
       at System.Data.Entity.Infrastructure.DbQuery`1.System.Collections.Generic.IEnumerable<TResult>.GetEnumerator()
       at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
       at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
       at Accounting.Data.Logic.OrdersLogic.getOrdersTmpList() in f:\proj\Accounting.Data.Logic\OrdersLogic.cs:line 16
       at Accounting.Data.Logic.OrdersLogic.RefreshDocumentsFromTmpOrders() in f:\proj\Accounting.Data.Logic\OrdersLogic.cs:line 22
       at Accounting.UI.MainForm.btnRefreshDocumentsFromOrderTmp_Click(Object sender, EventArgs e) in f:\proj\Accounting.UI\MainForm.cs:line 57
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(Form mainForm)
       at Accounting.UI.Program.Main() in f:\proj\Accounting.UI\Program.cs:line 19
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException:

我的实体:

namespace Iwatch.Accounting.Entity
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.Xml.Serialization;


    public partial class DocumentStatus 
    {
        public DocumentStatus()
        {
            this.Documents = new HashSet<Document>();
            this.DocumentsTrackings = new HashSet<DocumentsTracking>();
            this.DocumentsTrackingChildDocuments = new HashSet<DocumentsTrackingChildDocument>();
        }
        [XmlElement("StateId")]
        [Key] 
        public int StateId { get; set; }
        [XmlElement("StateName")]
        public string StateName { get; set; }
        [XmlElement("GroupId")]
        public Nullable<int> GroupId { get; set; }

        public virtual HashSet<Document> Documents { get; set; }
        public virtual HashSet<DocumentsTracking> DocumentsTrackings { get; set; }
        public virtual HashSet<DocumentsTrackingChildDocument> DocumentsTrackingChildDocuments { get; set; }
    }
}

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated from a template.
//
//     Manual changes to this file may cause unexpected behavior in your application.
//     Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace Iwatch.Accounting.Entity
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.Globalization;
    using System.Xml.Serialization;

    public partial class DocumentsTracking
    {
        [XmlIgnore]
        CultureInfo culture = CultureInfo.CreateSpecificCulture("de-LI");

        [XmlIgnore]
        public int Id { get; set; }
        [Key]
        [XmlElement("Barcode")]
        public string Barcode { get; set; }
        [XmlElement("StateId")]
        public Nullable<int> StateId { get; set; }
        [XmlElement("StateName")]
        public string StateName { get; set; }
        [XmlElement("CheckWeight")]
        public Nullable<decimal> CheckWeight { get; set; }
        [XmlElement("DocumentCost")]
        public Nullable<decimal> DocumentCost { get; set; }

        [XmlIgnore]
        public DateTime? DateReceived { get; set; }

        [XmlElement("DateReceived")]
        public string DateReceivedString
        {
            get { return this.DateReceived != null ? this.DateReceived.Value.ToString("dd.MM.yyyy hh:mm:ss") : ""; }
            set { this.DateReceived = (value.Equals("") ? (DateTime?)null : System.DateTime.Parse(value, culture)); }
        }

        [XmlElement("RecipientFullName")]
        public string RecipientFullName { get; set; }
        [XmlElement("RecipientPost")]
        public string RecipientPost { get; set; }
        [XmlIgnore]
        public DateTime? ReceiptDateTime { get; set; }

        [XmlElement("ReceiptDateTime")]
        public string ReceiptDateTimeString
        {
            get { return this.ReceiptDateTime != null ? this.ReceiptDateTime.Value.ToString("yyyy-MM-dd hh:mm:ss") : ""; }
            set { this.ReceiptDateTime = (value.Equals("") ? (DateTime?)null : System.DateTime.Parse(value)); }
        }
        [XmlElement("OnlinePayment")]
        public Nullable<bool> OnlinePayment { get; set; }
        [XmlElement("DeliveryForm")]
        public Nullable<int> DeliveryForm { get; set; }
        [XmlElement("AddressUA")]
        public string AddressUA { get; set; }
        [XmlElement("AddressRU")]
        public string AddressRU { get; set; }
        [XmlElement("WareReceiverId")]
        public Nullable<int> WareReceiverId { get; set; }
        [XmlElement("BackDelivery")]
        public Nullable<int> BackDelivery { get; set; }
        [XmlElement("RedeliveryNUM")]
        public string RedeliveryNUM { get; set; }
        [XmlElement("CityReceiverSiteKey")]
        public Nullable<int> CityReceiverSiteKey { get; set; }
        [XmlElement("CityReceiverUA")]
        public string CityReceiverUA { get; set; }
        [XmlElement("CityReceiverRU")]
        public string CityReceiverRU { get; set; }
        [XmlElement("CitySenderSiteKey")]
        public Nullable<int> CitySenderSiteKey { get; set; }
        [XmlElement("CitySenderUA")]
        public string CitySenderUA { get; set; }
        [XmlElement("CitySenderRU")]
        public string CitySenderRU { get; set; }
        [XmlElement("DeliveryType")]
        public string DeliveryType { get; set; }
        [XmlElement("BackwardDeliveryNumber")]
        public System.Guid BackwardDeliveryNumber { get; set; }
        [XmlElement("RedeliveryCargoDescriptionMoney")]
        public string RedeliveryCargoDescriptionMoney { get; set; }
        [XmlElement("Failure")]
        public Nullable<bool> Failure { get; set; }
        [XmlElement("ReasonDescription")]
        public string ReasonDescription { get; set; }
        [XmlElement("GlobalMoneyExistDelivery")]
        public Nullable<bool> GlobalMoneyExistDelivery { get; set; }
        [XmlElement("GlobalMoneyLastTransactionStatus")]
        public string GlobalMoneyLastTransactionStatus { get; set; }
        [XmlIgnore]
        public DateTime? GlobalMoneyLastTransactionDate { get; set; }

        [XmlElement("GlobalMoneyLastTransactionDate")]
        public string GlobalMoneyLastTransactionDateString
        {
            get { return this.GlobalMoneyLastTransactionDate != null ? this.GlobalMoneyLastTransactionDate.Value.ToString("yyyy-MM-dd hh:mm:ss") : ""; }
            set { this.GlobalMoneyLastTransactionDate = (value.Equals("") ? (DateTime?)null : System.DateTime.Parse(value)); }
        }
        [XmlElement("Sum")]
        public Nullable<decimal> Sum { get; set; }
        [XmlElement("DocumentWeight")]
        public Nullable<decimal> DocumentWeight { get; set; }

        //TODO: the right data type is supposed to be used
        [XmlIgnore] //[XmlElement("SumBeforeCheckWeight")]
        public Nullable<decimal> SumBeforeCheckWeight { get; set; }
        [XmlElement("isEWPaid")]
        public Nullable<bool> isEWPaid { get; set; }
        [XmlElement("isEWPaidCashLess")]
        public Nullable<bool> isEWPaidCashLess { get; set; }
        [XmlElement("ewPaidSumm")]
        public Nullable<decimal> ewPaidSumm { get; set; }
        [XmlElement("RedeliverySum")]
        public Nullable<decimal> RedeliverySum { get; set; }
        [XmlElement("OwnerDocumentType")]
        public string OwnerDocumentType { get; set; }
        [XmlElement("errors")]
        public string errors { get; set; }
        [XmlElement("warnings")]
        public string warnings { get; set; }
        [XmlElement("info")]
        public string info { get; set; }

        public virtual DocumentStatus DocumentStatus { get; set; }
        public virtual DocumentsTrackingChildDocument DocumentsTrackingChildDocument { get; set; }
    }
}

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated from a template.
//
//     Manual changes to this file may cause unexpected behavior in your application.
//     Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace Iwatch.Accounting.Entity
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.Xml.Serialization;

    public partial class DocumentsTrackingChildDocument
    {

        public int Id { get; set; }
        [XmlElement("DocumentsTrackingId")]
        [Key]
        public Nullable<int> DocumentsTrackingId { get; set; }
        [XmlElement("Barcode")]
        public string Barcode { get; set; }
        [XmlElement("StateId")]
        public Nullable<int> StateId { get; set; }
        [XmlElement("StateName")]
        public string StateName { get; set; }
        [XmlElement("CheckWeight")]
        public Nullable<decimal> CheckWeight { get; set; }
        [XmlElement("DocumentCost")]
        public Nullable<decimal> DocumentCost { get; set; }
        [XmlElement("DateReceived")]
        public Nullable<System.DateTime> DateReceived { get; set; }
        [XmlElement("RecipientFullName")]
        public string RecipientFullName { get; set; }
        [XmlElement("RecipientPost")]
        public string RecipientPost { get; set; }
        [XmlElement("ReceiptDateTime")]
        public Nullable<System.DateTime> ReceiptDateTime { get; set; }
        [XmlElement("OnlinePayment")]
        public Nullable<bool> OnlinePayment { get; set; }
        [XmlElement("DeliveryForm")]
        public string DeliveryForm { get; set; }
        [XmlElement("AddressUA")]
        public string AddressUA { get; set; }
        [XmlElement("AddressRU")]
        public string AddressRU { get; set; }
        [XmlElement("WareReceiverId")]
        public Nullable<int> WareReceiverId { get; set; }
        [XmlElement("BackDelivery")]
        public string BackDelivery { get; set; }
        [XmlElement("RedeliveryNUM")]
        public string RedeliveryNUM { get; set; }
        [XmlElement("CityReceiverSiteKey")]
        public string CityReceiverSiteKey { get; set; }
        [XmlElement("CityReceiverUA")]
        public string CityReceiverUA { get; set; }
        [XmlElement("CityReceiverRU")]
        public string CityReceiverRU { get; set; }
        [XmlElement("CitySenderSiteKey")]
        public string CitySenderSiteKey { get; set; }
        [XmlElement("CitySenderUA")]
        public string CitySenderUA { get; set; }
        [XmlElement("CitySenderRU")]
        public string CitySenderRU { get; set; }
        [XmlElement("DeliveryType")]
        public string DeliveryType { get; set; }
        [XmlElement("BackwardDeliveryNumber")]
        public System.Guid BackwardDeliveryNumber { get; set; }
        [XmlElement("RedeliveryCargoDescriptionMoney")]
        public string RedeliveryCargoDescriptionMoney { get; set; }
        [XmlElement("Failure")]
        public Nullable<bool> Failure { get; set; }
        [XmlElement("ReasonDescription")]
        public string ReasonDescription { get; set; }
        [XmlElement("GlobalMoneyExistDelivery")]
        public Nullable<bool> GlobalMoneyExistDelivery { get; set; }
        [XmlElement("GlobalMoneyLastTransactionStatus")]
        public string GlobalMoneyLastTransactionStatus { get; set; }
        [XmlElement("GlobalMoneyLastTransactionDate")]
        public Nullable<System.DateTime> GlobalMoneyLastTransactionDate { get; set; }
        [XmlElement("Sum")]
        public Nullable<decimal> Sum { get; set; }
        [XmlElement("DocumentWeight")]
        public Nullable<decimal> DocumentWeight { get; set; }
        [XmlElement("SumBeforeCheckWeight")]
        public Nullable<decimal> SumBeforeCheckWeight { get; set; }
        [XmlElement("isEWPaid")]
        public Nullable<bool> isEWPaid { get; set; }
        [XmlElement("isEWPaidCashLess")]
        public Nullable<bool> isEWPaidCashLess { get; set; }
        [XmlElement("ewPaidSumm")]
        public Nullable<decimal> ewPaidSumm { get; set; }
        [XmlElement("RedeliverySum")]
        public Nullable<decimal> RedeliverySum { get; set; }
        [XmlElement("OwnerDocumentType")]
        public string OwnerDocumentType { get; set; }

        public virtual DocumentStatus DocumentStatus { get; set; }
        public virtual DocumentsTracking DocumentsTracking { get; set; }
    }
}

我想念什么?

0 个答案:

没有答案