【问题标题】:Why my save method always giving exception not saving values为什么我的保存方法总是给出异常而不保存值
【发布时间】:2019-04-03 18:21:17
【问题描述】:

我的系统上有 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

现在我的问题保存方法总是抛出异常。

我做错了什么??

我的堆栈跟踪:at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action1 wrapCloseInAction) 在 System.Data.SqlClient.SqlInternalConnection.OnError(SqlException 异常,布尔 breakConnection,Action1 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, TaskCompletionSource1 完成,Int32 超时,Task& 任务,布尔 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 执行方法,IReadOnlyDictionary2 parameterValues) at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.ExecuteReader(IRelationalConnection connection, IReadOnlyDictionary2 参数值) 在 Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.Execute(IRelationalConnection 连接) --- 内部异常堆栈跟踪结束 ---`

【问题讨论】:

  • 你得到了哪个例外?
  • Microsoft.EntityFrameworkCore.dll 中的 Microsoft.EntityFrameworkCore.DbUpdateException'
  • 更新条目时出错。有关详细信息,请参阅内部异常。
  • 出现异常是有原因的,您需要花一两秒时间阅读类型和消息。如果异常的消息状态为See the inner exception for details,那么您应该这样做...
  • 您需要阅读异常的消息。在这种情况下,堆栈跟踪并不那么有趣。

标签: c# asp.net-core


【解决方案1】:

您需要正确配置模型之间的关系,如下所示;

  • 每个用户将拥有一个公司和一个部门
  • 每个部门将有一家公司和许多用户
  • 每个公司都会有很多用户和很多部门

因此,您的数据模型必须如下所示:

public class User {
    public int Id { get; set; }
    public string Name { get; set; }

    public int CompanyId { get; set; }
    public Company Company { get; set; }

    public int DepartmentId { get; set; }
    public Department Department { get; set; }
}

Public class Department {
    public int Id { get; set; }
    public string Name { get; set; }

    public int CompanyId { get; set; }
    public Company Company { get; set; }

    public ICollection<User> Users { get; set; }
}

public class Company {
    public int Id { get; set; }
    public string Name { get; set; }

    public ICollection<Department> Departments { get; set; }

    public ICollection<User> Users { get; set; }
}

稍后我们可以看到您面临的错误。

【讨论】:

  • 问题我只想存储用户表上的名称和表公司上的公司名称然后在我的部门表上我将用户链接到公司并给出部门的名称
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多