Entity Framework 4.0 - EF - maps many-to-many rela

2019-07-30 04:42发布

问题:

Sorry I don't speak EF lingo that well, having just set up my first EF project. I'm used to working with Linq-to-SQL as a way of avoiding SQL queries, and it worked well with some WCF web services I used. All this in Microsoft Visual Studio 2010.

Now with that background, I created a database using Servr that has Tables A and B in a many to many relationship with Table C. Since it is illegal to do this in database theory, I set up a linking table, as is well known. Two of them: one called A_C, and the other called B_C, each having a foreign key (1 to many) from tables A and B, respectively. Then I linked table C t both these linking tables, with these linking tables receiving a foreign key from C. So now I am OK I thought.

So I create an ADO.NET entity data model, using the Entity Data Model Wizard, I picked the option "from an existing database", and clicked the buttons. Much to my surprise, and chagrin, and shock and awe, I saw ONLY THREE TABLES: A, B and C! No linking tables whatsoever. Are they kidding me?

Think about this for a moment. The engine that runs the EF figured out that tables A and B share a "many-to-many" relationship with Table C. In the schema diagram of the .edmx file there is show asterisks between A,B and C, exactly as expected.

BUT NO LINKING TABLES ARE SHOWN IN THE SCHEMA! What? It's very logical I suppose not to show the linking tables...but unexpected.

Is this your experience, for anybody who has ever done many-to-many table relationships?

BTW, before I hit the Send key, I did a lot of research on EF 4.0, and I find it is a work in progress it seems. Lots of pitfalls, that I will probably fall into. For example, I intend to deploy this in a WCF REST solution that uses web methods that work with a SQL Server database. In the past I've used Linq-to-SQL and now I am going to try, guardedly, to use Linq-to-Entities.

回答1:

That is exactly what is supposed to happen. And all three tables will be updated correctly.