How to combine 3 code first MVC projects into one project? - asp.net-mvc

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

Related

Entity framework Model to multiple database

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.

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.

Single or multiple DbContext File in mvc4 using Entity framework 6

i am using entity framework 5 in mvc4 application .i want to ask about multiple DbContexts files in single project. is this a good approach to use multiple dbContext files in single mvc4 project using Entity Framework 5 or should i use only single DbContext File or Multiple please guide me Thank you.
As previously stated it is good practice to have a one DbContext file per database. So if you are running for example, a blog and shop these would no doubt be separate databases therefore separate DbContexts. A project I am currently on uses one database and roughly 20 tables, this has one DbContext. Good luck.
You can use multiple dbcontexts if:
your data is stored in different databases (cause dbcontext working with single database)
you have several independent datamodels in single database. (it is good to separate independent data).

Entity split in two databases

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.

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