簡體   English   中英

實體框架忽略了NotMapped屬性

[英]Entity Framework is ignoring NotMapped attribute

嘗試保存更改時,我收到以下異常:System.Data.SqlClient.SqlException:無效的列名稱'ClientID'。 列名稱“ID”無效。

ClientID屬性具有[NotMapped]屬性,並且該類沒有ID屬性。 此外,數據庫表與正確的屬性匹配。 我知道當你有一個沒有關聯FK的導航屬性時,有時EntityFramework會創建隱式外鍵列,但這不是這里的情況。

public class AccountPreference :  fgleo.Objects.PortfolioManagement.IAccountPreference
{
    #region Constructors

    public AccountPreference() { }

    #endregion

    #region Fields & Properties

    public Guid Guid { set; get; }

    [ForeignKey("Account"), Key]
    public int AccountID { set; get; }
    public virtual tAccount Account
    {
        get;
        set;
    }

    public string Nickname { set; get; }


    public string AccountType { set; get; }


    public string InsuranceCompanyName { set; get; }


    public string PolicyName { set; get; }


    public ContainerType ContainerType { set; get; }


    public bool Hide { set; get; }


    public bool IsActive { set; get; }


    public bool IsReviewed { set; get; }


    public bool IsPreserved { set; get; }


    public bool IsImplemented { set; get; }


    public bool AllocateByAccount { set; get; }


    public bool IsActivelyManaged { set; get; }


    public int AccountRiskTolerance { set; get; }

    public decimal? BalanceAtAccountLock { set; get; }


    public bool AddCashHolding { set; get; }


    public bool RefreshCalcs { set; get; }


    public bool UsePortfolioOverride { set; get; }


    public bool UseBestOfClassOverride { set; get; }


    public bool UseSavedRecommendations { set; get; }


    public bool WaitingForTimer { set; get; }


    public AccountPendingStatus PendingStatus { set; get; }


    public DateTime? DatePendingChange { set; get; }

    [NotMapped]
    public int ClientID
    {
        get
        {
            return (int) ClientIDNullable;
        }
        set
        {
            this.ClientIDNullable = value;
        }
    } 

    public virtual Client Client { get; set; }
    [ForeignKey("Client")]
    public int? ClientIDNullable { get; set; }


    #endregion


}

最奇怪的是,這段代碼在本地運行得非常好,但在我們的生產環境中卻沒有。 我檢查了數據庫,看起來是相同的。

如果沒有更多信息或代碼示例,則不知道您是否使用了Fluent API。

對於Fluent API,您還可以使用OnModelCreating事件來忽略屬性,如下所示:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
   modelBuilder.Entity<YourModel>().Ignore(t => t.ClientID );
   base.OnModelCreating(modelBuilder);
}

是一些有用的信息。

實際錯誤來自存儲過程。 這非常令人困惑,因為實體框架沒有轉儲整個錯誤消息(包括程序的名稱),並使它看起來像EF只是生成不正確的SQL。 相反,存儲過程只是有一些過時的定義,導致問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM