c# - 如何防止 EntityFramework Core 创建关系
问题描述
我正在制作一个商店项目。
我创建了一个产品类:
public Guid Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
public string Description { get; set; }
public string PhotoUrl { get; set; }
public int Quantity { get; set; }
和一个订单类:
public Guid Id { get; set; }
public List<Product> Products { get; set; }
public decimal TotalPrice { get; set; }
//address
public string Street { get; set; }
public string HouseNumber { get; set; }
public string PostCode { get; set; }
public string City { get; set; }
public Order()
{
Products = new List<Product>();
}
正如您在 Order.cs 中看到的,有一个产品列表,但实体框架总是在我的产品和订单之间设置关系,但我只想将产品添加到此列表中,而没有关系。作为回应,我想得到这样的东西
{
"id" :"someID",
"products": [
{
first product
},
{
second product
}]
}
等。我怎样才能防止创建 ef 关系并做简单的列表?或者我怎样才能建立许多产品与许多订单的关系?
解决方案
您可以将 NotMapped 属性添加到 Property:
...
[NotMapped]
public List<Product> Products { get; set; }
...
您将需要导入 System.ComponentModel.DataAnnotations
推荐阅读
- ios - Objective-C - 如何在 UICollectionview 中添加静态第一个单元格?
- javascript - HTML 视频在 svg 中的 foreignObject 中不可见
- google-app-engine - 我的 PHP 项目是否需要 appengine-web.xml
- ios - 如何在 iOS 中将 alpha 图像转换为 8 位深度
- arrays - 离开 DispatchGroup 会导致我的代码崩溃
- sql - Oracle 查询到 SQL Server 查询
- java - SpringBootTest:没有可用的“org.springframework.test.web.servlet.MockMvc”类型的合格bean:
- symfony - 在 null 上调用成员函数 getAssociationAdmin()
- php - 如何在多个数组中查找和替换特定值
- xquery - 如何在 MarkLogic 数据中心框架中嵌入自定义代码?