Ste*_*zer 4 c# entity-framework
尝试保存更改时收到以下异常: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
}
Run Code Online (Sandbox Code Playgroud)
最奇怪的是,此代码在本地可以很好地工作,但不能在我们的生产环境中使用。我检查了数据库,它似乎是相同的。
小智 5
如果没有更多信息或代码示例,就无法知道是否使用Fluent API。
对于Fluent API,您还可以使用OnModelCreating事件来忽略属性,如下所示:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<YourModel>().Ignore(t => t.ClientID );
base.OnModelCreating(modelBuilder);
}
Run Code Online (Sandbox Code Playgroud)
这是一些有用的信息。
实际的错误来自存储过程。这非常令人困惑,因为实体框架没有转储整个错误消息(其中包括过程名称),并且使 EF 看起来只是生成了不正确的 SQL。相反,存储过程只是有一些过时的定义,导致了这个问题。