Entity Framework - Association Set - entity-framework

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.

Related

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.

Defining business objects in Entity Framework

Trying to understand Entity Framework. My approach is database first. However I would like to define other entites in the model that is closer to my business objects. I guess I could write queries in the db and include them in the model. But I would also like to define entirely new entities in the model though they would be based on underlying tables in the db. How do I do that - does anyone know a tutorial?
Regards
Bjørn
db Oldtimer, EF Newbie
Database first means that you have existing database and you can either create model by updating from database or manually. You can use wizard to create initial model and modify it manually to define new entities but you must not use update from database any more or some of your changes will be deleted. Also your custom modifications must follow EF mapping rules (for example it is not directly possible to map multiple entities to the same table except some more advanced mapping scenarios like splitting and inheritance) and some of them (custom queries) must be done directly in EDMX source (XML) because designer doesn't support them - this requires more complex knowledge of EF mapping and it will be definitely hard for newbie.
You can check specification of that XML. For entities mapped to custom queries you will have to use DefiningQuery element in SSDL part of EDMX.

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

Entity Framework map multiple tables to one entity

I have a database with a table for active orders and one for inactive orders. I would like to model this in Entity Framework as one entity called Orders. I also need a way to determine if an order in this collection is active or not, preferably by having a status property on the entity that is set according to what table it is in. Is there anyway to do this using Entity Framework 1. What about in Entity Framework 4?
You could create a view in your DB and generate the entity from that.
Take a look at the Table Per Concrete Type inheritance.
It is described here in ADO.NET Team Blog.
I think this is what you are looking for: How to: Define a Model with Multiple Entity Sets per Type (Entity Framework)
"The Entity Data Model (EDM) allows an entity type to be included by multiple entity sets within a single entity container or for an entity type to be included in entity sets in multiple entity containers. Defining multiple entity sets per type (MEST) allows users to streamline their code when databases have partitioning or other such scenarios where multiple tables have the same structure."
If I am understanding you correctly both active and inactive orders would share the same properties (for example: both would have a decimal "amount" property) if this is the case then in EF 1, I am pretty certain this is not possible. I think you will have to fall back to Mapping your entities to a POCO Orders object.
A good way to do one entity that shares multiple tables is to use Entity Splitting. MSDN has a very simple tutorial that walks you through the process which is very easy, however, you may need to reshape your data model: http://msdn.microsoft.com/en-us/data/jj715646.aspx

Is it possible to make an association between a Table and View in Entity Framework?

I have 2 databases (sql server 2005) in my system, one for configuration data and the other one for Application Data, but there are some tables that are needed in both databases. We've solved that using Synonyms but the problem is when we map the tables in Entity Framework.
We have a Language table in the config database, used for localization purposes. But in the application we have a table called "Countries", and it has a child table to contain the country's fields translated.
My Entity Framework Context maps tables in the Application database and the only way to map the Languages table from the other database is including a View created in the Application Database. Everything works fine, but when I try to make an Association between the CountryTranslation entity and the Language entity I get this exception:
Problem in Mapping Fragments starting at lines 733, 855: Non-Primary-Key column(s) [pai_codlan] are being mapped in both fragments to different conceptual side properties - data inconsistency is possible because the corresponding conceptual side properties can be independently modified.
Do you know if this is a possible scenario? How do I resolve this?
Thx!
Solved!
All I had to do was delete the column that was mapped as a Entity Property and just keep the Navigational Property..