简体   繁体   English

实体框架创建了一个我不想要的表

[英]Entity Framework creates a table I don't want

I am using Entity Framework Code First approach to have Data Access in my application. 我正在使用Entity Framework Code First方法在我的应用程序中进行数据访问。 I have a table Web_Documents and a table Web_Profils and a relation table Web_Profil_joint_Documents , all of which I have made POCOs for. 我有一个表Web_Documents和一个表Web_Profils和一个关系表Web_Profil_joint_Documents ,所有这些都是我为POCO做的。 My problem is when I added a migration, Entity Framework created an extra relation's table.Now in my context I make a binding between both as follows: 我的问题是当我添加一个迁移时,Entity Framework创建了一个额外的关系表。现在在我的上下文中我在两者之间进行了绑定,如下所示:

modelBuilder.Entity<Web_Profils>()
    .HasMany<Web_Documents>(p => p.Documents)
    .WithMany(d => d.Profils)
    .Map(Web_Profil_joint_Documents =>
    {
        Web_Profil_joint_Documents.MapLeftKey("IDProfil");
        Web_Profil_joint_Documents.MapRightKey("IDDocument");
        Web_Profil_joint_Documents.ToTable("Web_Profil_joint_Documents");
    });

But I think this adds a new table, and since I already have Web_Profil_joint_Documents it creates Web_Profil_joint_Documents1 and adding data to the first, actually fills the second. 但我认为这会添加一个新表,因为我已经有了Web_Profil_joint_Documents它创建了Web_Profil_joint_Documents1并向第一个添加数据,实际上填充了第二个。 I have tried reverse engineer the tables to my model but that just created a whole mess. 我已经尝试将表格反向工程到我的模型,但这只是造成了一团糟。 I've also tried deleting the tables in SQL Server Management and then manually modifying migrations to have Web_Profil_joint_Documents only but had error everytime. 我还尝试删除SQL Server Management中的表,然后手动修改迁移以仅具有Web_Profil_joint_Documents但每次都有错误。

Does this code create a new table? 这段代码是否创建了一个新表? I thought it would add a many to many relationship between both tables and add it to the already existing relations table. 我认为它会在两个表之间添加多对多的关系并将其添加到已存在的关系表中。

Any ideas how to fix this? 任何想法如何解决这一问题?

You should not define related entity POCO class for many to many relationship. 您不应该为多对多关系定义相关实体POCO类。 Entity framework does generates it automatically for us. 实体框架确实为我们自动生成它。 From the comment I come to know that why you had define the relationship entity. 从评论中我了解到为什么你定义了关系实体。

Related entity will contain two columns. 相关实体将包含两列。

  1. Primary key of Web_Profils Web_Profils主键
  2. Primary key of Web_Documents . Web_Documents主键。

So far everything is simple. 到目前为止一切都很简单。 Now you wanted to access the data in relationship table (which is very very very rare) then you can do something like this. 现在你想访问关系表中的数据(这是非常非常罕见的)然后你可以做这样的事情。

suppose you have instance of web_profile and you wanted to get its record in relationship then first column is id (primary key) of instance and second you can get it by selecting id of its related collection of Web_Profil_joint_Documents . 假设你有web_profile的实例并且你想在关系中得到它的记录,那么第一列是实例的id(主键),第二列你可以通过选择其相关的Web_Profil_joint_Documents集合的id来获得它。

Web_Profils.Web_Profil_joint_Documents.Select(d => d.Id)

so in that way you also achieve second column of relationship table. 所以这样你也可以实现第二列关系表。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM