简体   繁体   English

如何播种外键一对多列表关系?

[英]How do I seed a foreign key one-to-many list relation?

I have class/table:我有课/表:

public class Song
{
    [Key]
    public Guid Id { get; set; }
    [Required]
    public string Name { get; set; }
    [ForeignKey("FK_SongTags")]
    public List<Tag> Tags { get; set; }
}

Where class/table Tag is:类/表标签在哪里:

[Table("Tags")]
public class Tag
{
    [Key]
    public string Name { get; set; }
}

In my Context I call this seed:在我的上下文中,我称这个种子为:

modelBuilder.Entity<Song>().HasData(new List<Song>
{
    new Song { Id = Guid.NewGuid(), Name = "Some random song", Tags = new List<Tag> { "pop", "rock" } }
});

When I do do.net ef database update , I get:当我执行do.net ef database update时,我得到:

The seed entity for entity type 'Song' cannot be added because it has the navigation 'Tags' set.无法添加实体类型“歌曲”的种子实体,因为它设置了导航“标签”。 To seed relationships, add the entity seed to 'Tag' and specify the foreign key values {'FK_SongTags'}.要播种关系,请将实体种子添加到“标签”并指定外键值 {'FK_SongTags'}。 Consider using 'DbContextOptionsBuilder.EnableSensitiveDataLogging' to see the involved property values.考虑使用“DbContextOptionsBuilder.EnableSensitiveDataLogging”来查看涉及的属性值。

How do I solve this error?我该如何解决这个错误?

You need many-to-many relations since one song can have several tags, and each tag can be used for another songs too.您需要多对多关系,因为一首歌曲可以有多个标签,每个标签也可以用于另一首歌曲。 Try this:尝试这个:

public class Song
{
    [Key]
    public Guid Id { get; set; }
    [Required]
    public string Name { get; set; }
  
    public virtual List<Tag> Tags { get; set; }
}


[Table("Tags")]
public class Tag
{
    [Key]
    public string Name { get; set; }
    public virtual List<Song> Songs{ get; set; }
}

you will have to make an initial migration to db after making changes.进行更改后,您将必须初始迁移到数据库。

but if you still think that you need one to many try this但如果你仍然认为你需要一对多试试这个

[Table("Tags")]
public class Tag
{
    [Key]
    public string Name { get; set; }

    public Guid SongId {get; set;}
     public virtual Song Song {get; set;}

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在nhibernate4中不使用外键创建一对多关系 - Create relation one-to-many without Foreign Key in nhibernate4 实体框架 - 在Seed()方法中实现一对多外键关系 - Entity Framework - implementing one-to-many foreign key relationship in Seed() method 如何在与EF映射的一对多关系中更新外键? - How to update the foreign key in a one-to-many relationship mapped with EF? 如果关系是一对多,如何在我的外键列上插入值 - How can I insert value on my Foreign Key Column if the relationship is one-to-many 导航属性名称与属性类型不同时,NHibernate多对一/一对多外键如何处理? - How to do a NHibernate many-to-one/one-to-many foreign key in case of navigation property name different from property type? 如何指定一对多关系中哪一列是外键,哪一列是主键。 使用实体框架核心 - How do I specify which column is the Foreign key and which column is the Primary Key on a one-to-many relationship. Using Entity Framework Core 为非主键属性配置一对多外键 - Configuring one-to-many foreign key to a non primary key property 下拉列表中的一对多关系 - One-to-Many Relation In Dropdown 如何使用SauceDB将一对多关系转换为列表属性? - How do I turn a one-to-many relationship into a list property using SauceDB? NHibernate单向一对多关系不保存外键 - NHibernate unidirectional one-to-many relationship not saving foreign key
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM