I work with one application connected with 3 databases same structure just the data inside different.
I used WinForms with entity framework 6 .Net not Core when user login he choose the database.
how I made function to send dbcontext or dbset from chosen database?
for example I have a Site table have same columns and same names after user login need to fill datagridview with data from database that user choose.
I did it but with duplicate the code for every database :(
If the databases are truly identical, then you should only need to use one of three different connectionstrings when initializing the dbcontext. Maybe have the first one as the default one for entity framework for building models etc.
Here is an example of how to set the connectionstring in code:
Entity Framework 6 set connection string in code
Good luck!!
Related
I have a SQL Server with multiple databases.
I'm trying to build a "database browser" with Entity Framework 6.4.4 (in C#, ASP.NET MVC 5) that allows users to access any data stored on a particular SQL Server.
The problem is that we already have a huge amount of databases each containing as many tables and columns and it would really be a pain being forced to generate a model for every database by hand.
Therefore, I thought the entity framework would provide a way to generate a sort of "ServerModel", making it easier to handle multiple databases but also have them packed in a single generated model.
I would use such a model like this:
ServerModel sm = new ServerModel("Sql server name");
//list all databases
foreach(var db in sm.Databases) {} // or maybe sm.ToList() ?
//access specific database model and table
sm.MyDatabase.MyTable.ToList()
I couldn't find anyone trying to achieve this with Entity Framework online.
Is Entity Framework made for this?
Or should I start thinking my own solution?
And... does anyone have any?
When you create a new ASP.NET MVC project with Integrated Identity, on the first run the app will create the necessary tables in the database given in the connection string.
However, this happens once, and only once. When I delete all AspNetXXX tables and table _MigrationHistory, these tables are not re-created anymore. When started, the app throws an exception that the tables are missing. When I created a new app from the template and set its connection string to the same existing database, the new app recreated the tables for me, and the first one resumed operation properly.
My questions are:
How does this project / EF know to create the ASP.NET identity tables only once and where does it store the information that this app has already created its tables?
Is there a way to "reset" this for an existing project and have it re-create the tables, without activating EF Code First Migrations?
Try using the DropCreateDatabaseIfModelChanges database initializer.
It's nicely included in this tutorial: https://learn.microsoft.com/en-us/aspnet/mvc/overview/getting-started/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application (search for "DropCreateDatabaseIfModelChanges" on the page).
However, consider, that you should use that only during the actual development - because if there is some difference between your model and database, whole database will be dropped and created again.
If you want to be sure, that tables are populated with some data, use Seed() method (also in the tutorial).
I am learning Entity Framework to query the database of my company. I have an ASP.NET MVC project and as of now, I have established a connection to the company's principal server database. That has given me access to all the tables and I created a separate class Library containing all the corresponding POCOs(generated automatically).
In the tutorial I was following they say to use "enable-migrations" to have the database updated with the model.
So does that mean that if I were to modify the models in my project, that would have a direct effect on the database? Since I am new to this project I do not want to do anything stupid, like altering the database. For now I just want to query the database and retrieve information, then use that information to show more or less information on a web page.
EDIT: Just as an example, I would like to show a difference between the model generated by EF and what my real table looks like. I have a table Web_Profils that contain and ID, a ProfileName and an Order (int). This DB has no primary keys or foreign keys. If there are relations, they are defined through new tables. But when EF generates all my classes, it adds ICollections, for example in Web_Profils, I have a.o virtual ICollection<Web_User_joint_Profils>Web_User_joint_Profils which is not present in the actual table, it just seems to be the relation that EF has deduced(it is the relation between Users and Profiles present in the table Web_User_joint_Profils). Now, will doing a migration affect my tables just because EF has added these collections in my model?
I've also read that it is possible to deactivate migrations using :
Database.SetInitializer(new ContextInitializerNone<YourDbContext>());
Any thoughts?
If you update your model, you need to add a migration to your project and update your database with that migration.
Unless you do those steps after updating your model, changes will not be reflected in the database.
I have to build an mvc application which connects to two different database.
i need to have a menu say Employees > Which will open a create Employee page which need to be
connected to Employee database (sql)and all the other menus/pages whould point to another database.
Can any one help me with the best approach with Entity Framework 6 Code First using MVC 4.
Just create a model for each database, each one having it's own entities, connection string and so on. In EF 6 you can even use the same context to host multiple database models.
http://olivierhelin.com/blog/entity-framework/entity-framework-6-one-entity-data-model-multiple-databases-2
Using entity framework on multiple databases
My company has 100’s of databases with few thousands of entities including Tables, SP and Views.
I am creating a generic Data layer using Entity Framework 6 for my company’s ad hoc projects such that developers don’t have to bother about the underlying database.
My project contains Empty Data Model (.edmx).
Now based on the user selection I need to set the connection and get the entities at run time. Once these entities are registered, I need to provide a collection of data to the caller.
For example: A developer invokes the method from my class and pass entity name as enum string .say … “Entity.Orders”
Here.. my code knows that “Orders” belong to Database named “Sales” which is on “SalesBox” server.
My code now sets up the connection to “Sales” database, get the rows from “Order” table, create a List collection (or any appropriate) and return the collection to caller.
Is this all possible using EF 6? If yes How?