简体   繁体   English

实体框架忽略了NotMapped属性

[英]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'. 尝试保存更改时,我收到以下异常:System.Data.SqlClient.SqlException:无效的列名称'ClientID'。 Invalid column name 'ID'. 列名称“ID”无效。

The ClientID property has a [NotMapped] attribute, and the class does not have an ID property. ClientID属性具有[NotMapped]属性,并且该类没有ID属性。 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. 我知道当你有一个没有关联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


}

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. 如果没有更多信息或代码示例,则不知道您是否使用了Fluent API。

In case of Fluent API, you can also make use of the OnModelCreating event, to ignore properties, as follow: 对于Fluent API,您还可以使用OnModelCreating事件来忽略属性,如下所示:

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. 这非常令人困惑,因为实体框架没有转储整个错误消息(包括程序的名称),并使它看起来像EF只是生成不正确的SQL。 Instead, the stored procedure just had some outdated definitions, leading to the issue. 相反,存储过程只是有一些过时的定义,导致问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 实体框架在发布模式下避免使用[NotMapped]属性 - Entity Framework avoiding [NotMapped] attribute in release mode 实体框架[NotMapped]属性元素更新时更新WPF视图 - Update WPF View when Entity Framework [NotMapped] attribute elements updated 对来自实体框架中父类的属性使用[NotMapped] - Use [NotMapped] on an attribute coming from a parent class in entity framework 使用NotMapped属性并在实体框架中的Json Post中 - Use NotMapped property and in Json Post in Entity Framework C# 实体框架 NotMapped 单位转换器 - C# Entity Framework NotMapped Unit Converter 尝试序列化[NotMapped] Entity Framework属性以供Breeze使用 - Trying to serialize [NotMapped] Entity Framework property for use by Breeze 实体框架如何填充需要联接的NotMapped属性? - Entity Framework How to fill NotMapped Property that need a join? 实体框架,自动将 SystemVersioning 列设置为未映射的临时表 - Entity Framework, Make SystemVersioning Columns as NotMapped for Temporal tables Automatically 实体框架忽略OrderByDescending - Entity Framework ignoring OrderByDescending 从Entity Framework 4.1 Code First中的NotMapped类派生实体类 - Derive Entity class from a NotMapped class in Entity Framework 4.1 Code First
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM