我使用Enitity框架codefirst和我有以下的設計從屬角色有不同的價值觀多個主體
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; }
}
我不想定義位置上的關係。當我更新數據庫時,它可以工作,但是當我運行我的種子代碼時,我得到了參照完整性錯誤。我試圖用流利的API就如下
modelBuilder.Entity<DeliveryRate>()
.HasRequired(e => e.Origin)
.WithOptional()
.WillCascadeOnDelete(false);
modelBuilder.Entity<DeliveryRate>()
.HasRequired(e => e.Destination)
.WithOptional()
.WillCascadeOnDelete(false);
嘗試更新,我得到以下錯誤
System.Data.Entity.Infrastructure.DbUpdateException數據庫配置:更新時出現錯誤條目。詳情請參閱內部例外。 ---> System.Data.UpdateException:更新條目時發生錯誤。詳情請參閱內部例外。 ---> System.Data.ConstraintException:參照完整性約束衝突。依賴角色具有不同值的多個主體。 ---內部異常堆棧跟蹤--- 在System.Data.Mapping.Update.Internal.TableChangeProcessor.DiagnoseKeyCollision(UpdateCompiler編譯器,PropagatorResult更改,CompositeKey鍵,PropagatorResult其他) 在System.Data.Mapping.Update .Internal.TableChangeProcessor.ProcessKeys(UpdateCompiler compiler,List
1 changes, Set
1 keys) at System.Data.Mapping.Update.Internal.TableChangeProcessor.CompileCommands(ChangeNode changeNode,UpdateCompiler compiler) at System.Data.Mapping.Update.Internal.UpdateTranslator .d__0.MoveNext() at System.Linq.Enumerable.d__711.MoveNext() at System.Data.Mapping.Update.Internal.UpdateCommandOrderer..ctor(IEnumerable
1命令,UpdateTranslator翻譯器) at System.Data.Mapping.Update.Internal.UpdateTranslator.ProduceCommands() at System.Dat a.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager,IEntityAdapter adapter) at System.Data.EntityClient.EntityAdapter.Update(IEntityStateManager entityCache) at System.Data.Objects.ObjectContext.SaveChanges(SaveOptions options) at System.Data.Entity.Internal.InternalContext.SaveChanges() --- End of internal exception stack trace --- at System.Data.Entity.Internal.InternalContext.SaveChanges() at 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(IEnumerable的1 pendingMigrations, String targetMigrationId, String lastMigrationId) at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.Upgrade(IEnumerable
1 pendingMigrations,字符串targetMigrationId,字符串lastMigrationId) 在System.Data.Entity.Migrations.DbMigrator.Update(字符串targetMigration) 在System.Data.Entity的。 Migrations.Infrastructure.MigratorBase.Update(字符串targetMigration) 在System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.RunCore() 在System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.Run() 更新條目時發生錯誤。詳情請參閱內部例外
有沒有更好的方式來建模位置和DeliveryRate之間的關係。 我是否犯了流利的api位
嗨@Slauma,你說的話是有道理的。我其實並不打算共享主鍵,但我認爲EF會「知道」將OriginId映射到Origin和DestinationId到目標......它顯然沒有,並且因爲兩個值都不同而被炸燬。我通過在外鍵上使用[ForeignKey()]註解解決了這個問題,但是由於這與您的答案完全相同,所以我會帶上您的。隊友的歡呼聲! – 2013-03-19 11:57:10