Showing posts with label Entity Framework. Show all posts
Showing posts with label Entity Framework. Show all posts

Wednesday, December 2, 2015

Update EDMX from database

In database first approach, you create your database first and then you generate model from database. This creates respective entities in your project.

However any change in database schema, does not reflects automatically in your model. For this you need to update your model. Following are 2 techniques:


  1. Delete and create(not prefered solution): Here you just need to delete entitiy in model browser. And then you just need to right click and click "Generate Model from Database" this regenerate model from database. This is a good solutions for hobby projects or test projects. However this is not recomended.
  2. Update entities from database: Below are the steps to update entities from database:
    1. Right click model(in edmx file).
    2. Click update model from database:
    3. There ate three tabs(Add, Refresh & Delete).
    4. In Refresh tab expend tables > dbo(schema) > and select updated entity.


All done. Now you

Tuesday, October 20, 2015

Difference between Eager Loading, Lazy Loading & Explicit Loading

Eagerly Loading

This is the process to load all the related entities while loading an entity. When we load an entity, we will be loading all the related entities all together. We use Include method to achieve Eager Loading. For example following will load all the Categories + All the Products of those categories:

using (InventoryContext ctx = new InventoryContext())
{
    var cats = ctx.Categories.Include(b => b.Products).ToList();
}

We can load related entities up to n number of levels.

Lazy Loading

Lazy loading is a concept where we delay the loading of the object until the point at which it is needed. Means lazy loading does not allow to load objects unnecessarily objects will be loaded only when they are needed.

Lazy loading created problem with serialization. Serializers access each and every property to serialize. Hence all the related entities will load. This will increase the overhead and will result in a large result set. So it is advised to turn Lazy loading OFF when work with Serialization.

Explicitly Loading

This is the process to load related entities keeping serialization off. For this we need to make explicit call to load related entity.