使用存储库模式获取最后插入的行ID

时间:2013-07-16 05:38:58

标签: asp.net-mvc-3 entity-framework c#-4.0 entity-framework-4.1 repository-pattern

我是这个存储库模式的新手。我在存储库中有以下方法。

public abstract class Repository<T> : IRepository<T> where T : class
    {
        private PHOnlineEntities dataContext;
        private readonly IDbSet<T> dbset;

        protected Repository(IDatabaseFactory databaseFactory)
        {
            DatabaseFactory = databaseFactory;
            dbset = DataContext.Set<T>();
        }

        protected IDatabaseFactory DatabaseFactory
        {
            get;
            private set;
        }

        protected PHOnlineEntities DataContext
        {
            get { return dataContext ?? (dataContext = DatabaseFactory.Get()); }
        }

        public virtual int Add(T entity)
        {
            dbset.Add(entity);
            dataContext.SaveChanges();

            // return id here
        }

        public virtual void Update(T entity)
        {
            dbset.Attach(entity);
            dataContext.Entry(entity).State = EntityState.Modified;
        }


        public virtual void Delete(T entity)
        {
            dbset.Remove(entity);
        }

        public virtual void Delete(Expression<Func<T, bool>> where)
        {
            IEnumerable<T> objects = dbset.Where<T>(where).AsEnumerable();
            foreach (T obj in objects)
                dbset.Remove(obj);
        }

        public virtual T GetById(long id)
        {
            return dbset.Find(id);
        }

        public virtual T GetById(string id)
        {
            return dbset.Find(id);
        }

        public virtual IEnumerable<T> GetAll()
        {
            return dbset.ToList();
        }

        public virtual IEnumerable<T> GetMany(Expression<Func<T, bool>> where)
        {
            return dbset.Where(where).ToList();
        }

        public T Get(Expression<Func<T, bool>> where)
        {
            return dbset.Where(where).FirstOrDefault<T>();
        }

这是我的CustomerRepository类

    public interface ICustomerDetailRepository : IRepository<CustomerDetail>
    {
    }

    /// <summary>
    /// CustomerDetail repository
    /// </summary>
    public class CustomerDetailRepository : Repository<CustomerDetail>, ICustomerDetailRepository
    {
        /// <summary>
        /// 
        /// </summary>
        private PHOnlineEntities _dataContext;

        /// <summary>
        /// 
        /// </summary>
        protected IDatabaseFactory DatabaseFactory
        {
            get;
            private set;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="databaseFactory"></param>
        public CustomerDetailRepository(IDatabaseFactory databaseFactory)
            : base(databaseFactory)
        {
            DatabaseFactory = databaseFactory;
        }

        /// <summary>
        /// 
        /// </summary>
        protected PHOnlineEntities DataContext
        {
            get { return _dataContext ?? (_dataContext = DatabaseFactory.Get()); }
        }
    }

CustomerDetail类包含Model。它包含所有Entity列和ID列。

当我将实体添加到数据库时,我想返回最后插入的行ID。 Id是标识列。有谁可以帮我这个?

2 个答案:

答案 0 :(得分:4)

你必须创建一个这样的界面:

public interface IEntity
{
    public int Id { get; set;}
}

让您的实体实现该接口并更改您的存储库类:

public abstract class Repository<T> : IRepository<T> where T : class, IEntity
{

    (...)

    public virtual int Add(T entity)
    {
        dbset.Add(entity);
        dataContext.SaveChanges();

        // return id here
        return entity.Id;
    }

}

答案 1 :(得分:4)

插入后检查您的CustomerDetail对象,该ID将已填充

public class CustomerDetail
{
    public int Id{ get; set; }
    public string Name{ get; set; }
    public string Address{ get; set; } 
}
var customerDetail = new CustomerDetail { Name = "Bubbles", Address = "1 way, city" }
customerDetailRepository.Add(customerDetail)

Console.WriteLine(customerDetail.Id); // This is the identity value