[英]EntityFramework Add to Many-To-Many
I have a Vacation and a list of Countries. 我有一个假期和国家清单。 I whish to bind these together using a many-to-many relationship.
我希望通过多对多关系将它们绑定在一起。 I have a code-first Vacations model and Countries model.
我有一个代码优先的Vacations模型和Countrys模型。 Both the individual tables aswell as the join table are successfully generated.
单个表以及联接表均已成功生成。
However, when I try to add a country to a vacation (or vice versa) the join table remains empty. 但是,当我尝试将国家/地区添加到假期中时(反之亦然),联接表仍然为空。 I am able to successfully add the individual vacations aswell as the countries.
我能够成功添加个人假期以及国家/地区。
Models 楷模
public class Vacations
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int VacationId { get; set; }
public string ProductId { get; set; }
public string Name { get; set; }
public string Price { get; set; }
public virtual ICollection<Countries> Countries { get; set; }
public Vacations()
{
Countries = new List<Countries>();
}
}
public class Countries
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int CountryID { get; set; }
public string CountryName { get; set; }
public virtual ICollection<Vacations> Vacations { get; set; }
public Countries()
{
Vacations = new List<Vacations>();
}
}
public class MyContext : DbContext
{
public MyContext()
: base("myconn")
{
}
public DbSet<Vacations> Vacations { get; set; }
public DbSet<Countries> Countries { get; set; }
}
Insert the Vacation 插入假期
Vacations vacation = database.Vacations.Add(new Vacations
{
Name = vacationData.Name,
Description = vacationData.Description,
});
database.SaveChanges();
// to make sure the key is in the database to refrence
foreach (string country in AllMyCountries)
{
Countries countries = database.Countries.Add(new Countries
{
CountryName = country
});
countries.Vacations.Add(vacation);
vacation.Countries.Add(countries);
}
database.SaveChanges();
I have also tried just adding to one entity, and adding more calls to SaveChanges()
inbetween. 我也尝试过仅添加到一个实体,并在其间添加对
SaveChanges()
更多调用。
Interesting problem in that your sample code all looks perfectly okay. 有趣的问题在于您的示例代码看起来都还不错。 Infact, I put a quick test together around this code and Entity Framework created the mapping table
vacationscountries
as expected and populated it correctly. 实际上,我对这段代码进行了快速测试,实体框架按预期创建了映射表
vacationscountries
并正确填充了它。
Just on the off-change, can you confirm that you are looking in the mapping table created by Entity Framework and not a custom mapping table? 刚好在更改时,您可以确认您正在查找的是Entity Framework创建的映射表,而不是自定义映射表吗? The only reason I mention it is that theres no way (that I know of) to map a many-to-many relationship to a custom junction table using Data Annotations.
我提到它的唯一原因是(我所知道的)没有办法使用数据注释将多对多关系映射到自定义联结表。
If thats not the case, then the next thing I would do is to trace out the sql being generated by Entity Framework - either using your native database tracing tooling (eg Sql Profiler), a third party tool like EFProf, or through a logging interceptor ( http://msdn.microsoft.com/en-gb/data/dn469464.aspx ). 如果不是这种情况,那么我接下来要做的就是找出由Entity Framework生成的sql-使用您的本机数据库跟踪工具(例如Sql Profiler),诸如EFProf的第三方工具,或者通过日志记录拦截器( http://msdn.microsoft.com/en-gb/data/dn469464.aspx )。 Hopefully that will give some lower-level insights into the problem.
希望这将对问题提供一些底层的见解。
I re-created your Models but made some adjustments that generate the same results. 我重新创建了您的模型,但进行了一些调整以产生相同的结果。 I manually created the junction table
VacationsCountries
with the following code: 我使用以下代码手动创建了联结表
VacationsCountries
:
public class VacationsCountries
{
[Key, Column(Order = 0)]
public int VacationId { get; set; }
public virtual Vacations Vacation { get; set; }
[Key, Column(Order = 1)]
public int CountryId { get; set; }
public virtual Countries Country { get; set; }
}
I also added this line of code to the MyContext class: 我还将这一行代码添加到MyContext类中:
public DbSet<VacationsCountries> VacationsCountries { get; set; }
Instead of using: 而不是使用:
countries.Vacations.Add(vacation);
vacation.Countries.Add(countries);
I use: 我用:
VacationsCountries vc = database.VacationsCountries.Add(new VacationsCountries
{
Country = countries,
Vacation = vacation
});
and then call database.SaveChanges();
然后调用
database.SaveChanges();
I checked the database and the entries were added to the VacationsCountries table. 我检查了数据库,并将条目添加到VacationsCountries表中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.