Wriju's BLOG

.NET, Cloud and everything

Entity Framework CodeFirst : Simple Migrations

When I change my model from let’s say //Employee Entity public class Employee { public int Id { get; set; } public string FullName { get; set; } } to //Employee Entity public class Employee { public int Id { get; set; } public string FullName { get; set; } public string Email { get;… Read more

Entity Framework CodeFirst Calling Find does Top 2

While using ADO.NET Entity Framework’s CodeFirst and we call Find() method against the PrimaryKey field we get one record. But surprisingly if we dig deeper we will be able to see that it is calling Top 2 which means actually getting two rows. Why is it so? Because just to track if there are more… Read more

Channel 9 Video on Entity Framework 4.1

Published the video on ADO.NET Entity Framework 4.1 Code First Development video. This covers quickly the basic features if this amazing product. Check it out at https://channel9.msdn.com/posts/EF41CodeFirst The code used for the demo is using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.Entity; using System.ComponentModel.DataAnnotations; namespace EF41CodeFirst_BDotnet { //Dept Class public class Dept {… Read more

Code First EF 4.1 : Table per Hierarchy

Concept of Table Per Hierarchy (TPH) is to implement polymorphism and to de-normalize the relational database via inheritance. The entire hierarchy is mapped to a single database table holding all the properties of all the classes. The database then would create a column called “[Discriminator]” to hold the specific type. In Code First this is… Read more

Using ADO.NET EF 4.1 Code First with existing Database

Most of the time questions comes to our mind about EF 4.1 Code First whether or not can we use it with already available database? Simple answer is YES. I have very few personal view on it. I have seen in most of my application development scenario, database gets designed and created before and then… Read more

Code First EF 4.1 : Querying Many to Many Relationship

In continuation to my previous post on how to create one to many, let’s see how can we query it. We will use the same code base and query it Option 1 When we want to query it normally like below var emps = ctx.Emps .Where(e => e.EmpId == 1) .SelectMany(e => e.Projects, (em, proj)… Read more

Code First EF 4.1 : Building Many to Many Relationship

Since we do not have any designer question might arise how can we create Many to Many relationship in Code First 4.1. Here it is public class Emp { public Emp() { Projects = new HashSet<Project>(); } public int EmpId { get; set; } public string EmpName { get; set; } public ICollection<Project> Projects {… Read more

CodeFirst EF 4.1 : Changing Database Table and Column name

At times we need to control the Table and Column name of our generated database or have different Entity/Property name of code than actual database in EF 4.1 Code First. There are two ways we can do it. Using Annotations using System.ComponentModel.DataAnnotations; //Changing database Table name to Employee [Table("Employee")] public class Emp { //Changing database… Read more

Code First EF 4.1 : Missing “ADO.NET DbContext Generator” VS Template

I have seen people coming back to me asking questions about the template visible in Visual Studio 2010 as “ADO.NET DbContext Generator”. This template allows you to create the POCO classes and the context for CodeFirst from already existing edmx file. The template in Visual Studio looks like But missing. But why? At times if… Read more

Code First 4.1 : Using Stored Procedure to Insert Data

Code First in Entity Framework does not support Stored Procedure by default. As there is no designer we cannot even map our stored procs to the entity. There are a many scenario we have seen where we are bound to use stored procedure for any database modifications (insert/update/delete). Here is how we can use stored… Read more

EF 4.1 Code First Validation

When we either use the Fluent API or the attribute to put restrictions to our properties and while adding if we violet that, error would occur. Code First provides us set of classes to capture them Code First Model public class Department {     [Key]     public int DeptId { get; set; }     public… Read more

ADO.NET Entity Framework Code First 4.1 – Online

April 20, 2011 at 11 AM Pacific Time. Please register at https://training.partner.microsoft.com/learning/app/management/LMS_ActDetails.aspx?UserMode=0&ActivityId=731858 Learn what is new in ADO.NET Entity Framework Code First 4.1 ADO.NET Entity Framework comes with a new update to make things simpler while accessing data. ·         Background – no edmx, no visual model ·         Assemblies ·         Create and Load Database ·         Data… Read more

EF 4.1 Code First – Running SQL Queries

ADO.NET Entity Framework’s Code First 4.1 allows us to execute SQL queries directly. You may need to get one column output or quickly update a table. One thing we need to be careful about running these raw queries is that you need to be double sure about the validity of these… If you are viewing… Read more

EF Code First 4.1 Lazy Loading

EF Code First supports Lazy Loading as Model (edmx). You need to define the navigation properties to set the relationship to load the query. So if we try to implement the below code 1: public class Department 2: { 3: [Key] //To make it Primary Key and Identity 4: public int DeptId { get; set;… Read more

ADO.NET Entity Framework Code First Development

I liked LINQ to SQL for its clean approach of defining only code to be able to access the database. ADO.NET Entity Framework comes with a much more richer experience of you being the fine grained control over the code. In this current version we have Model First and Database First approaches, now we have… Read more