在EF Core中,我有两个彼此具有1-1关系的模型。
MultiPlayerPlay:
public class MultiPlayerPlay
{
public long Id { get; set; }
...
// Relations
...
public long? CallerPlayerPlayId { get; set; }
public virtual UserMutantPlay CallerPlayerPlay { get; set; }
public long? CalledPlayerPlayId { get; set; }
public virtual UserMutantPlay CalledPlayerPlay { get; set; }
...
}
和UserMutantPlay:
public class UserMutantPlay
{
public long Id { get; set; }
...
// Relations
...
public long? MultiPlayerPlayAsCallerId { get; set; }
public virtual MultiPlayerPlay MultiPlayerPlayAsCaller { get; set; }
public long? MultiPlayerPlayAsCalledId { get; set; }
public virtual MultiPlayerPlay MultiPlayerPlayAsCalled { get; set; }
}
和关系:
modelBuilder.Entity<MultiPlayerPlay>()
.HasOne(mpp => mpp.CallerPlayerPlay)
.WithOne(p => p.MultiPlayerPlayAsCaller)
.HasForeignKey<MultiPlayerPlay>(mpp => mpp.CallerPlayerPlayId)
//.HasForeignKey<UserMutantPlay>(ump => ump.MultiPlayerPlayAsCallerId)
.IsRequired(false)
.OnDelete(Microsoft.EntityFrameworkCore.Metadata.DeleteBehavior.Restrict);
modelBuilder.Entity<MultiPlayerPlay>()
.HasOne(mpp => mpp.CalledPlayerPlay)
.WithOne(p => p.MultiPlayerPlayAsCalled)
.HasForeignKey<MultiPlayerPlay>(mpp => mpp.CalledPlayerPlayId)
//.HasForeignKey<UserMutantPlay>(ump => ump.MultiPlayerPlayAsCalledId)
.IsRequired(false)
.OnDelete(Microsoft.EntityFrameworkCore.Metadata.DeleteBehavior.Restrict);
如您所见,MultiPlayerPlay与UserMutantPlay有两个1-1关系。但是当我试图运行这个陈述时:
MultiPlayerPlay multiPlayerPlay = new MultiPlayerPlay()
{
CallerPlayerId = inviterUser.Id,
CalledPlayerId = invitedUser.Id,
StillRunning = true,
StillRunningForCaller = true,
StillRunningForCalled = true,
MutantId = GetRandomMutantIdByLevelId(dbContext, inviterUser.Id, invitedUser.Id),
};
dbContext.MultiPlayerPlays.Add(multiPlayerPlay);
dbContext.SaveChanges();
第一次它工作正常。接下来,它提到我在FK关系中不能有多个空值。错误是这样的:
{System.Data.SqlClient.SqlException:无法在对象&#39; dbo.MultiPlayerPlays&#39;中插入重复的键行。具有唯一索引&#39; IX_MultiPlayerPlays_CalledPlayerPlayId&#39;。重复键值为()。
该语句已终止。
在System.Data.SqlClient.SqlConnection.OnError(SqlException异常,Boolean breakConnection,Action 1 wrapCloseInAction)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
at System.Data.SqlClient.SqlDataReader.TryConsumeMetaData()
at System.Data.SqlClient.SqlDataReader.get_MetaData()
at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds)
at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior)
at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.Execute(IRelationalConnection connection, String executeMethod, IReadOnlyDictionary
2 parameterValues,Boolean closeConnection)
在Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.ExecuteReader(IRelationalConnection连接,IReadOnlyDictionary`2 parameterValues)
在Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.Execute(IRelationalConnection连接)
ClientConnectionId:7c7adc7b-0172-4134-89c9-6b5f050ac860
错误号码:2601,州:1,班级:14}