C# 实体框架正在忽略NotMapped属性
尝试保存更改时,我收到以下异常:System.Data.SqlClient.SqlException:列名“ClientID”无效。 列名“ID”无效 ClientID属性具有[NotMapped]属性,而该类没有ID属性。此外,数据库表与正确的属性匹配。我知道有时候EntityFramework会在导航属性没有关联FK的情况下创建隐式外键列,但这里不是这样C# 实体框架正在忽略NotMapped属性,c#,entity-framework,C#,Entity Framework,尝试保存更改时,我收到以下异常:System.Data.SqlClient.SqlException:列名“ClientID”无效。 列名“ID”无效 ClientID属性具有[NotMapped]属性,而该类没有ID属性。此外,数据库表与正确的属性匹配。我知道有时候EntityFramework会在导航属性没有关联FK的情况下创建隐式外键列,但这里不是这样 public class AccountPreference : fgleo.Objects.PortfolioManagement.I
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);
}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
modelBuilder.Entity().Ignore(t=>t.ClientID);
基于模型创建(modelBuilder);
}
是一些有用的信息。实际错误来自存储过程。这非常令人困惑,因为EntityFramework没有转储整个错误消息(其中包括过程的名称),并使其看起来像EF只是生成了不正确的SQL。相反,存储过程只是有一些过时的定义,这导致了问题。能否发布生成此错误的实体的类定义?我有一个理论,但如果没有完整的定义,很难说它是否完全正确。我在课堂上编辑过。
Client
是否确实包含一个名为ClientIDNullable
的字段?如果您不告诉它,EF约定会为主键字段查找Id
或Id
。它可能试图根据您的导航属性将其关联起来,事情可能会变得…奇怪。这可能适用于ClientID属性,但甚至没有一个ID属性可以忽略(即如果我这样做,我将在尝试忽略不存在的属性时遇到编译时错误)。