在实体框架中为实体创建基类 [英] Creating base class for Entities in Entity Framework
本文介绍了在实体框架中为实体创建基类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想创建一个基本类,该类对于我的所有实体都是通用的。该类将具有Save(),Delete(),GetByID()之类的方法以及一些其他基本功能和属性。我对Linq to SQL有更多的经验,希望能得到一些类似EF中类似示例的好例子。谢谢。
I would like to create a base class that is somewhat generic for all of my entities. The class would have methods like Save(), Delete(), GetByID() and some other basic functionality and properties. I have more experience with Linq to SQL and was hoping to get some good examples for something similar in the EF. Thank you.
推荐答案
像这样:
public abstract class BaseObject<T>
{
public void Delete(T entity)
{
db.DeleteObject(entity);
db.SaveChanges();
}
public void Update(T entity)
{
db.AcceptAllChanges();
db.SaveChanges();
}
}
public interface IBaseRepository<T>
{
void Add(T entity);
T GetById(int id);
IQueryable<T> GetAll();
}
这篇关于在实体框架中为实体创建基类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文