Search code examples
c#entity-frameworkentity-framework-6ef-code-firstef-code-first-mapping

How to define a navigation property via Entity Framework code first approach


I have the following class that I want to use as my data context in 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; }
    }

I have studies the examples provided for EF6 but I still have the problem that when I try to update my migration that I get the following error:

One or more validation errors were detected during model generation:

There are no primary or candidate keys in the referenced table 'dbo.AggregateRecords' that match the referencing column list in the foreign key 'FK_dbo.InstrumentInfoes_dbo.AggregateRecords_AggregateRecordID'. Could not create constraint or index. See previous errors.

How do I have to define the classes so that InstrumentInfo can be accessed via a navigation property?


Solution

  • I "solved" the problem. It's weird, but maybe it helps somebody in future that's why I answer my own question.

    I renamed my class AggregateRecord to AggregateEntry. Performed the Add-Migration and Update-Database, with the new renamed class name. And it worked. It looks like there was some problem with the migration definition or whatsoever, but it solved it. In the end, I renamed it back to the original name, did the same procedure again and, voila, it works.

    @Dennis Spade: Thanks for your effort, without your hint it would have taken me even more time to find the real "problem".