c# - A Dependent Role has multiple principals with different values -


i using enitity framework codefirst , have following design

public class location {     public int id { get; set; }     public string name { get; set; }     public bool isdeleted { get; set; } }  public class delieryrate  {     public int id { get; set; }      public int originid { get; set; }     public location origin { get; set; }      public int destinationid { get; set; }     public location destination { get; set; }      public double amount { get; set; }      public bool isactive { get; set; } } 

i not want define relationship on location. when update database, works referential integrity error when run seed code. have tried configure using fluent api follows

modelbuilder.entity<deliveryrate>()             .hasrequired(e => e.origin)             .withoptional()             .willcascadeondelete(false);  modelbuilder.entity<deliveryrate>()             .hasrequired(e => e.destination)             .withoptional()             .willcascadeondelete(false); 

when attempting update database following error

system.data.entity.infrastructure.dbupdateexception: error occurred while updating entries. see inner exception details. ---> system.data.updateexception: error occurred while updating entries. see inner exception details. ---> system.data.constraintexception: referential integrity constraint violation. dependent role has multiple principals different values. --- end of inner exception stack trace --- @ system.data.mapping.update.internal.tablechangeprocessor.diagnosekeycollision(updatecompiler compiler, propagatorresult change, compositekey key, propagatorresult other) @ system.data.mapping.update.internal.tablechangeprocessor.processkeys(updatecompiler compiler, list1 changes, set1 keys) @ system.data.mapping.update.internal.tablechangeprocessor.compilecommands(changenode changenode, updatecompiler compiler) @ system.data.mapping.update.internal.updatetranslator.d__0.movenext() @ system.linq.enumerable.d__711.movenext() @ system.data.mapping.update.internal.updatecommandorderer..ctor(ienumerable1 commands, updatetranslator translator) @ system.data.mapping.update.internal.updatetranslator.producecommands() @ system.data.mapping.update.internal.updatetranslator.update(ientitystatemanager statemanager, ientityadapter adapter) @ system.data.entityclient.entityadapter.update(ientitystatemanager entitycache) @ system.data.objects.objectcontext.savechanges(saveoptions options) @ system.data.entity.internal.internalcontext.savechanges() --- end of inner exception stack trace --- @ system.data.entity.internal.internalcontext.savechanges() @ system.data.entity.internal.lazyinternalcontext.savechanges() @ system.data.entity.dbcontext.savechanges() @ system.data.entity.migrations.dbmigrator.seeddatabase() @ system.data.entity.migrations.infrastructure.migratorloggingdecorator.seeddatabase() @ system.data.entity.migrations.dbmigrator.upgrade(ienumerable1 pendingmigrations, string targetmigrationid, string lastmigrationid) @ system.data.entity.migrations.infrastructure.migratorloggingdecorator.upgrade(ienumerable1 pendingmigrations, string targetmigrationid, string lastmigrationid) @ system.data.entity.migrations.dbmigrator.update(string targetmigration) @ system.data.entity.migrations.infrastructure.migratorbase.update(string targetmigration) @ system.data.entity.migrations.design.toolingfacade.updaterunner.runcore() @ system.data.entity.migrations.design.toolingfacade.baserunner.run() error occurred while updating entries. see inner exception details

is there better way model relationship between locations , deliveryrate. making mistake fluent api bit

one-to-one relationships separate foreign keys not supported entity framework. ef maps model shared primary keys, deliveryrate.id, deliveryrate.origin.id , deliveryrate.destination.id must have same value. have not in seed method cause of exception.

shared primary keys not useful model because never create deliveryrate has destination different origin.

you can solve problem modeling 2 relationships one-to-many:

modelbuilder.entity<deliveryrate>()     .hasrequired(e => e.origin)     .withmany()     .hasforeignkey(e => e.originid)     .willcascadeondelete(false);  modelbuilder.entity<deliveryrate>()     .hasrequired(e => e.destination)     .withmany()     .hasforeignkey(e => e.destinationid)     .willcascadeondelete(false); 

Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -