Is it possible, in a code-first approach, to define two different context classes and use them simultaneously?
The database schema is already given and I cannot modify it (besides the creation of some new tables).
The problem I'm facing is that I'm getting "The model backing... has changed" error. It seems there's some kind of collision between the two contexts.
Note that each context includes a different set of tables, so sync problems aren't a concern.
Assuming you are using EF 6 or higher, this can be done either by the ContextKey-Property of DbMigrationsConfiguration (this will make the MigrationsHistory table multi tenant by ContextKey) or by setting HasDefaultSchema in OnModelCreating (this will enable multiple MigrationsHistory tables, one for each schema). For further instructions see the Documentation.
Related
I am using Oracle and LightSpeed Orm and generating model from database.
It seems there is an issue while creating tables that are both plural and singular.
I am getting an error Error 2 Ambiguous moniker '/Ecom/ClaimType' encountered. It is used for both 'ClaimType' and 'ClaimType'.
In this specific instance it's tables CLAIM_TYPE and CLAIM_TYPES that get same (ClaimType) entity generated.
How do I generate two different entities for these?
This came back from lightspeed support:
You will need to split this into two separate model files. We require
entities to be uniquely named in a single model.
J.
(Which imho is ridiculous solution)
I am very new to entity framework and I am trying to do something like following scenario.
I am creating ASP.net web from application. That website needs to connect to two databases which the schemas are completely different.
I have no idea how the basic structure should be.
I though of have EF on class library. please guide me with instructions since I have less knowledge.
Since you are using two different databases, the only viable option is to create two separate conceptual models. Even if you would be able to merge two different databases into a single conceptual model, it would be a pain to maintain is the databases are of mentionable sizes.
The two models could reside within the same project, in seprate folders to get different namespaces.
E.g.:
Company.MyApp.DataModel
Company.MyApp.DataModel.Model1
Company.MyApp.DataModel.Model2
Then you could put a new layer on top of these two models which do all the heavy lifting, and could even make them look like one database if you want that, or merge data from entities in both models into a DTO or something similar.
Check out the Repository pattern.
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 tag but it does not match the EDM XML Schema so VS warns about it: Error 10021: Duplicated Schema element encountered.
You are going to use model first approach as the database already exists.
You will need to create two edmx for the two database.
You can create one model project which you can have connection strings for both the edmx in the app.config file and then create your edmx.
Is it somehow possible to handle concurrency in Entity Framework without adding an additional column like described in this MSDN-article?
There are only solutions described where i have to change the database structure, but it is not possible to change the Database i am working with, so i need another possibility.
EDIT:
The conflicts should be handled optimistic. There are several (hundreds) of tables, so what i am looking for is a more general solution, not specific to one table.
According to this: Handling Concurrency with the Entity Framework
Configure the Entity Framework to include the original values of every
column in the table in the Where clause of Update and Delete commands
If you do want to implement this approach to concurrency, you have to
mark all non-primary-key properties in the entity you want to track
concurrency for by adding the ConcurrencyCheck attribute to them
I have to create an application that will copy common configuration settings from one database to another database (same schema). I'm wondering if I can use one Entity Model to access both databases. Say it is stores and products that need to be copied. Can I use the same model to get a store from serverA.databaseA and then insert into server.databaseB?
You can have two similar DbSets in two different DbContext classes. As mentioned in comments, the real problem is to deal with identity columns and IMO with context attachments. I think best way is to handle identity columns manually and fetch data from db in a detached state.
I'd like to know how to (if possible) associate tables from different databases using entity framework. I have one edmx for my "xyz" database and another for my "abc" I need to associate them using EF. I know how to fix this using FKs, but that's not what I want, I'd like to be able to do that using the visual editor, not adding, by hand, a FK to my DB.
Here's a pratical example of what I'd like to accomplish:
Table User - database: abc
Table Message - database: xyz
I'd like to associate User to Message and vice-versa.
Could anyone help me?
Thanks!
You can query them together with Linq-to-Objects, but not with L2S or L2E.
A context is wrapped around 1 Db-connection, you cannot track/update entites from multiple db's.
A possible workaround is to 'link' to one of the Db's from the other. MS-SQL can do this. To the EF it would appear 1 database.
There is no way to achieve this using the edmx design surface in EF.
You could encapsulate this association within your domain model and provide a distributed transaction across the two EF Contexts. We've implemented the latter with an EF Context "container" class that given an internal collection of contexts uses a TransactionScope when it contains more than one context (this relies on the IUnitOfWork abstraction to work effectively).