【问题标题】:Getting last inserted row id using repository pattern使用存储库模式获取最后插入的行 ID
【发布时间】:2013-07-16 05:38:57
【问题描述】:

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

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 类包含模型。它具有所有实体列以及 ID 列。

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

【问题讨论】:

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


    【解决方案1】:

    插入后检查您的 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
    

    【讨论】:

    • 是的。由于我们使用的是通用存储库类,因此我在 CustomerRepository 中添加了 Override Add 函数。这解决了我的问题。谢谢:)
    【解决方案2】:

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

    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;
        }
    
    }
    

    【讨论】:

    • Marcin,我添加了 CustomerRepository 类。并且 customerDetail 本身包含 ID peoperty。
    • @Marcin,并不意味着您不能使用界面。你可以动态地或者用反射访问 entity.Id 来返回它。
    • 您能否在每次添加时给出不带 .SaveChanges() 的示例??我想在 UnitOfWork 调用 dataContext.SaveChanges() 后获取 ID;
    • 另外,你如何处理交易??
    猜你喜欢
    • 1970-01-01
    • 2018-03-14
    • 2016-03-28
    • 1970-01-01
    • 1970-01-01
    • 2020-09-12
    • 1970-01-01
    • 2013-07-31
    • 2012-07-26
    相关资源
    最近更新 更多