Building Dynamic Connection String at Runtime in Mvc3 DbContext Model class…

I have following codes in my CompanyDbContext.cs class  for my MVC 3 C# project.  I  am building dynamic connection string at runtime for multiple database operations in one application. I am also using the concept of Bounded DbContext by which we can create separate DbContext for group of Entities and keep them clean and grouped.

Advantage of Bounded DbContext:

If you have sub areas in your mvc project which have specific business logic, you can split it into multiple DbContext. (These smaller contexts follow a pattern critical to Domain Driven Design called Bounded Contexts). There are a number of benefits to creating DbContexts that are targeted to these various processes rather than one all-purpose context. As your application grow, it will be much easier to maintain each context as well as locate the logic you need within it. (Better than adding or modifying existing logic in single DbContext with many DbSet properties and fluent configuration for many class)

Performance is another consideration. When Entity Framework creates an in-memory model of the context, the larger the context is the more resources are expended to generate and maintain that in-memory model.

 

Hope this will help! Thanks for reading this post.

– Amit

One thought on “Building Dynamic Connection String at Runtime in Mvc3 DbContext Model class…

  1. I found this approach that You are using very intelligent and elegant. But I don’t understand it’s implementation. Could You elaborate more on this with some more code .What goes to Controllers what goes in DAL how are creating multiple DbContext an how do You use them. Some example project in VS would be excellent

Leave a comment