Entity split in two databases - entity-framework-4

I'm working on a project already started by several developers before me. One thing in particular bothers me is that they have single entity split in two databases.
Entity is called Tracker.
First database is called ConfigBase, and it has table named Trackers that has TrackerId along with it's attributes.
Second database is called StoreBase, and it also has table named Trackers, whose elements have matching TrackerId as it is in the first base.
Moreover, to have things even more complicated, when you access specific tracker in ConfigBase, you gain SQL server name and credentials that allow you to access it in StoreBase.
Now all this isn't too much complicated if you use plain old ADO.NET. But as my task is to raise entire solution to newest EF 4.3.1, I'm having troubles maintaining consistency of my entity. Half of things related to Tracker entity are in ConfigBase and the other half in StoreBase, and usually I have to get both to get some result.
Is there any solution to this that does not involve virtual merge on database level. I'm looking for a solution that can be done with Code First modelling.
Thanks in advance!

No there is no solution provided out of the box because EF itself is even not able to use more than one database per context. So you will either merge your databases or you will access each database separately (with separate Tracker entity per database) and merge data somehow in your application.

Related

EF 6 DB First Audit Logs

I have a EF 6 DB first MVC 5 application. My requirement is to do audit logging of every operation (including read). I went through many posts and have few queries:
Should audit logging be done at EF level (by overriding SaveChanges) or DB level (by using triggers). Which is the recommended way.
I want to log one row per entity change instead of per property change. What am I thinking is to make a valid XML schema but then each entity will have different schema depending on the column. Any other inputs on how to achieve this
I want log for read operation too
Last thing is, client wants to maintain checksum value per row using SHA3 or MD5.
Considering above points, what is the suggested approach. I could really use some pointers.
To achieve this, I did not use any utility as my requirements were bit different. Finally I went ahead with overriding the SaveChanges method of DbContext used by EF. Also used Newtonsoft JSON library to convert whole updated object to JSON and save it.
To get complete code, check this link - How to audit MVC app which used EF DB first approach

Entity Framework Code First - Relationship between multiple databases

I would like to know if it is possible to map relationship between two different databases using Code First. For example i want to deploy different websites with the same database structure(same system). But i want one Master Database to have tables that all systems will share access.
Example:
Table Clients in Master Database;
Table ClientContacs in individual specific sytem's database;
Is it possible to map using Code First and multiple DbContext? If it is not, i really would appreciate suggestions on how to achiev that!
Thanks!
Yes, it's possible, but not very convenient.
Here's a description of doing it with EDMX Files:
http://rachel53461.wordpress.com/2011/05/22/tricking-ef-to-span-multiple-databases/
It's a bit easier with code first, since you need only reference the table names. However, you can't get EF to create this automatically. You have to create the synonym first and then treat the synonym as a normal table.
Obviously, this only works in a database that supports Synonyms... SQL Server 2008+ should be fine.
But, this is really treating it as a single database with "links" to the other database as tables... you can't treat it as two separate databases explicitly.
I believe it's possible to have multiple contexts for a single database, but not multiple databases for a single context.

Entity Framework - consider only records that meet certain criteria

I'm working on a project (web application - ASP.NET MVC) that uses one database. Until now said database kept records for one client, but now I need to convert it to multi-client database. The idea is that we add a column (eg. CompanyId) to each Table in DB and in queries consider only records that have specific CompanyId.
Unfortunately, the project is quite developed right now and adding extra parameter to every service call, every Linq query etc would be really troublesome. I know that it should be approached this way (multi-client) from the beginning and now Im paying for it.
So I have two options:
Single database - the problem is how to tell EF to consider only records with CompanyId = X and not to write Where(i => i.CompanyId == X) in every single Linq query.
One DB = one client - creating one db for each client seems to be pretty good solution, but in our project user can have access to several databases (client can be associated with many companies and is asked to choose in context of which company he wants to work with and he can change it freely). The pronblem is I dont really know how to change connection string at runtime and I dont know if this solution is ok.
Could you please tell me which option is better and how to approach it?
Thank you in advance.
why don't you have separate but identical schemas for each company. You can then inject the right db context objects based on the company selected. In this way, you can avoid major db redactoring and also avoid the condition on company id.

