-2

我實施的工作模式我的MVC項目庫和單位第一時間拿到並通過閱讀這篇文章,插入數據庫記錄:如何獲取標識插入實體在實施庫和工作單元模式?

Implementing the Repository and Unit of Work Patterns in an ASP.NET MVC Application

這就是很好的辦法,一切都只是獲得的記錄ID做工精細插入到數據庫中。 當插入記錄時,我無法在數據庫中獲得身份標識。 任何人都可以幫我解決這個問題嗎?

** --- UpDate --- **

OK!這是代碼 - 工作單位:

enter code here 




public class UnitOfWork : IDisposable 
{ 
    private SchoolContext context = new SchoolContext(); 
    private GenericRepository<Department> departmentRepository; 
    private GenericRepository<Course> courseRepository; 

    public GenericRepository<Department> DepartmentRepository 
    { 
     get 
     { 

      if (this.departmentRepository == null) 
      { 
       this.departmentRepository = new GenericRepository<Department>(context); 
      } 
      return departmentRepository; 
     } 
    } 

    public GenericRepository<Course> CourseRepository 
    { 
     get 
     { 

      if (this.courseRepository == null) 
      { 
       this.courseRepository = new GenericRepository<Course>(context); 
      } 
      return courseRepository; 
     } 
    } 

    public void Save() 
    { 
     context.SaveChanges(); 
    } 

    private bool disposed = false; 

    protected virtual void Dispose(bool disposing) 
    { 
     if (!this.disposed) 
     { 
      if (disposing) 
      { 
       context.Dispose(); 
      } 
     } 
     this.disposed = true; 
    } 

    public void Dispose() 
    { 
     Dispose(true); 
     GC.SuppressFinalize(this); 
    } 
} 

,這是DAL層:

public class GenericRepository<TEntity> where TEntity : class 
{ 
    internal SchoolContext context; 
    internal DbSet<TEntity> dbSet; 

    public GenericRepository(SchoolContext context) 
    { 
     this.context = context; 
     this.dbSet = context.Set<TEntity>(); 
    } 

    public virtual TEntity GetByID(object id) 
    { 
     return dbSet.Find(id); 
    } 

    public virtual void Insert(TEntity entity) 
    { 
     dbSet.Add(entity); 
    } 

    public virtual void Delete(object id) 
    { 
     TEntity entityToDelete = dbSet.Find(id); 
     Delete(entityToDelete); 
    } 

    public virtual void Delete(TEntity entityToDelete) 
    { 
     if (context.Entry(entityToDelete).State == EntityState.Detached) 
     { 
      dbSet.Attach(entityToDelete); 
     } 
     dbSet.Remove(entityToDelete); 
    } 

    public virtual void Update(TEntity entityToUpdate) 
    { 
     dbSet.Attach(entityToUpdate); 
     context.Entry(entityToUpdate).State = EntityState.Modified; 
    } 
} 

}

現在我正在尋找一種方式,當在數據庫中插入一行得到返回的ID?

+0

如果在數據庫中有Entity Framework和** identity **列,在您調用'.SaveChanges()'之後,新插入的'ID'值將出現在您的EF實體中 - 就像那樣 - 沒有額外的工作需要 –

+0

@marc_s我知道saveChange後,我們可以得到Id,但在工作單位,我無法得到。 – Mohammad

+1

爲什麼你需要'UoW'類中的'Id'?你的場景是什麼? –

回答

0

我解決了!但如此簡單:(

當我打電話UOW我用這個代碼:

  unitOfWork.AccountAllUsersRepository.Insert(AccountsMappers.ConvertCreateAccountsToAccounts(createAccounts)); 
     unitOfWork.Save(); 
     long CreatedAccountID = accountAllUsers.Id; 

,但是當我用它投給我的主力機型ID還給我,工作得很好:

 AccountAllUsers _AccountAllUsers = (AccountAllUsers)AccountsMappers.ConvertCreateAccountsToAccounts(createAccounts); 
      unitOfWork.AccountAllUsersRepository.Insert(_AccountAllUsers); 
     unitOfWork.Save(); 
     long CreatedAccountID = _AccountAllUsers.Id; 
+0

***頂部UnitOfWork存儲庫不是一個好主意*** http://rob.conery.io/2014/03/04/repositories-and-unitofwork-are-not-a -好主意/ – Kiquenet

相關問題