Using Entity framework features Part 1
About two year ago I wrote many storeprocedures and many string queries and views for data access layer projects. But after some months my life is better: come up Linq to SQL and then advent Entity framework 4.0. We added a .edmx file to project and map all table entities to project and use modelEntities context for using hot LINQ queries. I was very happy when used this technology and then come up many other templates (T4 templates) for change tracking, making POCO classes, … . Entity framework had two concepts: model first and Database first, I guess you known about them, until come up Entity framework 4.1.
So today I want speak about entity framework 4.1/2.
In the EF 4.1/2 we can design a project with code first approach and make a dynamic SQL database.
Why don’t used Entity framework features?
- Without create a database project and writing dummy query and insert data to DB for test we could create a DbContextInitializer class for create dummy objects for creating Database and insert all objects to Database.
- We could separate all entities form each others.In many project we don’t need come all entities populate in one library or I want make modular projects. See my sample project models:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace Arash.Core.Model
{
[Table("EntityBase")]
public abstract class EntityBase
{
// primitive
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int EntityId
{
get;
set;
}
[ForeignKey("EntityType")]
public int EntityTypeId { get; set; }
// navigation
[ForeignKey("EntityTypeId")]
public EntityType EntityType
{
get;
set;
}
public EntityTypes EntityTypes
{
get
{
return (EntityTypes)EntityType.Id;
}
set
{
EntityTypeId = (int)EntityTypes;
}
}
public virtual IEnumerable Tages { get; set; }
}
}
}
in sample project we have a three projects in Domain layer project: Core, Membership, Restaurant. in each project we have a Model as a sub namespace that populate our models. These model folders have many entity classes. This entity classes are same object tables!Really?
After complete all models, we should create a DataAccess project and all model references to it:

There is two concept for develop code first POCO classes in EF 4.1/2: develop a partial class and bind properties to field names in SQL table or put code first attributes top of mapped properties of a POCO class.I use attribute model without add any .edmx file to project
using System.Data.Entity;
using Arash.Core.Model;
using Arash.Membership.Model;
using Arash.Restaurant.Model;
using Paradiso.Infrastructure.Data;
namespace Arash.DataAccess
{
public class ArashDbContext : DbContext, IDbContext
{
public ArashDbContext() : base("arashConnectionString") { }
public DbSet Members { get; set; }
public DbSet Roles { get; set; }
public DbSet Users { get; set; }
public DbSet Jobs { get; set; }
public DbSet EntityBases { get; set; }
public DbSet EntityType { get; set; }
public DbSet Tages { get; set; }
public DbSet TagEntities { get; set; }
public DbSet Coffeeshops { get; set; }
public new IDbSet Set() where T : class
{
return base.Set();
}
public Database Query
{
get { return this.Database; }
}
public IDbSet GetDbSet() where T : class
{
return null;
}
public Database Db
{
get { return this.Database; }
}
}
}
So We making a new structure of model for domain layer and data access layer. We could add a class as data initialize object to Db. For this:
sing System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Validation;
using System.Linq;
using Arash.Core.Model;
using Arash.DataAccess;
namespace Arash.DataAccess
{
public class ArashDbContextInitializer : DropCreateDatabaseAlways
{
protected override void Seed(ArashDbContext context)
{
#region entity type
new List
{
new EntityType {
Name = "Coffeeshop"
},
new EntityType {
Name = "bar"
}
}.ForEach(m => context.EntityType.Add(m));
#endregion
try
{
context.SaveChanges();
}
catch (DbEntityValidationException e)
{
var ex = e.EntityValidationErrors.ToList();
}
base.Seed(context);
}
}
}
Concludion:
We used EF 4.1/2 with code first concept for separating model projects. With these approach we can add a new models without change old assemblies or old domain projects
Please read Part 2 for using these approach for using in Service(Bussiness) layer
Posted on November 15, 2011, in Architecture, C#, Entity framework and tagged code-first, Data-Access-architecture, Entity-framework. Bookmark the permalink. 1 Comment.
-
Guest
