Entity Framework 2.0 is out for some time. If you don’t know Entity Framework 2.0 is a lightweight, extensible and Cross-Platform Object-relational mapper from Microsoft. It is a new version of Entity Framework which is now cross-platform and can now run now operating system like Linux, Mac etc.With Entity Framework Core 2.0, There are tons of ways to create a table and we are going to see one of that. In this blog post, We are going to see how we are going to see how we can create a field for private fields in the table.

So what we are waiting for. Let’s get started.

Flexible Object Mapping in Entity Framework Core 2.0:

So in this example, We are going to create a Console Application like following.

Once we are done with Creating Application We are going to insert NuGet Package for EF Core like below.

You can also install it via running following command.

Install-Package Microsoft.EntityFrameworkCore -Version 2.0.2

Here we are going to install SQL Server Express as a database so we need to install Nuget Package for that also.

Here in the above code, You have seen that I have created few fields for Student Model. If you see it carefully, You can see that there are two private fields _standard and _division. And there are two methods for each field to get and set private variables.

Here in the above code, If you see I have created a dataset for our student model that is a standard way to create a table for the map. There is another method OnCofiguring for giving database connection string. There are is another method called OnModelCreating which Entity framework core uses to create tables. Here If you see that I have written code to map private fields to Table Fields so that two fields Division and Standard will be created with tables.

Now let’s run the migration to create a table like following.

You need to go to the Nuget Package Manager Console and then run the following command.

Add Migration Initial Create

Once you are done with it. It will create the tables in the database like following.

Now let’s write some code insert data in the table. So following is code for the same.

Here you can see that in the above code, I have created Student called Joe Tailor and then I have saved it in the database and then I have written the code for fetching the student data and print it via for loop.

When you run this application It will show like following.

That’s it. Here you can see that It’s very easy to Manipulate the Columns with Entity Framework core 2.0. Hope you like it.

In this blog post, We are going to learn about Entity Framework feature. Explicit loading means that related data is explicitly loaded from the database at a later time. As you might know, that lazy loading is still not possible with Entity Framework core but there is a way to explicit load related data in a transparent manner. We are going explore how we can load data explicitly with entity framework in this blog post in detail.

How to do Explicit loading in Entity Framework Core?

To demonstrate how we can use explicit loading in Entity Framework core. We are going to create a console application with two entities Student and Department. A department can have multiple students. Here we are going to see how we can load students for each department explicitly.

So let’s create a console application like following.

Now once you click “Ok” it will create a console application. Now let’s add nuget package for entity framework core in console application like following. You need to run following command in Package Manager Console.

Install-Package Microsoft.EntityFrameworkCore.SqlServer

Now it’s time to create our models for Student and Department like below.

Here in the above code, you can see I have created student context object and then I have got all the databases. After that, I have checked that whether each department is having students or not. In the next statement, I have loaded the students explicitly with the student with collection load method and then again I checking whether it got students and also printing count of a student.

Now let’s run this application and here is the output as expected.

So it is loading student data after the department is loaded with the help of explicitly. That’s it. Hope you like it. There are so many scenarios where this explicit loading can be quite useful. Stay tuned for the more!!.

Entity framework core is the lightweight, extensible and cross-platform version of Entity Framework. Before some time, Microsoft has Released a new version of Entity Framework RC2. I have written a couple of blog post about Entity framework code first migration earlier for Entity framework 6.0. So there was a couple of request coming for writing a blog post about Entity Framework Core RC2 migration. So I thought it will be a good idea to give an overview how database migration works in Entity Framework Core RC2. This post will cover a basic scenario where we are going to create the database with existing ASP.NET Identity migration and then we are going to create a new model and have that migration applied in the database.

How to use Entity Framework Migrations:

Once we select asp.net core application it will appear the following dialog.

Now when you create a sample application. It will basically create a boilerplate code for the asp.net identity and as a part of that it is going to create entity framework migration files under Data –> Migrations folder.

Here you can find that sample code in GitHub repository given at the bottom. Now we already asp.net identity migration code ready. So Let’s have those migrations applied with the following command from NuGet package manager console.

This post may be pretty basic for many people. But I have been getting this request over and over so I thought it will be a good idea to write a blog post about it.

