gpt4 book ai didi

c# - Entity Framework 忽略 NotMapped 属性

转载 作者:太空狗 更新时间:2023-10-30 00:39:38 25 4
gpt4 key购买 nike

我在尝试保存更改时收到以下异常: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);
}

Here是一些有用的信息。

关于c# - Entity Framework 忽略 NotMapped 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34164285/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com