MVC添加实体

106 阅读1分钟

///

/// 公共方法封装 /// where 泛型约束,只有继承了BaseEntity的实体才可以传入BaseService中 /// public class BaseService where T:BaseEntity { //怎么释放资源

    private RentHouseContext db;
    //在BaseService中不实例化上下文,将实例化的工作交给实现方
    public BaseService(RentHouseContext db)
    {
        this.db = db;
    }
    /// <summary>
    /// 添加实体
    /// </summary>
    /// <param name="entity"></param>
    /// <returns></returns>
    //编写用户相关的基础操作
    public long Add(T entity)
    {          
            db.Entry<T>(entity).State = EntityState.Added;
            db.SaveChanges();
            return entity.Id;
    }