In this blog post, We are going to learn how we can create the cascading dropdown with ASP.NET Web forms and Entity framework. To create an application first we need to create web application like following.

After creating a web application like following. It’s time to create model classes for our application. Here to illustrate the example, We are going to use Standard and Student models. Multiple students can be there in a standard. So we are going to have two dropdowns Standard and Student. Once you change select standard based on that student dropdown will be filled.

Here I have created two DbSet for students and standards. Also in model creating event I have removed plural names so when entity framework create tables when we run this application it will remove plural names from the table.

Creating Web Forms and using Entity framework for cascading dropdowns:

Now we have our database layer or operation ready. it’s time to create a new Web Form which will have Standard and Student drop-down.

After creating Web Form I have written following HTML code in aspx file.

You can see that I have created two ASP.NET dropdowns Standard and Student and Also for Standard dropdown I have autopostback=”true” and selected index change event.
On the aspx.cs file i have written the following code.

Here You can see that I have created two method BindStudent and BindStarnds. In Bindstandards it fetches all the standards and bind it to Standard dropdown . In BindStudents method, I have used a nullable parameter standardId which will be null by default. So If standard id is not provided as you can see it will only insert default item. Now in page load method, I have called this two methods. Also, you can see that I have written Standard event dropdown selected index change event which will pass standard value.

Now everything is ready. I have inserted following data in Standard and Student table.

When you use cloud services to deploy your application and database it might be possible that transient connection will be a problem between database server and web servers. When you use on premise servers that uses the database server and web server on same data centre. When you use cloud there will be huge infrastructures and you never know where it is deployed even if it deployed on same data centre there will more connection like network load balancers etc. When you use cloud services that will be shared by lots of users which means its responsiveness can be affected by them. And your access to the database might be subject to throttling. Throttling means the database service throws exceptions when you try to access it more frequently than is allowed in your Service Level Agreement (SLA).

So in cloud service there will be transient problems which will be resolved in short period of time. So when you got such kind of errors then you can wait for some time and then you have retry. For that kind of operation Entity Framework provides connection resiliency feature.

The connection resiliency features must be configured for proper database services. It has to know which exceptions are likely to be transient problem and which exceptions are caused by our code. It has to wait for an appropriate amount of time between retries of failed operation. Also it has to try number of times before giving up.

I have been heavily learning Domain Driven Design recently and In that there is a concept called “Anemic Domain Model” which we used to use in our project. In simple terms Anemic Domain Models are those classes that do not have any behaviour with them and most of created with properties. You can use them as container for your data from database. While Domain driven design is all about behaviours. So we need to make our models incorporate behaviours also which is called “Rich Domain models”.

One of step to convert your Anemic Domain Models to Rich Domain Models is to create parameterised constructors. For example an Employee must have FirstName and LastName. So instead of doing validation at the insertion time on UI we should not allow Employee to be created without FirstName and Lastname. The only way to do this to make this properties setters private and assign value of those properties via parameters constructors like below.

Now, If you want to do DDD with any Object Relational Mappers there will be a problem as most of Object Relational Mappers create properties with public setters. Entity Framework is such a Object Relational mapper and I love to work with Entity Framework.

In .NET world, We all are using Enums in our code. It’s makes our code more readable then using hardcoded numbers of strings. Entity Framework 5.0 or higher version has support for Enums. In this blog post we are going to see how we can use Enums with entity framework.

We are going to create a console application to see how it’s works. I’m going to create a console application for the same.

Now days, Entity framework is one the most used ORM in .NET world. Still I can see lots of people confused with IQueryable and IEnumerable. If they dont' know what is difference between them then they are going to make mistakes .When you first look at it both looks same. But there are quite differences and If you don’t understands internals of it then it will impact your query performance also.

IEnumerable vs IQueryable:

Here is the basic difference between IEnumerable and IQueryable. When you write queries with IEnumerable it executes queries on Server and load data in memory and then filter data on client side. This will be a problem when you are fetching large amount of data. While IQueryable executes queries on server side with all filters and then load all data into memory.

Example:

Sound complex!. Let’s write a simple example to understand it better. I am going to create a simple table in database called student table.