简体   繁体   中英

Entity Framework is ignoring NotMapped attribute

I'm receiving the following exception when attempting to save changes: System.Data.SqlClient.SqlException: Invalid column name 'ClientID'. Invalid column name 'ID'.

The ClientID property has a [NotMapped] attribute, and the class does not have an ID property. In addition, the database tables match the correct properties. I know that sometimes EntityFramework will create implicit foreign key columns when you have a navigation property with no associated FK, but that is not the case here.

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


}

The weirdest thing is that this code works perfectly fine locally, but not in our production environment. I've inspected the DB and it appears to be identical.

Without more information or code examples, it is not known if you make use of the Fluent API.

In case of Fluent API, you can also make use of the OnModelCreating event, to ignore properties, as follow:

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

Here is some useful information.

The actual error was coming from a stored procedure. This was very confusing because Entity Framework did not dump the entire error message (which included the procedure's name), and made it look like EF was just generating incorrect SQL. Instead, the stored procedure just had some outdated definitions, leading to the issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM