0

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?

2 Answers 2

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

Seems you forgot "public"

Sign up to request clarification or add additional context in comments.

2 Comments

If i change it to public i get the error: 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.
Can you show your dbContext and IAggregateRecord files? (I will try to reimplement it on my side(seems you work with old example))
0

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".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.