Why cannot the Entity Data Model Wizard work for many-many related tables? - entity-framework

I have database with 3 tables as follows:
From within Visual Web Developer 2010 Express, I create an EF model using Entity Data Model Wizard. I select the 3 tables. Unfortunately, the resulting EF model does not contain the junction table, i.e., QuestionsTags table. The following figure shows the EF model diagram.
My question: Why cannot the Entity Data Model Wizard work for many-many related tables?

This is the difference between physical model and conceptual model. In physical model you use junction able to define M:N relation because relation databases don't support it natively. In conceptual model you do not deal with physical storage. Junction table is not included in conceptual model because it is not needed. You don't need to access it, you need to access Tags related to Questions or Questions related to Tags. Those relations are directly accessible by navigation properties.
Junction table will be automatically added to model only if it contains additional columns (not only FKs to build M:N relation). It is also possible to manually modify (EF4) model and force it to add entity for junction table.

It works. Notice the navigation properties at the bottom of your EF model diagram.
The QuestionsTags table only exists to model the many-to-many relationship in a relational database. When you have objects that don't have to fit into a rigid table schema, you can use a collection on a question object to get all the tags on that question, and likewise, a collection on a tag object to get all the questions with that tag...The Entity Framework models this for you and will populate these collections automatically.

Related

Is it possible to associate entities in different models?

We have one DB with many tables. We decided to create different models instead of just one containing all the tables.
We'd now need to associate an Entity (table) in ModelA with an Entity (table) in ModelB. Obviously at DB level this is possible (a simple foreign key) but it looks like it is not at model level.
Suggestions?
Your database is composed of one big model with many tables and relationships
But in your application if you are going to split it into two models then there is a lot of chance of having "Entities" that will be needed in different models.
You can create two different entities instead each representing what is needed from the table in each particular model and map each entity to the same table in Entity Framework
See the image below
The black boxes are your tables, the red and blue are your entites.
As you can see the whole database is related but your ModelA includes only some fields on the common table, while Model B includes its own set of fields from the common table.

Proper way to generate a view from a many to many relationship?

I have two tables - ContactInformation & EmailAddress - which have a many to many (* : *) relationship. After making this association in the ADO.NET data model and generate the db from it, a table titled ContactInformationEmailAddresses which maps the two tables is created in the Entities Container.
Unlike when I scaffold views which have a 1:* relationship, there's no entries available for its counterpart in the view when I scaffold either one and scaffolding off the combined table isn't an option even after updating the model from the db.
My question is simply: is there an automated way to generate the creation form for ContactInformationEmailAddresses that will have the EmailAddress entry field?
At present the scaffolding templates don’t support generating views that require the selection and association with more than one entity – the “many” end of the association.
Make sure to check out this blog article
http://blogs.msdn.com/b/mcsuksoldev/archive/2013/09/20/managing-entity-relationships-with-mvc-scaffolding.aspx

Entity Framework create a logical relationship

I'm mapping a database, which has no physical relationships whatsoever. But tables are related. (they use SP to do their relationships).
Is there a way I can logically create the relationship on the data model I generated? I'd love if eager loading works with this also (even tho no physical relationship exists).
You can add the relationships manually in the model.
One way to do this is to open the edmx file using an xml editor and edit it directly.
If you create a test database with a few related tables and then generate a model from that database, you can see how relationships are defined in an edmx file.

Entity Framework - Association Set

I am converting a project from another ORM to Entity Framework. I have a table where all 3 fields are foreign keys. So this table has been automatically mapped as an Association Set. In the previous ORM I could still work with this table as an entity - writing linq statements against it, adding and deleting objects etc. Is it possible to do this in Entity Framework with a table that has been mapped as an Association Set? I think that in the other ORM I had an option when mapping to treat the table as an entity rather than just as a collection.
In Entity framework you do not access the connection tables directly.
You relate objects to each other, and the framework adds or removes the relevant rows in the connection tables.
It can be confusing at first, but once you get used to it, it simplifies your code.

Entity Framework - Where are my Navigation Properties?

When I am generating ADO.NET Entity Data Model from Northwind I am getting well-structured Data Models with both scalar and navigation properties, but when I am generating from my own Database, I get only scalar properties and no navigation properties.
Why?
and how can I get those navigation properties for my own database Tables?
Your DB is not structured in a way that the Add/Update wizard can recognize. Not using foreign keys, for example, would cause this behavior.
If this is not the problem and you can't figure out why the wizard doesn't recognize your keys, post the DDL for two related tables and any constraints you may have defined between them and I'll try and help.