I classify my entity models as business objects and the dbcontext as the data access layer. Then I typically write a set of wrapper classes (business layer classes) that consume the dbcontext and expose the operations that are available to my frontend. i.e. Retrieve, Search, Add, Update and Delete on the model entities.
If you dont like this approach then you could write a set of converters in your business layer that convert between business entities and data access entities. Since code first entities are of the poco flavor I tend not to do this. I find it pointless to convert from one poco class to another that contain the same properties.
Another route would be to define a set of business layer interfaces that your model entities would inherit. This allows you the freedom to hide properties at the model layer that you do not want exposed in the UI layer. Your bussiness layer wrappers would then return the model entities as bussiness layer interfaces. This would be my approach, if i were not gpoing to use the ef model entities as bussiness objects, as it is simpler than creating a whole set of converters, and this is what interfaces are for.
example
EF model generate Poco class
Code:
name space ModelEntity
{
public partial class Entity1
{
public Property1 {get;set;}
public Property2 { get;set;} // lets say we dont want property 2 expose at the UI layer
}
}
Custom Partial class to match Ef model poco class, typically i store these in a File name like such Entity1.Custom.cs
Code:
namespace ModelEntity
{
public partial class Entity1 : Bussiness.Layer.Interface.Entity1
{
// nothing to code here unless you have custom logic
// this was mearly to inherit the bussiness layer interface
// with out affecting code generation
}
}
Bussiness layer interface and Data service
Code:
namespace Bussiness.Layer.Interface
{
public interface Entity1
{
string Prorperty1 {get; set;} // this will be the only property we expose to UI layer with this interface
}
}
namespace Bussiness.Layer.DataServices
{
public Entity1DataSvc
{
Bussiness.Layer.Interface.Entity1[] Search( SearchCriteria criteria )
{
// code here for db context a fetch operation
ModelEntity.Entity1[] rVal = null;
// we can directly cast ModelEntity to Bussiness layer entity because of inheritance
// we also hide the property2 that we dont want to expose to the UI layer
return rVal != null ? (Bussiness.Layer.Interface.Entity1[])rVal : null;
}
}
}