Entity framework : how to generate a SQL Server model - c#

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?

Related

how deal with Dbcontext and Dbset?

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

entity framework dynamic schema load

I'm working on a database schema that involves two static sales agency that have the same database model.
For example:
dbo.User becomes
paris.User and toulouse.User
Our project works with Linq To SQL and we use sqlmetal to generate two map files one for the Paris agency and the other for Toulouse.
With active directory, we are able to detect where the user come from and by that, we choose the map file corresponding to his agency.
My question is:
Is there a way to do the same in Entity framework ?
if not, what can i do ? and what's the best solution that i should follow, while using Entity framework.
You can have multiple dbcontext in entity framework so depending on your agency you can use the different dbcontext.

How do I find the database or table from embedded SQL in C#

I am proficient in SQL but not so much in C#. Recently a web application has broke. I am trying to find the data in the SQL database that is entered through the web application. I am confused about where the SQL table and fields are in var query 1 and 2. See attached image.
This looks like Entity Framework, and it would depend on how the mappings are setup, this is usually done either with an EDMX, or using code first with fluent mappings.
So you'll have to look at your mappings, to see what
_ICL_RESENTS_INCOMEDTLS,
_ICL_RESENTS_PAYMENTs,
CDCLIENTS
entities are mapped to which tables. This mapping should tell you what the underlying tables are.

Generating Class Object from Postgresql Database ServiceStack.Ormlite

We are in the process of developing a brand new application.
We want to use ASP.NET MVC 5 with ServiceStack.Ormlite.
Also want to use Postgresql database to store relational objects / tables.
Question: Is there a way / tool to generate C# Class Objects from the Postgresql Database which the ORM then can use to perform operations on the tables.
E.G. I have a table called "Person" in the Database.
What I want to do is, using some tool (Need to know which tool) to generate the C# class object so I can use servicestack.ormlite to add a new person to the table.
Then if I add a column to that table, I generate a new class to replace the old one.
Is that possible and which tool will allow me to do that?
OrmLite's primarily a code-first ORM but does have T4 Templates to help with initially generating the code-first POCO's for existing database tables.
Although after generating the POCO's, I'd be maintaining them (i.e. code-first) from that point forward.

Combine Class with Database first model

I've got an older ASP project that is being re-written into a responsive MVC4 w/bootstrap web application. The original version used direct SQL queries to interact with the application backend SQL database, and a custom class called EmployeeUserInfo that directly queries the ERP system for current employee information(current tasks, hours worked, etc). That connection is strictly read-only.
Using Entity Framework, it was extremely easy to create the models for the various writable databases that make up the application, but how can I add the original EmployeeUserInfo class to this EDMX model? I'd prefer not to rewrite the class because it does a lot of querying and analyzing to build the EmployeeUserInfo object. Is it possible to combine another class with an EF database-first generated model?
I'm stuck now because I can use the EmployeeUserInfo as the model for a view, but then I cannot access the EF generated model to read/write to the application database. I feel like I'm overlooking something here and making this more difficult than it needs to be.

Categories