[英]many to many relationship adding items
I'm using the following two classes as suggested in this website: https://www.entityframeworktutorial.net/code-first/configure-many-to-many-relationship-in-code-first.aspx 我正在使用本网站建议的以下两个类: https : //www.entityframeworktutorial.net/code-first/configure-many-to-many-relationship-in-code-first.aspx
public class Barca
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int BarcaID { get; set; }
public virtual ICollection<Imbarco> Imbarco { get; set; }
public virtual Imbarco ImbarcoBase { get; set; }
}
public class Imbarco
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ImbarcoID { get; set; }
public virtual ICollection<Barca> Barche { get; set; }
public virtual ICollection<Barca> BaseBarche { get; set; }
}
I need to create 1 to M relationship and for that I used these 2 navigation properties: 我需要创建1到M的关系,为此我使用了这两个导航属性:
public virtual Imbarco ImbarcoBase { get; set; }
public virtual ICollection<Barca> BaseBarche { get; set; }
but I need also a M to M relationship and for that I used: 但我还需要一个M到M的关系,为此我用过:
public virtual ICollection<Imbarco> Imbarco { get; set; }
public virtual ICollection<Barca> Barche { get; set; }
when I try to add a new item in the M to M, the bridge table is not created in the database, as described in the link reported above and also the edmx file I tried to generate in VisualStudio created a 0..1 to M and a M to 0..1 relationship instead of m to M. What am I doing wrong ? 当我尝试在M到M中添加新项目时,不会在数据库中创建桥接表,如上面报告的链接中所述,我尝试在VisualStudio中生成的edmx文件创建了0..1到M和M到0..1的关系而不是m到M.我做错了什么?
I would start by fixing your model. 我会先修复你的模型。 I recommend you configure with fluent api , but you can try the InverseProperty annotation so EF makes the proper connections: 我建议您使用流畅的api配置,但您可以尝试使用InverseProperty注释,以便EF建立正确的连接:
public class Imbarco
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ImbarcoID { get; set; }
[InverseProperty("Imbarco")]
public virtual ICollection<Barca> Barche { get; set; }
[InverseProperty("ImbarcoBase")]
public virtual ICollection<Barca> BaseBarche { get; set; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.