使用存储库模式时,如何找回数据库生成的ID? [英] How do I get DB generated IDs back when using repository pattern?

查看:86
本文介绍了使用存储库模式时,如何找回数据库生成的ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的解决方案中,我有一个Domain项目和一个DataAccess项目。我的域项目包含用于各种存储库的域对象和接口,以及用于包含我所有存储库的UnitOfWork的接口。我的DataAccess项目包含所有这些接口的实现以及我的Domain对象的EF版本。

In my solution I have a Domain project and a DataAccess project. My Domain project contains domain objects and interfaces for various repositories and an interface for a UnitOfWork which will contain all my repositories. My DataAccess project contains the implementations of all these interfaces and the EF versions of my Domain objects.

因此,如果我有一个名为 Person的类,我想向数据库中添加一个 Person ,它看起来像这样:

So if I have a class called Person and I want to add a Person to the DB it would look like this:

unitOfWork.People.Add(person);
unitOfWork.Complete();

内部 unitOfWork.People.Add(person)我使用AutoMapper将域 Person 对象映射到我的EF Person 对象。然后将EF Person 对象添加到EF DbContext 对象。

Inside unitOfWork.People.Add(person) I map my domain Person object to my EF Person object with AutoMapper. Then I add my EF Person object to the EF DbContext object.

public void Add(Person person) {
  DataAccess.Entities.Person dbPerson = Mapper.Map<DataAccess.Entities.Person>(person);
  dbContext.People.Add(dbPerson);
}

Complete()函数仅包装SaveChanges()。

The Complete() function just wraps SaveChanges().

public int Complete() {
  dbContext.SaveChanges();
}

通常,EF在插入后会更新插入对象的数据库生成的ID。但是在这种情况下,EF对我的Domain Person对象一无所知。

Normally EF updates an inserted object's DB generated ID after insertion. But in this case EF knows nothing about my Domain Person object.

由于我的Domain Person对象没有ID,因此我无法更新数据库条目或重新从数据库中获取它。

Since my Domain Person object doesn't have it's ID, I can't update the DB entry or re-fetch it from the database.

我唯一想到的替代方法是在服务器上生成一个GUID并将其用作ID。但这会以两个ID的形式创建冗余。由于用户会在搜索字段中引用此ID,因此它也不是很友好。记住ID 135比记住GUID要简单得多。

The only alternative I can think of is generating a guid on the server and using that as an ID. But this creates redundancy in the form of two IDs. It also isn't very user friendly since users will reference this ID in search fields. Remembering ID 135 is much simpler than remembering a guid.

是否可以通过某种方式使dbContext直接更新域对象的ID,或者使ID遍历各层?

Is there some way to get dbContext to update the domain object's ID directly or alternatively have the ID bubble up through the layers?

推荐答案

您可以尝试以下方法。

存储库

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

    public GenericRepository(DbContext context)
    {
        this.context = context;
        this.dbSet = context.Set<TEntity>();
    }
    public virtual TEntity Insert(TEntity entity)
    {
        dbSet.Add(entity);
        return entity;
    }
}

像这样使用

 var uow = new UnitOfWork();
 var added = uow.StudentReposiotry.Insert(new Student
 {
    Name = "Repository",
    RegistrationNo = "BSE-2018-004",
    Date = DateTime.Now,
    Department = "Philosphy",
    Email = "test@"
 });
 uow.Save(); //if its saved.
 long id = added.Id;

我添加了一个称为DataProvider的单独层,该层有助于使我的实体仅在存储库中存在,所有数据都来自DataProvider

I added a separate layer called DataProvider which help to keep my entities live only in Repository everything came from DataProvider speaks about DTO.

public class StudentDataProvider : DataProviderBase
{
     public StudentDto Insert(Student dto)
     { 
         var entity = new Student{ Name = dto.Name,Deparment= dto.Deparment};

         var addedItem = unitofwork.StudentReposiotry.Insert(entity);
         unitofWork.Save();

         dto.Id = addedItem.Id;
         return dto;
     }

     public StudentDto AddAndUpdate(StudentDto dto, StudentDto updateDto)
     { 
         var entity = new Student{ Name = dto.Name,Deparment= dto.Deparment};
         var update= new Student{ Name = updateDto.Name,Deparment= updateDto.Deparment};

         var addedItem = unitofwork.StudentReposiotry.Insert(entity);
         var updateItem=  unitofwork.StudentReposiotry.Update(update)
         unitofWork.Save();

         dto.Id = addedItem.Id;
         return dto;
     }
}

 public class DataProvideBase
{
    protected IUnitOfWork UnitOfWork;

    public DataProvideBase()
    {
        UnitOfWork = new UnitOfWork();
    }
}

这篇关于使用存储库模式时,如何找回数据库生成的ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