我在实体框架的映射方面遇到了一些问题。我有一个表Check,它有一个指向表CheckStatusHistory的外键,我在这个表中存储了一段时间内所有检查状态的变化。正如您所看到的,Check有一列LastCheckStatusID,它是FK to CheckStatusHistoryID,但是表CheckStatusHistory也有CheckID作为Check的FK to CheckID列。这个想法是将最后的CheckStatusHistoryID存储在检查中,以便更容易地获得最后的检查状态,并具有更好的性能。而且,也要有所有的历史信息。
然后,当我从DataBase生成实体框架实体时,我得到了这个图:

它的*(从检查)到1 (CheckStatusHistory),应该是1比1。但由于EF的限制,这是不可能的。
然后,在我的代码中,当我想要创建一个状态历史检查时,我会这样做:
Check newCheck = new Check
{
Amount = c.Amount,
CheckDate = c.CheckDate,
CheckNumber = c.CheckNumber,
CheckPrefix = c.CheckPrefix,
Days = c.Days,
Expenses = c.Expenses,
RoutingNumbers = c.RoutingNumbers,
TradeHours = c.TradeHours,
TotalInterest = c.TotalInterest,
TentativeDepositDate = c.TentativeDepositDate,
TentativeAccreditationDate = c.TentativeAccreditationDate,
MonthlyRate = c.MonthlyRate
};
newCheck.CheckStatusHistory = new CheckStatusHistory
{
CheckStatusID = CheckStatusIDs.Nuevo,
StatusDateTime = DateTime.Now,
};这应该是在CheckStatusHistory中添加一行。此newCheck.CheckStatusHistory是与LastCheckStatusID相关的CheckStatusHistory。
但是当我执行db.SaveChanges()时。
我得到以下错误:
System.Data.Entity.Infrastructure.DbUpdateException was unhandled by user code
HResult=-2146233087
Message=Unable to determine a valid ordering for dependent operations. Dependencies may exist due to foreign key constraints, model requirements, or store-generated values.
Source=EntityFramework
StackTrace:
en System.Data.Entity.Internal.InternalContext.SaveChanges()
en System.Data.Entity.Internal.LazyInternalContext.SaveChanges()
en System.Data.Entity.DbContext.SaveChanges()
en Plutus.Services.Check.CreateOperationHandler.Handle(CreateOperationRequest request) en e:\Plutus\Plutus.Services\Plutus.Services\Check\CreateOperationHandler.cs:línea 169
en Plutus.Services.RequestResponse.RequestResponseFactory.Handle[TRequest,TResponse](TRequest request) en e:\Plutus\Plutus.Services\Plutus.Services\RequestResponse\RequestResponseFactory.cs:línea 48
en Plutus.Services.Host.CheckService.CreateOperation(CreateOperationRequest request) en e:\Plutus\Plutus.Services\Plutus.Services.Host\CheckService.svc.cs:línea 50
en SyncInvokeCreateOperation(Object , Object[] , Object[] )
en System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]& outputs)
en System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc)
InnerException: System.Data.Entity.Core.UpdateException
HResult=-2146233087
Message=Unable to determine a valid ordering for dependent operations. Dependencies may exist due to foreign key constraints, model requirements, or store-generated values.
Source=EntityFramework
StackTrace:
en System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.DependencyOrderingError(IEnumerable`1 remainder)
en System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.ProduceCommands()
en System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.Update()
en System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.<Update>b__2(UpdateTranslator ut)
en System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.Update[T](T noChangesResult, Func`2 updateFunction, Boolean throwOnClosedConnection)
en System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.Update(Boolean throwOnClosedConnection)
en System.Data.Entity.Core.Objects.ObjectContext.<SaveChangesToStore>b__33()
en System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
en System.Data.Entity.Core.Objects.ObjectContext.SaveChangesToStore(SaveOptions options, IDbExecutionStrategy executionStrategy)
en System.Data.Entity.Core.Objects.ObjectContext.<>c__DisplayClass28.<SaveChanges>b__25()
en System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation)
en System.Data.Entity.Core.Objects.ObjectContext.SaveChanges(SaveOptions options)
en System.Data.Entity.Internal.InternalContext.SaveChanges()
InnerException: 我认为这可能与两个FK有关,一个从一个表到另一个表,另一个从最后一个表到第一个表。
我需要一些帮助。
我把SQL表留在这里:
CREATE TABLE [dbo].[Check](
[CheckID] INT PRIMARY KEY IDENTITY NOT NULL,
[RoutingNumbers] NCHAR(29) NOT NULL,
[BankID] INT NOT NULL, --FK
[BankBranchOfficeID] INT NOT NULL, -- FK
[BankAccountID] INT NOT NULL,
[CheckPrefix] NVARCHAR(3) NOT NULL,
[CheckNumber] NCHAR(7) NOT NULL,
[CheckDate] DATE NOT NULL,
[Amount] MONEY NOT NULL,
[TentativeDepositDate] DATE NOT NULL,
[TradeHours] INT NOT NULL,
[TentativeAccreditationDate] DATE NOT NULL,
[Expenses] MONEY NULL,
[MonthlyRate] DECIMAL (6,2) NOT NULL,
[TotalInterest] MONEY NOT NULL,
[Days] INT NOT NULL,
[LastCheckStatusID] INT NOT NULL,
[OperationID] INT NOT NULL,--FK
CONSTRAINT FK_Check_BankID_Bank FOREIGN KEY ([BankID]) REFERENCES [dbo].[Bank]([BankID]),
CONSTRAINT FK_Check_BankBranchOfficeID_BankBranchOffice FOREIGN KEY ([BankBranchOfficeID]) REFERENCES [dbo].[BankBranchOffice]([BankBranchOfficeID]),
CONSTRAINT FK_Check_BankAccountID_BankAccount FOREIGN KEY ([BankAccountID]) REFERENCES [dbo].[BankAccount]([BankAccountID]),
CONSTRAINT FK_Check_CheckStatusHistoryID_CheckStatusHistory FOREIGN KEY (LastCheckStatusID) REFERENCES [dbo].[CheckStatusHistory]([CheckStatusHistoryID]),
CONSTRAINT FK_Check_OperationID_Operation FOREIGN KEY ([OperationID]) REFERENCES [dbo].[Operation]([OperationID])
)
/*---------------------------------------------------------------
ESTADO DE CHEQUES
*/---------------------------------------------------------------
CREATE TABLE [dbo].CheckStatus(
[CheckStatusID] TINYINT PRIMARY KEY,
[Name] NVARCHAR(30) NOT NULL
)
/*---------------------------------------------------------------
RELACION ESTADO - CHEQUE
*/---------------------------------------------------------------
CREATE TABLE [dbo].[CheckStatusHistory](
[CheckStatusHistoryID] INT PRIMARY KEY IDENTITY,
[CheckStatusID] TINYINT NOT NULL, --FK
[CheckID] INT NOT NULL,
[StatusDateTime] DATETIME NOT NULL
CONSTRAINT FK_CheckStatusHistory_CheckID_Check FOREIGN KEY ([CheckID]) REFERENCES [dbo].[Check]([CheckID]),
CONSTRAINT FK_CheckStatusHistory_CheckStatusID_CheckStatus FOREIGN KEY ([CheckStatusID]) REFERENCES [dbo].[CheckStatus]([CheckStatusID])
)发布于 2014-01-16 06:40:47
是的,它与两个FK相关。EF使用该模型来创建DB命令的排序。它不会检查您设置的确切数据。它试图找到一种在每种情况下都有效的排序-这种排序在您的情况下不存在,因为您的模型允许创建两个相互依赖的记录(每个表中一个)。在这种情况下,不可能在一次传递中插入它们。在DB级,您只需将第一个没有依赖关系的对象插入到第二个对象中,将第二个对象与第一个对象的依赖关系插入,然后将第一个对象与第二个对象的依赖关系更新。EF不是这样工作的。
其他人可能不同意,但随着时间的推移,我得出结论,历史表在不受不同引用约束的情况下工作得最好。例如,允许保留从主表中删除的数据的历史记录。你需要StatusHistory的FK来检查吗?即使没有FK,您仍然可以存储CheckID并执行手动Linq查询来获取历史记录以进行检查,但您将丢失导航属性属性。
https://stackoverflow.com/questions/21077739
复制相似问题