How to add a table to the EF4 Context dynamically in code - No Code First

We run a series of reports every 6 months and store the results to tables that can be queried/viewed at any time in the future. Depending on the cycle either two or four tables will be added. They have a standard naming convention of yyyy_mmm_Table_x.
Our website is built using ASP.Net MVC2 and the database is modeled using EF4 using the standard model designer, not Code First.
I would like to be able to dynamically add the report tables to the EF4 context at runtime. I don't want to have to manually add them to the model using the designer, otherwise every reporting cycle we have to update and recompile the model just because we added the extra reports. That would be a maintenance headache when nothing else has changed.
I can get a list of the available tables simply by querying sysobjects. If I could get this list and add the tables to the context when the site started up then I could use something like the Dynamic LINQ library to query against them depending on which table the user selected from a dropdown.
I can't use EF4's Code First out of the box because that would force me to create concrete classes for the tables and that would just be the same maintenance headache. I suspect I could use the same strategies the Code First framework uses to dynamically update the context, but I haven't looked at this library at all and I'm hoping someone familiar with it can point me in the right direction.
Otherwise I think I would have to drop back to ADO.Net to handle this area. That may be the best and simple way so I guess I'm looking for comments. I'm not a zealot so I don't need everything to be in LINQ and EF4. :) But it would seem to be a little cleaner and consistent, especially if it allows me to make use of Dynamic LINQ. But sometimes the old way is just simpler.
So, if you have any suggestions or comments I would love to hear them.
Thanks!
Even with common EF you still need new data type for each table because when you map the table you need new ObjectSet of new entity type to be able to run queries. As I know it is not possible to map two tables to the same entity even if table structure is absolutely same.
All runtime mapping is stored in MetadataWorkspace prepared by EntityConnection. So if you want to play with it you can start there but public interfaces of these classes don't look promising.
I guess you want to run Linq-to-entities on these tables so using Stored procedure returning data from correct table based on data parameter is probably not an option.
You should use common ADO.NET for this.

EF4 cross database relationships

I was wondering if EF4 support cross-databse relationships? For instance:
db1
Author
Id
Name
db2
Posts
Id
Content
db1.Author.Id
What ideally I need to do to get this relation in my ef4 model?
Do you guys have any idea?
Thanks
I've found this entry in Microsoft Connect that answers the question about the support given at this moment by EF (actually it is not supported yet).
Also found a thread in Social MSDN about this concern.
Other links on Stack Overflow:
ADO.Net Entity Framework across multiple databases
Entity framework 4 and multiple database
In summary, the only given alternatives are:
Using views in EF
Use NHibernate instead
If your database supports Synonyms, you can trick EF to span multiple databases. I wrote up how to do it here.
Basically you end up with an edmx file per database, and a script which merges them into a single edmx file. Synonyms are used to reference one database from another by using the actual table name, so EF doesn't throw a fit when you try to access database2.table from database1. You still have to setup links between the two databases manually in the EF model, but once setup they'll stay even if you re-run the merge script.
Scripts to setup Synonyms and to merge the edmx files are posted in the link
I recently began a project that uses entity framework with two databases, one Oracle and one SQL Server. I could not find any information regarding cross-database or multiple database support in the entity framework.
Most posts from the MS Entity framework team are a couple of years old and indicate that including two databases in a single model is not a feature that will be included soon. I would be interested in having a concrete answer on whether it was included in 2010 myself although I suspect the answer is no.
Currently out project gets around this limitation by having a separate entity model for each database. This has solved the problem for the majority of the scenarios we've encountered thus far in the project.
In cases where we've needed to query the data from the two databases at the same time, we simply created a view in one or the other databases. Since we're using Oracle and SQL Server, this view would utilize either a Linked Server (SQL) or a DBLink (Oracle).
The disadvantage of views in the entity framework is we've had to spent more time than I expected getting the primary keys working.
Hope this helps.

Resources