我想存储在用户类的项目集合中但我无法克服这个问题:System.InvalidOperationException:“无法映射属性“User.ItemsIds”,因为它属于“List”类型,它不是受支持的基本类型或有效的实体类型。显式映射此属性,或使用“[NotMapped]”属性或使用“OnModelCreating”中的“EntityTypeBuilder.Ignore”忽略它。我正在使用 ASP.NET Core 2.0 + EFCorepublic class User{ [Key] public Guid Id { get; set; } [ForeignKey("Items")] public virtual List<Guid> ItemsIds{ get; set; } (...)}public class Item{ [Key] public Guid Id { get; set; } public string ItemName { get; set; } public Item(string name) { Id = new Guid(); ItemName = name; }}public class AppContext : DbContext{ public AppContext(DbContextOptions<AppContext> options) : base(options) { Database.SetCommandTimeout(35000); } public DbSet<User> Users { get; set; } public DbSet<Item> Items { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<User>().ToTable("Users"); modelBuilder.Entity<Item>().ToTable("Items"); }}这是类似的问题,但仍然没有“修复”?
2 回答
![?](http://img1.sycdn.imooc.com/54584d080001566902200220-100-100.jpg)
森栏
TA贡献1810条经验 获得超5个赞
您不能只存储依赖实体标识符。
主体实体User必须收集Item:
public class User
{
[Key]
public Guid Id { get; set; }
public virtual List<Item> Items { get; set; }
// ...
}
- 2 回答
- 0 关注
- 238 浏览
添加回答
举报
0/150
提交
取消