![](/img/trans.png)
[英]Convert SQL query with a one-to-many relationship to an ASP.NET Core / Entity Framework Core call with a parent/children object structure
[英]ASP.NET Core 3.1 and Entity Framework Core : one-to-many relationship
一般来说,我是 Entity Framework Core 3.1 和 ASP.NET Core 3.1 的新手。 我要做的是设置我的表Categories
、 Manufacturers
、 Conditions
、 Locations
和AssetFiles
与我的Asset
表建立一对多的关系。
这些是我目前拥有的数据模型
Asset
表
public class Asset
{
public int Id { get; set; }
public Categories Category { get; set; }
public Manufacturers Manufacturer { get; set; }
public string Model { get; set; }
public string SerialNumber { get; set; }
public string PurchasePlace { get; set; }
public int Quantity { get; set; }
public DateTime AcquiredDate { get; set; }
public string PurchasePrice { get; set; }
public string CurrentValue { get; set; }
public Conditions Condition { get; set; }
public Locations Location { get; set; }
public AssetFiles Files { get; set; }
}
Categories
表
public class Categories
{
public int Id { get; set; }
public string CategoryName { get; set; }
public ICollection<Asset> Assets { get; set; }
}
其他表格的格式几乎相同。 我可以创建迁移并更新数据库以便创建它。 在我看来,它正在按照我想要的方式工作。
我的问题是如何在[HttpPost]
方法中访问我的资产 controller 中的这些表,以便能够在资产发布时向Asset
添加类别、制造商、条件、位置,因为现在使用 controller 搭建了所有我的脚手架对于我的资产的这些部分,我得到的是 null
这是我的assetController
的 post 方法:
[HttpPost]
public async Task<ActionResult<Asset>> PostAsset([FromForm] Asset asset)
{
_context.Assets.Add(asset);
await _context.SaveChangesAsync();
return CreatedAtAction("GetAsset", new { id = asset.Id }, asset);
}
我只是在寻找一个关于我需要如何看待这个向前发展的小方向。
任何帮助,将不胜感激。
编辑:2
还要提一下,我正在使用 VueJs 前端,不确定这对您提供的提示是否重要,或者访问数据是否相同。
您的字符串值:Tool、Ikea 等不会被神奇地转换为 C# 对象。 如果你想通过表单数据传递 object,你可以像这样在 postman 中进行 POST:
这将初始化您的类别 object,Id=0 和 Name=“工具”。 如果您想使用您的请求创建新类别并且 id 将由数据库引擎分配(最常见的方式),则此解决方案是合适的。
但我想您想在创建资产时使用现有类别。 您最好创建另一个端点: POST /api/categories
。 端点应返回带有数据库类别 ID 的新类别。 类别 ID 可用于POST /asset
请求。
您的 API controller 不应将 EF 实体作为参数。 而是使用ViewModel :
public class AssetViewModel
{
public int CategoryId { get; set; }
public int ManufacturerId { get; set; }
public string Model { get; set; }
public string SerialNumber { get; set; }
public string PurchasePlace { get; set; }
public int Quantity { get; set; }
public DateTime AcquiredDate { get; set; }
public string PurchasePrice { get; set; }
public string CurrentValue { get; set; }
public int ConditionId { get; set; }
public int LocationId { get; set; }
public int AssetFilesId { get; set; }
}
在您的 controller 中,您应该检查数据库中是否存在具有 CategoryId 的类别。 如果不返回NotFound
或BadRequest
。
您可能还想在同一个请求中创建例如 AssetFiles。 而不是属性int AssetFilesId
,您可以使用AssetFileViewModel
并如上所述发布它。
public class AssetViewModel
{
public int CategoryId { get; set; }
...
public AssetFileViewModel AssetFile { get; set; }
}
public class AssetFileViewModel
{
public string Name { get; set; }
public string SomeOtherProperty { get; set; }
}
最后,您可以从 ViewModel 创建资产并将其保存到数据库。
您确定要使用表单数据体。 最常见的方法是使用 JSON (在 postman 中选择 Body -> Raw -> JSON)
这是表单数据 model 绑定问题。 要将嵌套对象绑定到 model,您需要指定子 object 的属性名称,并以 ModelName.PropertyName 格式命名。
以 Category 为例,如果要绑定它的 Id 和 Name,应该这样写: Category.Id = 1
, Category.CategoryName = "Tool"
。
关于model绑定的更多细节,可以参考文档
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.