How does EF know the name of the joining table in a many-to-many relationship - entity-framework

I've started working on a mature code-first EF6 project. There is no EDMX file. I can't find anywhere in the code where the names of the joining tables that support many-to-many relationships is specified, and yet EF knows what they are. How?

The EF model is built by a set of conventions, which you can override with attributes or code-based configuration.

Related

How to cleanly generate POCO classes from existing database using Entity Framework 4.3 Code First approach?

I'm following the EF Code-First approach in a project that works against an existing database, to which I'm adding tables as needed.
This database has a number of tables for which I need to generate POCO classes for, and so I was wondering if there was a straight-forward, clean approach, to generating simple POCO classes from the database ... from which I can continue to work with using the general Code-First paradigm?
You can use the Entity Framework Power Tools for that.
If you want just the simple Poco classes without any relations use this T4 template
Generate entity class from database table

EF4 POCO duplicate table names

I'm currently trying to migrate a project to EF4 POCOs in order to get rid of EntityObject in my business logic and ran into an issue with duplicate table names. The DAL has access to 3 different databases and there are 3 .edmx files, one for each database.
However, some tables in those databases share the same name, e.g. DB1.CUSTOMER and DB2.CUSTOMER. I managed to have the related entities created in different namespaces (one namespace for each database) like MyApp.Db1.CUSTOMER and MyApp.Db2.CUSTOMER, the trouble is EF can't decide which one to pick and claims there was an ambiguity which isn't actually the case.
Is there any way of mapping Entities to their respective POCOs manually or any sort of workaround? This is EF 4.2.
EF doesn't use namespace when recognizing entity type. The name of class is matched directly with name of entity in your model diagram (EDMX). So the workaround is using different names in different models which will also make your code much better readable.

Entity Framework 4.0 Mapping POCOS with different property names from db fields names

I'm a newbie to ADO.Net Entity framework 4. I have a set of pocos which I need to map to a legacy database. The problem is that the db field names are different to the poco property names. eg. db field name = 'cusID' and poco property = 'CustomerID'.
What is the best way to map these?
This is exactly the problem EF mapping is designed to solve.
Your POCO class need to match your 'conceptual model'... Not your 'data model'.
If in EF you build your model from the database you simply need to rename your entity properties. Doing this changes the conceptual model - to match your POCO classes - but leaves the storage model unchanged, and sets up the appropriate mappings.
Entity Framework CTP4 has a new feature called Code First that allows you to map POCO property members to database table column names. This blog article may be what you are looking for,
http://theminimalistdeveloper.com/2010/07/28/how-to-map-pocos-to-existing-databases-in-entity-framework-4-0-code-first-and-asp-net-mvc-2/
Additionally, EF CTP 5 - which will be released in the next few weeks - has a better API to fluently configure your own conventions to map your POCO domain classes to existing database structures.
Hope this helps.
Update Here is the new article that discusses how to achieve this in EF4 CTP5

How to map 2+ entities to same table in .NET Entity Framework?

I'm trying to create 2 entities that operate as different views on the same underlying db table. When I create these in Visual Studio's entity model and place an association between them, I get a "Association is not mapped" error. I read an article (http://blogs.msdn.com/adonet/archive/2008/12/05/table-splitting-mapping-multiple-entity-types-to-the-same-table.aspx) that describes how to hand-code the XML in the edmx to add a ReferentialConstraint but that didn't help me any.
Any thoughts? Does the designer not support this?
From the error, I'd guess that there wasn't an Association created for the storage schema in the edmx (the SSDL of the edmx XML). A conceptual association has been created if you can see it in the designer, but there's no storage definition behind the scenes. I'm guessin'. =)

Linq to Entities / Entity Framework cross edmx "join"

I have two entities, each from a different database and therefore different edmx files. There is, however, an infered relationship between them.
Foo has many Bars for example.
What's the easiest way to do this join in the EntityFramework, with fewest database calls?
Thanks.
You can do this by adding cross-model navigation properties. This requires manually editing the EDMX. There is an example, including LINQ to Entities, here.
You can do this by using Views to represent data in the other database. Read about cross database joins in EF