Entity framework Model to multiple database - asp.net-mvc

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.

Related

Best Approach for shared entities (Tables) among multiple applications

We are working on suite of applications in our company. Each applications have different business logic but share some structures. For example one application is for "IT services" and another one is for "package handling system" among different buildings of the company. We want to create each application with separate asp.net mvc projects (Entity framework Code-First). But the problem is that all of the applications have some similar entities. For example all of them have People, Buildings and Floors Entities in their dbContext. and also have some other tables that have relations to this similar tables
What is the best approach to designing this applications?
Create a single database for all of the applications? what is side-effects?
Create separate Database for each application and duplicate the similar Tables? (currently we working on this but we should write some SQL server jobs to always sync this tables. so I don't think its a good approach)
Create a database for shared tables and another databases for each application. This will cause to lose relations between tables and also produces multi-Context applications (I prefer this but I read that with Code-First EF and linq, it is not possible to query cross multiple contexts)
or something else ?
I am very interesting for Db management system. I like your 3 aproach firstly. All of them different advantages and disadvantages. If you have huge datas on this db may you can use seperate Database like solution like 2. But you have to develop sync mechanism that you said. If we look at another advantge of seperate db that when one db is failed or broken, another db can work :) but some of departmant data can be unavaible until error fixed. This can be good for huge systems.
Lastyly, "Create a database for shared tables and another databases for each application ", this idea is so good :)
May you can use N-Tier artitecture at least 3 tier(Business-Presentation-Data)and
Busines==> Your Worker Functions
Data Layer==> Many Db Context
Presentation ==> MVC project
I think the solution 3 and Lose Coupling Connection with N-Tier Artch is best way.
Lastly may you seach;
Unit Of work, Ninject Freamwork, N-Tier topics
Note: Alwasy the code-first :) I like Code First style to much :D
Have a nice day :)
If you want I can show a few N-Tier structure Lesson Link around "CodeProject.com" and Stackoverflow

How to combine 3 code first MVC projects into one project?

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

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 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.

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.

Resources