Is it possible to make an association between a Table and View in Entity Framework? - 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..

Related

Create entities from database views using eclipse and JPA

Can we create entities classes from Database views using JPA Tools the way we do for database tables?
Manuj
You use the same syntax as you would for creating entity classes for a table. The only differences are: 1) typically views are read only, thus your entity will likely only be used as read only, and 2) Like all entities you need to have a primary key, thus your view will need to either have one column that has unique identity values, or a combination of columns that can be used as a unique identity.
The views are also available in the list of tables when you specify the connection and the schema.

Entity Framework model-first design not won't let you edit the table mappings?

If we've been using an Entity Framework 4 model for some time, and we eventually want to switch the underlying database to a different vendor's product (say, from SQL Server to MySQL), is it simple to adjust the table and column mappings in the entity model without needing to update any of the entity class code?
We're trying to design code that is as database agnostic as possible, so I'd like to know in advance how much trouble we're in for if we ever switch our databases around. Ideally, we'd like to not have to touch our applications that use our entity classes. I can't seem to find any way in the entity designer or XML editor to adjust the underlying database column names without it giving me an error.
(I can, however, edit the entity's property names in the designer while leaving the database column names alone, but that's the opposite of what I need.)
Thanks!
EDMX is not database agnostic. SSDL part of EDMX is tightly coupled with database server (in case of MSSQL even with its version). You need separate SSDL for each supported database server.
I don't understand how changing column names relates to database agnostic model. Reverse is true! If you need your database to have different column names for different server products you need separate mapping for each of them!
Changing column names when using model first is possible only if you modify T4 template used for generating database creation SQL script. But every time you create that script designer will delete whole your storage description (SSDL) and mapping (MSL) and replace them with a new one.
The easiest way to have database agnostic code is using code first but even then you can have problems with some type and feature inconsistency among servers.
If you want database agnostic ORM you should probably check NHibernate.

EF model mapping multiple databases

I have a model in my project that maps to a LOT of views in my database, but I need to map to a view in another database.
How can I do this? Do I have to create another model? I don't want to, but I will if I have to.
The same model can't get data from the two different DBs. The easiest way would be to create a view in the same database that calls and returns data from the other database i.e. the abstraction view that internally calls external DB view.
If your database supports synonyms, you could setup a synonym to the other database, and merge the edmx definition in with your 1st database's definition. I wrote how to do it here
Basically you end up with two edmx files, and a script that merges the two into a working edmx file. Synonyms are used to reference one database from the other without needing the full database path.
If you use code first approach in Entity Framework, here is how to map EF entity to the table from other database:
SQL Script that needs to be run in your database to create synonym for the table from other database:
CREATE SYNONYM OtherDatabaseTableSynonym FOR otherdatabase.dbo.otherdatabasetable
Entity Framework Mapping in (Fluent API):
modelBuilder.Entity<OtherDatabaseTableEntity>().ToTable("OtherDatabaseTableSynonym").HasKey(x => x.id);

Entity framework 4 and multiple database

Something changes or it still not support this?
For example join database1.dbo.Users and database2.dbo.Addresses
I actually did find a way to make an EF model span multiple databases if your database supports Synonyms. Basically you setup Synonyms to Database2 on Database1, create separate edmx models for each, then merge the XML
I posted the exact steps to make an edmx file span multiple databases here if you're interested, along with a script to do the merge for you whenever something changes.
I think what ais asked is if you can join tables from different databases, not different providers, resulting in one entity mapped to two or more tables or views from different databases.
If you think about it, when you create a EDM model with Visual Studio it ask you to give an existing database, and when finished creating the model, it generates an EF connection string, that internally address to the given underlying database connection string.
E.g: metadata=res:///EFTestModel.csdl|res:///EFTestModel.ssdl|res:///EFTestModel.msl;provider=System.Data.SqlClient;provider connection string="Data Source=.\;Initial Catalog=EFTest;Integrated Security=True;MultipleActiveResultSets=True"*
So each model matches only a database, only a connection string.
EF4 still does not support creating one conceptual model which works with N storage models. At least this is not supported with any built-in provider. Perhaps in the future this could be done through a new provider that combines the support of many storages (from the same providers or different).
I havent done enough research on it, but perhaps Windows Server AppFabric (Codename Velocity) could be the bridge to go through this gap.
Note: I have tried even editing manually the xml for the EDM (edmx) to insert a second element inside the <edmx:StorageModels> tag but it does not match the EDM XML Schema so VS warns about it:
Error 10021: Duplicated Schema element encountered.
Rafa Ortega
MAP2010
See answer to similar question:
Entity Framework - Inserting entity with multiple models and databases

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.