猿问

EF 关系 - 由于 list<guid> 是原始的而无法映射

我想存储在用户类的项目集合中


但我无法克服这个问题:


System.InvalidOperationException:“无法映射属性“User.ItemsIds”,因为它属于“List”类型,它不是受支持的基本类型或有效的实体类型。显式映射此属性,或使用“[NotMapped]”属性或使用“OnModelCreating”中的“EntityTypeBuilder.Ignore”忽略它。


我正在使用 ASP.NET Core 2.0 + EFCore


public 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");

    }

}

这是类似的问题,但仍然没有“修复”?


红颜莎娜
浏览 239回答 2
2回答

森栏

您不能只存储依赖实体标识符。主体实体User必须收集Item:public class User{&nbsp; &nbsp; [Key]&nbsp; &nbsp; public Guid Id { get;&nbsp; set; }&nbsp; &nbsp; public virtual List<Item> Items { get; set; }&nbsp; &nbsp; // ...}
随时随地看视频慕课网APP
我要回答