我的系统上有3个模型
Public class Company {Id,Name}//Already have data
Public Class User {Id, Name}//Already have data
Public Class Department {Id,Name,CompanyId,UserId,Public Company Companies,Public User Users}
现在我想为我的部门模型创建种子数据。 我的种子数据类中的代码;
if(!context.Department.Any()){
try {
var departments = new Department[]{
new Department{ComapnyId=context.Companies.Single(s => s.Name=="Stackoverflow").Id,
UserId =context.Users.Single(s => s.Name=="Admin").Id, Name="IT"}
};
foreach (Department department in departments)
{
context.Department.Add(department);
}
context.SaveChanges();
}catch (Exception e)
{
throw new Exception(e.Message);
}//End Try and Catch
}//End If
现在,我的问题保存方法总是删除异常。
我在做什么错??
我的Stacktrace:at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action
1 wrapCloseInAction)
在System.Data.SqlClient.SqlInternalConnection.OnError(SqlException异常,布尔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.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, TaskCompletionSource
1完成,Int32超时,任务和任务,布尔asyncWrite,String方法)
在System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior行为)
在System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior行为)
在System.Data.Common.DbCommand.ExecuteReader()
在Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.Execute(IRelationalConnection连接,DbCommandMethod executeMethod,IReadOnlyDictionary 2 parameterValues)
at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.ExecuteReader(IRelationalConnection connection, IReadOnlyDictionary
2参数值)处
在Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.Execute(IRelationalConnection连接)
---内部异常堆栈跟踪的结尾---`
答案 0 :(得分:0)
您需要按照以下说明正确配置模型之间的关联;
因此,您的数据模型必须如下所示:
select distinct on (subject_id) subject_id, hadm_id
from labevents
where itemid in ('50809','50931','51529')
AND valuenum > 110
and hadm_id is not null
order by subject_id, row_id;
稍后我们可以看到您面临的错误。