如何在Entity Framework中Save/Update导航属性?
How to Save/Update navigation property in Entity Framework?
我正在使用 .Net 核心开发 Restful API。这里使用 Entity Framework 核心(代码优先迁移)与 SQL 服务器进行数据相关操作。
我的主要实体是:
public class Employee
{
public string Name { get; set; }
//...other properties.
public IList<Address> Addresses { get; set; }
}
其中public IList<Address> Addresses { get; set; }
是参考导航。
并且 Address
是一个依赖实体,如下所示:
public class Address
{
public string Address1 { get; set; }
//...other properties.
public Employee Employee { get; set; }
}
DbContext
是
public class OneToManyDbContext : DbContext
{
public DbSet<Employee> Employees { get; set; }
public DbSet<Address> Addresses { get; set; }
//..other config related connection string
}
Employee
的 API 控制器是
[Route("api/[controller]")]
[ApiController]
public class EmployeeController : ControllerBase
{
protected OneToManyDbContext _dbContext { get; set; }
public EmployeeController()
{
_dbContext = new OneToManyDbContext();
}
[HttpPost]
public void Add(Employee employee)
{
_dbContext.Employees.Add(employee);
_dbContext.SaveChanges();
}
}
仅与 Employee
实体相关的 CRUD 没有 Address
属性,一切正常。问题是如果我为 POST 方法发送嵌套有效负载,例如
{
"name":"Dennis",
//..other properties,
"addresses": {
"address1":"Place name",
//..other properties
}
}
地址是嵌套键,因为地址属于员工。现在 Add
方法失败了,因为它只需要 Employee
没有 Address
的对象。
错误信息是{"type":"https://tools.ietf.org/html/rfc7231#section-6.5.1","title":"One or more validation errors occurred.","status":400,"traceId":"|8f31a2b1-4bcda017ebe85390.","errors":{"$.Addresses":["The JSON value could not be converted to System.Collections.Generic.IList'1[OneToManyRelationships.Models.Address]. Path: $.Addresses | LineNumber: 4 | BytePositionInLine: 15."]}}
我该如何解决这个问题。有什么我可以做的,比如 serialization/deserialization 过程。我遵循存储库模式和工作单元,只是为了简化这个问题我没有把它放在这里。
同样的问题也适用于 Update/Delete 方法。
如果您 POST 员工有地址列表,应该没有问题。
问题是您发送模型的方式。 IList<Addreess>
是 JSON 中的对象数组。
I.E :[{},{}]
你在里面发送一个对象,而不是对象。即:{{},{}}
按照问题中提供的模型,发送的 JSON 对象应该是这样的:
{
name: "string",
//other values
addresses:
[
{
"address1":"string",
//other properties
},
{
"address1":"another string"
//some other properties
}
]
}
我正在使用 .Net 核心开发 Restful API。这里使用 Entity Framework 核心(代码优先迁移)与 SQL 服务器进行数据相关操作。
我的主要实体是:
public class Employee
{
public string Name { get; set; }
//...other properties.
public IList<Address> Addresses { get; set; }
}
其中public IList<Address> Addresses { get; set; }
是参考导航。
并且 Address
是一个依赖实体,如下所示:
public class Address
{
public string Address1 { get; set; }
//...other properties.
public Employee Employee { get; set; }
}
DbContext
是
public class OneToManyDbContext : DbContext
{
public DbSet<Employee> Employees { get; set; }
public DbSet<Address> Addresses { get; set; }
//..other config related connection string
}
Employee
的 API 控制器是
[Route("api/[controller]")]
[ApiController]
public class EmployeeController : ControllerBase
{
protected OneToManyDbContext _dbContext { get; set; }
public EmployeeController()
{
_dbContext = new OneToManyDbContext();
}
[HttpPost]
public void Add(Employee employee)
{
_dbContext.Employees.Add(employee);
_dbContext.SaveChanges();
}
}
仅与 Employee
实体相关的 CRUD 没有 Address
属性,一切正常。问题是如果我为 POST 方法发送嵌套有效负载,例如
{
"name":"Dennis",
//..other properties,
"addresses": {
"address1":"Place name",
//..other properties
}
}
地址是嵌套键,因为地址属于员工。现在 Add
方法失败了,因为它只需要 Employee
没有 Address
的对象。
错误信息是{"type":"https://tools.ietf.org/html/rfc7231#section-6.5.1","title":"One or more validation errors occurred.","status":400,"traceId":"|8f31a2b1-4bcda017ebe85390.","errors":{"$.Addresses":["The JSON value could not be converted to System.Collections.Generic.IList'1[OneToManyRelationships.Models.Address]. Path: $.Addresses | LineNumber: 4 | BytePositionInLine: 15."]}}
我该如何解决这个问题。有什么我可以做的,比如 serialization/deserialization 过程。我遵循存储库模式和工作单元,只是为了简化这个问题我没有把它放在这里。
同样的问题也适用于 Update/Delete 方法。
如果您 POST 员工有地址列表,应该没有问题。
问题是您发送模型的方式。 IList<Addreess>
是 JSON 中的对象数组。
I.E :[{},{}]
你在里面发送一个对象,而不是对象。即:{{},{}}
按照问题中提供的模型,发送的 JSON 对象应该是这样的:
{
name: "string",
//other values
addresses:
[
{
"address1":"string",
//other properties
},
{
"address1":"another string"
//some other properties
}
]
}