如何通过实体框架代码优先方法定义导航属性

问题描述 投票:0回答:1

我有以下要在Entity Framework中用作数据上下文的类:

    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; }
    }

我已经研究了为EF6提供的示例,但是仍然有问题,当我尝试更新迁移时,出现以下错误:

[在模型生成过程中检测到一个或多个验证错误:

在引用表'dbo.AggregateRecords'中没有与外键'FK_dbo.InstrumentInfoes_dbo.AggregateRecords_AggregateRecordID'中的引用列列表匹配的主键或候选键。无法创建约束或索引。请参阅先前的错误。

我必须如何定义类,以便可以通过导航属性访问InstrumentInfo?

c# entity-framework entity-framework-6 ef-code-first ef-code-first-mapping
1个回答
0
投票
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; }
}

似乎您忘记了“公共”

© www.soinside.com 2019 - 2024. All rights reserved.