【发布时间】:2020-07-11 16:14:49
【问题描述】:
我在 MySql 服务器上使用来自 MySql 的 Sakila 示例数据库。该图如下所示。
重要的表是 store、inventory 和 film 表。表之间是多对多关系,链接器表是inventory表。
我使用 EFCore 2 在一个新的 dotnetcore 项目中搭建了这个数据库。 我正在尝试获取 商店 列表及其电影 列表。
实体定义如下:
商店
public class Store
{
public Store()
{
Customer = new HashSet<Customer>();
Inventory = new HashSet<Inventory>();
Staff = new HashSet<Staff>();
}
public byte StoreId { get; set; }
public byte ManagerStaffId { get; set; }
public short AddressId { get; set; }
public DateTimeOffset LastUpdate { get; set; }
public Address Address { get; set; }
public Staff ManagerStaff { get; set; }
public ICollection<Customer> Customer { get; set; }
public ICollection<Inventory> Inventory { get; set; }
public ICollection<Staff> Staff { get; set; }
}
库存
public partial class Inventory
{
public Inventory()
{
Rental = new HashSet<Rental>();
}
public int InventoryId { get; set; }
public short FilmId { get; set; }
public byte StoreId { get; set; }
public DateTimeOffset LastUpdate { get; set; }
public Film Film { get; set; }
public Store Store { get; set; }
public ICollection<Rental> Rental { get; set; }
}
电影
public partial class Film
{
public Film()
{
FilmActor = new HashSet<FilmActor>();
FilmCategory = new HashSet<FilmCategory>();
Inventory = new HashSet<Inventory>();
}
public short FilmId { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public short? ReleaseYear { get; set; }
public byte LanguageId { get; set; }
public byte? OriginalLanguageId { get; set; }
public byte RentalDuration { get; set; }
public decimal RentalRate { get; set; }
public short? Length { get; set; }
public decimal ReplacementCost { get; set; }
public string Rating { get; set; }
public string SpecialFeatures { get; set; }
public DateTimeOffset LastUpdate { get; set; }
public Language Language { get; set;
public Language OriginalLanguage { get; set; }
public ICollection<FilmActor> FilmActor { get; set; }
public ICollection<FilmCategory> FilmCategory { get; set; }
public ICollection<Inventory> Inventory { get; set; }
}
我的上下文如下所示:
modelBuilder.Entity<Inventory>(entity =>
{
entity.ToTable("inventory", "sakila");
entity.HasIndex(e => e.FilmId)
.HasName("idx_fk_film_id");
entity.HasIndex(e => new { e.StoreId, e.FilmId })
.HasName("idx_store_id_film_id");
最后,repo 如下所示:
public IEnumerable<Store> GetStores()
{
return _context.Store.
Include(a => a.Inventory).
ToList();
}
问题: 当我从控制器调用此方法以获取商店列表时,我在 Postman 上没有收到任何 json 响应。然而,如果我调试从控制器返回的列表,我会找到商店列表。 问题是该列表包含: store->inventory->film->store->inventory->film->store... 等等。创建一个循环依赖,填满请求的允许进程内存。
可能的解决方案: 我认为这与 Context 上的两个 外键 都被定义为 HasIndex 而不是 HasKey强>
entity.HasIndex(e => new { e.StoreId, e.FilmId })
.HasName("idx_store_id_film_id");
当我将它定义为 HasKey 时,我得到一个错误:
'从'Rental.Inventory'到'Inventory.Rental'的关系 外键属性 {'InventoryId' : int} 不能定位主 key {'StoreId' : byte, 'FilmId' : short} 因为它不兼容。 配置一个主键或一组兼容的外键 这种关系的属性。'
【问题讨论】:
-
嘿,你找到解决办法了吗?
标签: mysql ef-core-2.0 .net-core-2.0