MVPMIX 2016 Toronto Technology Conference

Folks,

Due to popular demand, we are bringing MVP MIX 2016 to Toronto yet again!!

Seats are limited, so reserve your spot now. There will be two simultaneous tracks with sessions ranging from .NET, VS Code, ASP.NET Core, EF Core, JavaScript frameworks, Roslyn, Open Source, Cross-plat Apps with Xamarin and Azure, Document DB, Keno UI, Office 365, NuGet and more!!

We have a stellar line-up speakers who will enthrall you with their technical prowess and virtuosity.

MVPMIX2016Speakers

MVPMIX2016Speakers1See you on October 17th at the Canadian Microsoft’s Toronto Headquarters.

Obi

Posted in Uncategorized | Comments Off on MVPMIX 2016 Toronto Technology Conference

How to use a single database connection with multiple Database Contexts!!

Reading is to the Mind what Bounded Context is to DDD!!

Typically, for small to medium-sized projects, you probably have a single database context or two, but what if you are working on a large project that requires multiple DbContexts!

As you create the so-called ‘bounded’ contexts (a pattern that was coined by Eric Evans in his ground-breaking book on Domain Driven Design), you need to specify a connection string. In other words, tables that are created in the database are represented as entities in the domain irrespective of the methodology used to create the domain.

A domain is made up classes which are wrapped in a single database context. If you have hundreds of tables and a single domain, then, from the maintenance standpoint, this can potentially be less maintainable. It is therefore a good practice to group these classes in multiple domains or multiple bounded-contexts.

The code below shows one of the context classes which inherits from the base DbContext class.

ContextsNotice the three database contexts in Solution Explorer that we have in the project, each of which is pointing to the same database defined separately in App.Config like so:AppConfig

Ideally, what we want is to use a single connection to the database.

In order to do that, let’s create  a new class and call it BaseContext. The BaseContext is defined as a generic class with a constraint where you are required to pass in value of DbContext.

So instead of having multiple DbContext(s) and having an individual connection string for each of them, we’d rather have a single connection string and use it for different DbContexts like so!BaseContext

Note that BaseContext constructor is marked static. This declaration ensures that the Database Initializer setting is set per an application instance of the given constructor.

In order to do that, modify each one of the dbContext classes to inherit from the BaseContext instead of inheriting from DbContext directly like before. Furthermore, in app.config or web.config, remove additional references to the database and just keep the one as shown below:AppConfig1

SingleContext

 

 

Now, when you run the application, EF would produce the same output pointing to a single database albeit multiple contexts through the magic of a generic BaseContext.

If you want to know more about DbContext, I would highly recommend a book by Julie Lerman, titled, Programming Entity Framework: DbContext.

Happy Coding!

Posted in Uncategorized | Comments Off on How to use a single database connection with multiple Database Contexts!!

Mapping Stored Procedure(s) to Entity Framework

You have written a nice stored procedure and you also use Entity Framework periodically. You may have also used LINQ to SQL in your past life, but now you want to use Stored Procedure with LINQ to Entity instead. So what do you do?

Well, in this short but succinct tutorial, we’ll do just that!

First, let’s write a simple stored procedure. You can create one in Visual Studio if you don’t have SQL Express or SQL Server Management Studio.

StoredProc

Let’s fire-up Visual Studio and do the following:

  1. Create a Console app
  2. Add reference to the latest version of EF. As of this writing I am using 6.1.3
  3. Add a new ADO.NET item from the template and select “EF Designer from database” option. See image below:EntityDataModel
  4. In the next step,  you’ll have an opportunity to select the database objects i.e. Tables, View, Stored Procedures. Here, we will select a couple of tables and stored procedures. See image below:
    DatabaseObjects
  5. After you hit Finish, Entity Framework will generate all the necessary goo including an diagram, designer and TT classes.
  6. Open the diagram. As you can, it’s pretty straight forward.Diagram2
  7. Next right-click on the Department Entity and select ‘Table Mapping’ from the context menu.
    Mappings
  8. Next, go to the Program file, new up the context and invoke the stored procedure, in this case ‘GetAllDepartments() and write the information out to a console
    Program

