在DB第一种方法中为Entity Framework 5中的所有实体创建基类 [英] Creating a Base Class for all entities in Entity Framework 5 in DB first approach
问题描述
如果有人可以提出实现这个想法,这将是非常有帮助的。
存储库代码如下所示:
public class GeneralRepository< T> :IRepository< T>其中T:class
{
private ObjectSet< T> _组;
private ObjectContext _context;
public GeneralRepository(ObjectContext context)
{
if(context == null)throw new ArgumentNullException(context);
_context = context; //设置上下文
_set = context.CreateObjectSet< T>(); //返回对象集
}
#region覆盖使用ObjectGraphs的方法。
///< summary>
///将实体中的数据插入到表中。
///< / summary>
///< param name =entity>< / param>
public virtual void Insert(T entity)
{
if(entity == null)throw new ArgumentNullException(entity);
_set.AddObject(entity);
}
///< summary>
///从表中删除实体。
///< / summary>
///< param name =entity>< / param>
public virtual void Delete(T entity)
{
if(entity == null)throw new ArgumentNullException(entity);
_set.Attach(entity);
_set.DeleteObject(entity);
}
///< summary>
///将Entity更新到表
///< / summary>
///< param name =entity>< / param>
public virtual void Update(T entity)
{
if(entity == null)throw new ArgumentNullException(entity);
_set.Attach(entity);
_context.ObjectStateManager.ChangeObjectState(entity,EntityState.Modified);
}
///< summary>
///获取整个表内容
///< / summary>
///< returns>< / returns>
public IQueryable< T> GetAll()
{
return _set;
}
}
如果你先做数据库,你可以随时编辑T4模板来做你想要的。在您的解决方案资源管理器中,展开MyEntities.edmx文件并找到MyEntities.tt文件并打开该文件。
在第307行,您应该具有以下方法: p>
public string EntityClassOpening(EntityType entity)
{
return string.Format(
CultureInfo.InvariantCulture ,
{0} {1}部分类{2} {3},
Accessibility.ForType(entity),
_code.SpaceAfter(_code.AbstractOption(entity)),
_code.Escape(entity),
_code.StringBefore(:,_typeMapper.GetTypeName(entity.BaseType)));
}
您将要更改为:
public string EntityClassOpening(EntityType entity)
{
return string.Format(
CultureInfo.InvariantCulture,
{0} {1}部分类{2} {3},
Accessibility.ForType(entity),
_code.SpaceAfter(_code.AbstractOption(entity)),
_code.Escape (entity),
_code.StringBefore(:,_typeMapper.GetTypeName(entity.BaseType)??MyBaseClass));
}
您可以看到,如果没有提供BaseType(可以在GUI,但这是让一个Entity从另一个实体继承),那么我们希望实体从MyBaseClass继承。
现在保存它将重新生成所有您的实体,他们将继承自MyBaseClass。简单的。
I have few properties like CreatedDate, ModifiedDate, VersionNo on every table. I need to change/ add values for these properties every time I modify the entity. I thought I could create a Base class with these Properties and let the Entities derive from this Base class and during SavingChanges based on ObjectState I could change the Values and Save, that way my audit entries will be isolated from the entity and the thing will be abstracted. But since I am new to Entity Framework , I am finding it difficult to understand how will I handle the mappings, etc..
If anyone can suggest ideas for implementing this, that would be really helpful. Repository code is like below:
public class GeneralRepository<T> : IRepository<T> where T : class
{
private ObjectSet<T> _set;
private ObjectContext _context;
public GeneralRepository(ObjectContext context)
{
if (context == null) throw new ArgumentNullException("context");
_context = context; // sets the context
_set = context.CreateObjectSet<T>(); // returns the Object Set
}
#region Methods to override to work with ObjectGraphs .
/// <summary>
/// To insert data from entity into a table.
/// </summary>
/// <param name="entity"></param>
public virtual void Insert(T entity)
{
if (entity == null) throw new ArgumentNullException("entity");
_set.AddObject(entity);
}
/// <summary>
/// To delete entity from a table.
/// </summary>
/// <param name="entity"></param>
public virtual void Delete(T entity)
{
if (entity == null) throw new ArgumentNullException("entity");
_set.Attach(entity);
_set.DeleteObject(entity);
}
/// <summary>
/// To update Entity into the table
/// </summary>
/// <param name="entity"></param>
public virtual void Update(T entity)
{
if (entity == null) throw new ArgumentNullException("entity");
_set.Attach(entity);
_context.ObjectStateManager.ChangeObjectState(entity, EntityState.Modified);
}
/// <summary>
/// To get th entire table contents
/// </summary>
/// <returns></returns>
public IQueryable<T> GetAll()
{
return _set;
}
}
If you are doing database first you can always edit the T4 template to do what you want. In your Solution Explorer, expand the MyEntities.edmx file and find the MyEntities.tt file and open that up.
On line 307 you should have the following method:
public string EntityClassOpening(EntityType entity)
{
return string.Format(
CultureInfo.InvariantCulture,
"{0} {1}partial class {2}{3}",
Accessibility.ForType(entity),
_code.SpaceAfter(_code.AbstractOption(entity)),
_code.Escape(entity),
_code.StringBefore(" : ", _typeMapper.GetTypeName(entity.BaseType)));
}
You will want to change this to:
public string EntityClassOpening(EntityType entity)
{
return string.Format(
CultureInfo.InvariantCulture,
"{0} {1}partial class {2}{3}",
Accessibility.ForType(entity),
_code.SpaceAfter(_code.AbstractOption(entity)),
_code.Escape(entity),
_code.StringBefore(" : ", _typeMapper.GetTypeName(entity.BaseType) ?? "MyBaseClass"));
}
You can see that if there is no BaseType provided (which you can do in the GUI, but that is for having a single Entity inherit from another entity) then we want the entity to inherit from MyBaseClass.
Now when you save this it will regenerate all your entities and they will inherit from MyBaseClass now. Simple as that.
这篇关于在DB第一种方法中为Entity Framework 5中的所有实体创建基类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!