samedi 25 mai 2019

How add multi database to UnitOfWork and generic repository in Entity Framework

I have a Windows Forms application and generic Entity Framework (latest version) method and multi-layer design pattern.

i was write this code . load first db is good but cant load second db and shiow this error : Invalid object name 'dbo.Section'.but dbo.section is exist

Repository layer:

private readonly DbSet<T> dbSetGlobalERP;
    private readonly DbSet<T> dbSetHamkaranSystem;        
    private T Entity;
    private IEnumerable<T> Entities;

public Repository(GlobalERPEntities dbcontextGlobalERP, HamkaranSystemEntities dbcontextHamkaranSystem)
{
    base.dbContextGlobalERP = dbcontextGlobalERP;
    dbSetGlobalERP = base.dbContextGlobalERP.Set<T>();

    base.dbContextHamkaranSystem = dbcontextHamkaranSystem;
    dbSetHamkaranSystem = base.dbContextHamkaranSystem.Set<T>();
}

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

    public virtual IEnumerable<T> GetAll2()
    {
        return dbSetHamkaranSystem.ToList();
    }

UnitOfWork layer: call get all method from repository layer

public UnitOfWork(GlobalERPEntities dbContextGlobalERP, HamkaranSystemEntities dbContextHamkaranSystem)
{
    base.dbContextGlobalERP = dbContextGlobalERP;
    base.dbContextHamkaranSystem = dbContextHamkaranSystem;
}       

public IRepository<T> Repository<T>() where T : class
{
    if (repositories == null)
    {
        repositories = new Dictionary<Type, object>();
    }

    if (repositories.Keys.Contains(typeof(T)) == true)
    {
        return repositories[typeof(T)] as Repository<T>;
    }
    Repository<T> repo = new Repository<T>(dbContextGlobalERP, dbContextHamkaranSystem);
    repositories.Add(typeof(T), repo);
    return repo;
}

 public bool SaveChanges()
    {
        bool returnValue = true;
        using (var dbContextTransaction = dbContextGlobalERP.Database.BeginTransaction())
        {
            try
            {
                dbContextGlobalERP.SaveChanges();
                dbContextTransaction.Commit();
            }
            catch (Exception)
            {
                returnValue = false;
                dbContextTransaction.Rollback();
            }
        }
        return returnValue;
    }

BLL layer: call get all method from UnitOfWork layer

private readonly IUnitOfWork unitOfWork;
public static List<HRPersonView> personFullName = new List<HRPersonView>();

public Service_HR_Person(IUnitOfWork unitOfWork)
{
    this.unitOfWork = unitOfWork;
    FillList();
}

public virtual IEnumerable<HRPerson> GetAll()
{
    return unitOfWork.Repository<HRPerson>().GetAll().ToList();
}

public virtual IEnumerable<Section> GetAll2()
{
    return unitOfWork.Repository<Section>().GetAll2().ToList();
}

how add multi database to this pattern?

Aucun commentaire:

Enregistrer un commentaire