locked
I am trying to do a composite foriegn key RRS feed

  • Question

  • User-2097295820 posted

    I am trying to do a relationship between two tables. A State table and another table. The second table as two Foriegn key. I am getting errors when seeding my database.  I would past my code below for help.

    public class State{
    
        public int StateID { get;set;}
    
        public string Name { get; set; }
    }
    
    The table that relates to State table is 
    
    
    public class Interstatepricing
        {
            public int InterstatepricingID { get; set; }
    
            public double Cost { get; set; } //This is the cost on the first 0 - 5 kg
    
            [Column(Order = 1),ForeignKey("State")]
            public int FromStateID { get; set; }
    
            [ForeignKey("State")]
            [Column(Order = 2)]
            public int ToStateID { get; set; }
    
            //Adding the relationship below
    
            public virtual State State { get; set; }
    
    }

    When I seed my table I get the error below

    Interstatepricing_State_Target_Interstatepricing_State_Source: : The number of properties in the Dependent and Principal Roles in a relationship constraint must be identical.

    at System.Data.Entity.Core.Metadata.Edm.EdmModel.Validate()
    at System.Data.Entity.DbModelBuilder.Build(DbProviderManifest providerManifest, DbProviderInfo providerInfo)
    at System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection)
    at System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)
    at System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input)
    at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
    at System.Data.Entity.Internal.LazyInternalContext.get_ModelBeingInitialized()
    at System.Data.Entity.Infrastructure.EdmxWriter.WriteEdmx(DbContext context, XmlWriter writer)
    at System.Data.Entity.Utilities.DbContextExtensions.<>c__DisplayClass1.<GetModel>b__0(XmlWriter w)
    at System.Data.Entity.Utilities.DbContextExtensions.GetModel(Action`1 writeXml)
    at System.Data.Entity.Utilities.DbContextExtensions.GetModel(DbContext context)
    at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration, DbContext usersContext, DatabaseExistenceState existenceState, Boolean calledByCreateDatabase)
    at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration)
    at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.GetMigrator()
    at System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.Run()
    at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
    at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
    at System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner runner)
    at System.Data.Entity.Migrations.Design.ToolingFacade.Update(String targetMigration, Boolean force)
    at System.Data.Entity.Migrations.UpdateDatabaseCommand.<>c__DisplayClass2.<.ctor>b__0()
    at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command)
    One or more validation errors were detected during model generation:

    Interstatepricing_State_Target_Interstatepricing_State_Source: : The number of properties in the Dependent and Principal Roles in a relationship constraint must be identical.

    Friday, April 8, 2016 12:47 PM

Answers

All replies

  • User-359936451 posted

    Your error message explains it all.....

    Interstatepricing_State_Target_Interstatepricing_State_Source: : The number of properties in the Dependent and Principal Roles in a relationship constraint must be identical.

    you are not passing the correct number of properties.

    Friday, April 8, 2016 12:51 PM
  • User-2097295820 posted

    Could you help with a sample. I have been trying but I am not getting it. 

    Thanks alot friend

    Friday, April 8, 2016 1:31 PM
  • User36583972 posted

    Hi skliz4rel,

    ForeignKey attribute can be applied to properties of a class. Default Code-First convention for ForeignKey relationship the expects foreign key property name match with the primary key property.

    The following tutorial introduces how to implement the foreign key.

    DataAnnotations - ForeignKey Attribute:

    http://www.entityframeworktutorial.net/code-first/foreignkey-dataannotations-attribute-in-code-first.aspx

    Best Regards,

    Yohann Lu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 12, 2016 8:49 AM