Most effective way to bind data models in WebForms without stored procedures?

104 views Asked by At

I just started working for a new company, and am on the team that builds the support tool for our external product. Currently, the support tool is a mashup of lots of random pieces that tries to solve a variety of different problems through a huge array of different techniques. The main product is transitioning over to functionality models that perform most of the functions for the application, however it uses stored procedures to get and load data to these models.

In the process of writing the new tools, I was looking for a better way of binding the data from the database to the models so that it can be manipulated inside of the application. Every client has their own database, and every time a stored procedure is created, it has to be created across a multitude of different databases to propagate globally. If I can reduce this headache, it'd be massively beneficial.

I was thinking of using DbContext in a way similar to how I used it in my previous projects with MVC, but I'm unsure of how I could best do this. Every client has their own Database with a client ID in the name, and their connection strings are essentially "DATABASE_CLIENTID", so when the support staff selects the appropriate client, it changes the connection string at runtime to get the appropriate data. I simply need to know what method is best for interacting with this data to create our tools.

The company is switching over to a One-Page style site using Rivets, Typescript, Ajex, etc, so something that could best mimic this internally is our goal.

1

There are 1 answers

2
Kane Wang On BEST ANSWER

I think you could easily do it by using latest Entity Framework 6, when you generate the model using database first approach, it generates the partial Entity class and you want to do is to add another file to implement the following for the same partial class. So for example, if your entity model is called TemplateEntities, then create a new file with the following code:

The generated TemplateEntities class is marked as partial.

All you have to do is add another file with another part of the partial class definition that exposes the constructor you want to use:

partial class TemplateEntities
{
  public TemplateEntities( string nameOrConnectionString )
    : base( nameOrConnectionString )
  {
  }
}

Then pass your connection string in to this constructor.

Stored procedure is good for very complex transaction but if it is only for loading data, Entity Framework should work as expected.