EF 6.0无法从具有多个DBcontext的数据库生成的值中返回PK密钥值

时间:2018-10-02 08:07:37

标签: c# .net entity-framework entity-framework-6

我有一个带有工作单元的通用存储库模式,该模式执行插入事务。由于某些细节,我有一个获取公司类型的BaseController,并根据该公司类型从分配的连接字符串创建了ModelEntities(具有适用于所有数据库上下文的默认模型)。这通常可行,但也会导致EF不返回数据库生成的值。这是我绝对需要的。该模型是数据库首先生成的模型。

该值是Identity指定的,并且具有增量值,所以这不是问题。 我之前做过类似的事情,但不明白为什么它不返回。

我希望你们中的一个对此有一个答案。

我在下面附加了所有代码。

BaseController.cs

public class BaseController : Controller
{
    public UnitOfWork _unitofwork;
    public BaseController(ModelType.Company company)
    {
        LoadInContext(company);
    }

    public void LoadInContext(ModelType.Company company)
    {
        string connectionString = string.Empty;

        switch (company)
        {
            case ModelType.Company.AT:
                connectionString = ConfigurationManager.ConnectionStrings["Entities"].ConnectionString;
                break;
            case ModelType.Company.ELB:
                connectionString = ConfigurationManager.ConnectionStrings["ELBEntities"].ConnectionString;
                break;
            case ModelType.Company.NB:
                connectionString = ConfigurationManager.ConnectionStrings["NBEntities"].ConnectionString;
                break;
            default:
                throw new Exception("Unhandled company");

        }

        _unitofwork = new UnitOfWork(new Entities().CreateEntitiesForDB(connectionString));
    }
}    

Repository.cs

public class Repository<T>
{
    internal DbContext dbContext;
    public IDbSet<T> dbSet;

    public Repository(DbContext context)
    {
        dbContext = context;
        dbSet = context.Set<T>();
    }


    public virtual void Insert(T entity)
    {
        dbSet.Add(entity);
        dbContext.SaveChanges();
    }

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

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

    public virtual void Update(T entityToUpdate)
    {
        dbSet.Attach(entityToUpdate);
        dbContext.Entry(entityToUpdate).State = EntityState.Modified;
        dbContext.SaveChanges();
    }

}

UnitOfWork.cs

public class UnitOfWork
{
    private readonly DbContext context;
    private bool disposed;

    public UnitOfWork(DbContext dbContext)
    {
        context = dbContext;
    }

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

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

    public void Save()
    { }

    public void SaveChangesAsync()
    { }

    public ExampleRepository ExampleRepository
    {
        return new ExampleRepository(context);
    }
}

示例存储库

public class ExampleRepository: Repository<Example>
{
     public ExampleRepository(DbContext context) : base(context)
     {}
}

示例方法

public class ExampleController: BaseController
{
    public ExampleController() : base(ModelType.Company.AT)
    {}

    public void ExampleMethod(Example example)
    {
      _unitofwork.ExampleRepository.Insert(example);
       int id = example.Id
       //This does not return the ID.
    }
}

0 个答案:

没有答案