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.
Related
I have three different projects which are inter related and now I want to combine all of 'em. All are developed by using MVC.NET code first method.
I am confused how to combine all of them? Options I can think of:
Create three 3 dbcontext files and copy paste it from old one respectively.
Use db first method and merge 3 db to another project.
I am not sure how to do it. I'd appreciate if you can suggest any workable method Is it fine if my application has multiple data context and multiple databases? How'd I join tables from it?
If the data in your application are related to each other, there is no need to use different dbcontexts as the current projects will also be merged. You can continue to use separate entities in the new dbcontext as you used to in the previous contexts. For creating relationships between the tables you might have a look at Configure One-to-Many Relationship and similar ones in Entity Framework Tutorial Pages. After creating the relationships properly you can retrieve data from multiple tables at the same time. In addition to this it would be also better to use ViewModel as explained on What is ViewModel in MAC?. Hope this helps...
Update:
If you want to retrieve data from different database you can try the similar methods to that given below:
SQL Server: Joining Tables from Different Databases on the Same - My Tec Bits
Can we use join for two different database tables?
Querying data by joining two tables in two database on different servers
I have one project,need build more then 300 models, i want use EF codefirst.
But I think saved in one database Seems not so good.
so I want to know how to Save more then 300models to 5 database and use code first?
Do it right?
How to do it?
Have the mature example ?
how to query data by Navigate properties in tow models? They are not in same database,
I want query by lambda int these database like One database (on DbContext).
I am chinase .so English is very Bad.
I hope you can understand what I'm saying
The problem with splitting the models across multiple databases is that you cannot have foreign key relationships between the two databases.
If you are using multiple databases you will need to handle all the navigation yourself in code.
You should consider redesigning the database so that there are less base models and then using application level models to access the required models.
Another option is to use ubermodels keeping all 300 tables and then use application level models. This can be aided by techniques proposed in the article here on shrinking EF models that may help.
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.
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.
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.