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
{
public Dept()
{
EmpDepts = new List<EmpDept>();
}
[Key] //Primary Key column
public int MyId { get; set; }
public string DeptName { get; set; }
public string Location { get; set; }
//For 1 to Many with EmpDept
public virtual ICollection<EmpDept> EmpDepts { get; set; }
}
public class EmpDept
{
public int EmpDeptId { get; set; }
public string EmpName { get; set; }
//For foreign key
public virtual Dept Dept { get; set; }
}
//Context to initialize connection using DbContext as base class
public class HRContext : DbContext
{
public DbSet<Dept> Depts { get; set; }
public DbSet<EmpDept> EmpDepts { get; set; }
}
class Program
{
static void Main(string[] args)
{
//Drop the database if model changes
Database.SetInitializer<HRContext>(new DropCreateDatabaseIfModelChanges<HRContext>());
//Adding values to the database
using (var ctx = new HRContext())
{
var d1 = new Dept() { DeptName = "Programming" };
var d2 = new Dept() { DeptName = "IT" };
new List<EmpDept>
{
new EmpDept(){EmpName = "Wriju", Dept = d1},
new EmpDept(){EmpName = "Saswati", Dept = d1},
new EmpDept(){EmpName = "Writam", Dept = d2},
new EmpDept(){EmpName = "Wrishika", Dept = d2},
new EmpDept(){EmpName = "Sreemoyee", Dept = d1},
new EmpDept(){EmpName = "Bingo", Dept = d1}
}.ForEach(e => ctx.EmpDepts.Add(e));
ctx.SaveChanges();
//Selecting data using Lazy Loading
var q = from d in ctx.Depts
select d;
foreach (var dep in q)
{
Console.WriteLine(dep.DeptName);
Console.WriteLine("+++++++++++++++++++++++++");
foreach (var e1 in dep.EmpDepts)
{
Console.WriteLine(e1.EmpName);
}
Console.WriteLine();
}
}
}
}
}
Namoskar!!!