C# 如何通过实体框架代码优先方法定义导航属性
我想在实体框架中将以下类用作我的数据上下文:C# 如何通过实体框架代码优先方法定义导航属性,c#,entity-framework,entity-framework-6,ef-code-first,ef-code-first-mapping,C#,Entity Framework,Entity Framework 6,Ef Code First,Ef Code First Mapping,我想在实体框架中将以下类用作我的数据上下文: public class AggregateRecord : IAggregateRecord { [Key] public int AggregateRecordID { get; set; } public DateTime? InsertDate { get; set; } = DateTime.Now; public DateTime BookingDate {
public class AggregateRecord : IAggregateRecord
{
[Key]
public int AggregateRecordID { get; set; }
public DateTime? InsertDate { get; set; } = DateTime.Now;
public DateTime BookingDate { get; set; }
public string AmountTypeName { get; set; }
public int? UnifiedInstrumentCode { get; set; }
public double? Amount { get; set; }
public string BookingAccountID { get; set; }
public string AccountCurrency { get; set; }
public string ClientCurrency { get; set; }
public string AffectsBalance { get; set; }
public string AssetType { get; set; }
public string UnderlyingInstrumentSubType { get; set; }
public string InstrumentSymbol { get; set; }
public string InstrumentSubType { get; set; }
public string UnderlyingInstrumentAssetType { get; set; }
public string UnderlyingInstrumentDescription { get; set; }
public string UnderlyingInstrumentSymbol { get; set; }
public string UnderlyingInstrumentUic { get; set; }
public double? AmountAccountCurrency { get; set; }
public string AmountClientCurrency { get; set; }
public string InstrumentDescription { get; set; }
public virtual ICollection<InstrumentInfo> InstrumentInfo { get; set; }
}
public class InstrumentInfo
{
[Key]
public int InstumentInfoID {get;set;}
public string SomeInformation { get; set; }
public int AggregateRecordID { get; set; }
public virtual AggregateRecord AggregateRecord { get; set; }
}
公共类聚合记录:IAggregateRecord
{
[关键]
公共整数聚合记录{get;set;}
public DateTime?InsertDate{get;set;}=DateTime.Now;
public DateTime BookingDate{get;set;}
公共字符串AmountTypeName{get;set;}
公共int?统一仪表代码{get;set;}
公共双倍?金额{get;set;}
公共字符串BookingAccountID{get;set;}
公共字符串AccountCurrency{get;set;}
公共字符串ClientCurrency{get;set;}
公共字符串影响平衡{get;set;}
公共字符串资产类型{get;set;}
子类型{get;set;}下的公共字符串
公共字符串指令符号{get;set;}
公共字符串指令子类型{get;set;}
公共字符串underyingInstrumentAssetType{get;set;}
公共字符串underyingInstrumentDescription{get;set;}
公共字符串underyingInstrumentSymbol{get;set;}
公共字符串underyingInstrumentUIC{get;set;}
public double?AmountAccountCurrency{get;set;}
公共字符串AmountClientCurrency{get;set;}
公共字符串InstrumentDescription{get;set;}
公共虚拟ICollection InstrumentInfo{get;set;}
}
公共类工具信息
{
[关键]
公共int instumentifid{get;set;}
公共字符串SomeInformation{get;set;}
公共整数聚合记录{get;set;}
公共虚拟聚合记录聚合记录{get;set;}
}
我已经研究了为EF6提供的示例,但我仍然存在一个问题,即当我尝试更新迁移时,会出现以下错误:
在模型生成过程中检测到一个或多个验证错误:
引用的表“dbo.AggregateRecords”中没有与外键“FK_dbo.InstrumentInfoes_dbo.AggregateRecords_AggregateRecords”中的引用列列表匹配的主键或候选键。
无法创建约束或索引。请参阅前面的错误。
如何定义类,以便可以通过导航属性访问InstrumentInfo
public class InstrumentInfo
{
[Key]
public int InstumentInfoID {get;set;}
public string SomeInformation { get; set; }
public int AggregateRecordId { get; set; }
public virtual AggregateRecord AggregateRecord { get; set; }
}
你好像忘了“公开”了,我“解决”了这个问题。这很奇怪,但也许对将来的人有帮助,这就是为什么我要回答我自己的问题
我将我的类AggregateRecord重命名为AggregateEntry。使用新重命名的类名执行了添加迁移和更新数据库。它成功了。
看起来迁移定义或其他方面存在一些问题,但它解决了这个问题。
最后,我把它重新命名为原来的名字,重复同样的步骤,瞧,它成功了
@丹尼斯·斯派德:谢谢你的努力,如果没有你的暗示,我会花更多的时间去发现真正的“问题”.如果将其更改为public,则会出现错误:引用的表“dbo.AggregateRecords”中没有与外键“FK_dbo.InstrumentInfoes_dbo.AggregateRecords_AggregateRecords”中的引用列列表匹配的主键或候选键。无法创建约束或索引。请参阅前面的错误。能否显示dbContext和IAggregateRecord文件?(我将尝试在我这边重新实现它(似乎您使用的是旧示例))