In the future post, we will do a little deep dive and show you how to map complex stored procedures to Entity Framework.

So stay tuned!!!

Posted in Uncategorized | Comments Off on Mapping Stored Procedure(s) to Entity Framework

Using Stored Procedures from LINQ to Entity

Entity Framework 6.0 and above supports the use of Stored Procedures if you choose EF Designer from Database workflow or  Generate from database (depending upon the version of VS.NET) option which essentially allows you to create a model that is reverse engineered from an existing database. After EF finishes reading the database schema, it will display a list of database objects including tables, views and stored procedures. See image below:

EFDesignerFromDB

However, if you choose Code First from Database workflow, the option to import Stored Procedures from database does not exist. This is by design. See image below:

CodeFirstFromDB

Now, what if you use the above workflow to reverse engineer your database to a model and still want to use stored procedures. How would you use it?

There is a SqlQuery method which is part of the Database object, i.e. Database.SqlQuery. This method is used to for creating a raw SQL query to return elements of a given type. The ‘type’ can by any type that has properties that match the names of the columns that are returned from the query.

As with any API that accepts SQL, it is important to parameterize any user input against a SQL injection attack. For that you can use parameter place holders in the SQL query or a Stored Procedure and supply value(s) as additional argument(s) like so:SqlQueryMethod

Call the method and pass a value which in this case happens to be an employeeID of 1.

Main

Output:
Output

So this was a short tutorial on calling a stored procedure leveraging SqlQuery method using LINQ to Entity.

 

Posted in Uncategorized | Comments Off on Using Stored Procedures from LINQ to Entity

Can LINQ to SQL & LINQ to Entity Co-exist? Absolutely!!!


Ever wondered how to combine LINQ to SQL (DBML) with LINQ to Entity (EDMX)?

Well, it’s easier than you think unless you intend to do it differently altogether!

In most cases, you may not be required to work with both of the above technologies. However, if there already is an investment in a LINQ to SQL project and you are about to add a  new module to an existing solution that you want to develop in EF using LINQ to Entity, then this blog post is for you.

What I am about to demonstrate is that inside my Visual Studio solution, I have two projects, namely, L2Entity and LinqToSql respectively. Each of these projects has appropriate references to LINQ to SQL and Entity Framework.

I am using Visual Studio 2013 with the latest version of EF i.e. 6.1.3. So let’s dive in.

Once you have created a solution, add two console application projects and name whatever you want. I’ve named L2Entity to one of them and LinqToSql to the other. In case you are new, you can read up on one of my earlier posts that describes how to create these projects here.

SolutionExplorer_1

Here’s some code in L2Entity project’s where I am retrieving a list of Departments. As you can see; a LINQ method is being used here with some Lambda’s syntactic sugar! Simple enough!!
L2Entity_1

This is the other project i.e. LinqToSQL where I have created two methods. The first method i.e. DemoForLinqToSql() simply queries Departments using LINQ To SQL API by spinning a new context instance of L2SDataContext() and then writing the results out to a console.

The second method i.e. LinqToEntity() is a method created in LINQ to SQL project that retrieves data using LINQ to Entity by instantiating a context from L2Entity project. Make sure you add this reference in LinqToSql project and you should be good!

LinqToSql_3

Output: The first-half of the output window retrieves results using LINQ to SQL and the second half gets results from LINQ to Entity from within LinqToSql project.

Output_4Conclusion: As you saw in this simple yet succinct sample, we could easily use LINQ to SQL and LINQ to Entity from a single project!

Posted in Uncategorized | Comments Off on Can LINQ to SQL & LINQ to Entity Co-exist? Absolutely!!!