passed dbcontext connection string to DALayer

杀马特。学长 韩版系。学妹 提交于 2019-12-13 02:59:11

问题


I have 4 layer in my application UI,DomainClass,Model(DBCntext),Repository.

In repository i have an abstract class like this :

 public abstract class GenericRepository<C, T> :
    IGenericRepository<T>
        where T : class
        where C : DbContext, new()
    {

        private C _entities = new C();
        public C Context
        {

            get { return _entities; }
            set { _entities = value; }
        }

        public virtual IQueryable<T> GetAll()
        {

            IQueryable<T> query = _entities.Set<T>();
            return query;
        }

        public IQueryable<T> FindBy(System.Linq.Expressions.Expression<Func<T, bool>> predicate)
        {

            IQueryable<T> query = _entities.Set<T>().Where(predicate);
            return query;
        }

        public virtual void Add(T entity)
        {
            _entities.Set<T>().Add(entity);
        }

        public virtual void Delete(T entity)
        {
            _entities.Set<T>().Remove(entity);
        }

        public virtual void Edit(T entity)
        {
            _entities.Entry(entity).State = System.Data.Entity.EntityState.Modified;
        }

        public virtual void Save()
        {
            _entities.SaveChanges();
        }

    }

All my entities inheritance from this class like this :

namespace Repository
{
    public class StationRepository : GenericRepository<ShirazRailWay.ShirazRailwayEntities, DomainClass.Station>
    {
    }
}

I UI i called this repositories. as you can see here :

  stationrepository objnew=new stationrepository();
  obnew.getall();

In UI layer i have an connection string in app.config as you can see here :

<connectionStrings>
    <add name="ShirazRailwayEntities" connectionString="metadata=res://*/RailWay.csdl|res://*/RailWay.ssdl|res://*/RailWay.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=****;initial catalog=DB-Metro;user id=sa;password=****;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>

But i want to give an option to my users that with this option they can set their connection string by themselves.So i created a form in UI layer that when the users trying to log in it asks them the connection string .My problem is How can pass this connection string to my dbcontext?

In my model layer(dbcontext) i have this :

public partial class ShirazRailwayEntities : DbContext
    {
        public ShirazRailwayEntities()
            : base("name=ShirazRailwayEntities")
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }

        public DbSet<Advertisement> Advertisements { get; set; }
        public DbSet<Line> Lines { get; set; }
        public DbSet<Log> Logs { get; set; }
        public DbSet<Path> Paths { get; set; }
        public DbSet<Sensor> Sensors { get; set; }
        public DbSet<Station> Stations { get; set; }
        public DbSet<Train> Trains { get; set; }
        public DbSet<User> Users { get; set; }
        public DbSet<TimeTable> TimeTables { get; set; }
        public DbSet<ConfigFont> ConfigFonts { get; set; }
        public DbSet<ArrivalTime> ArrivalTimes { get; set; }
        public DbSet<ConfigColor> ConfigColors { get; set; }
    }

回答1:


Add another constructor that takes your connection string:

public partial class ShirazRailwayEntities : DbContext
{
    public ShirazRailwayEntities()
        : base(name: "ShirazRailwayEntities")
    {
    }

    public ShirazRailwayEntities(string connectionName)
        : base(name: connectionName)
    {
    }
}

var context = new ShirazRailwayEntities("whatever connection name you want");


来源:https://stackoverflow.com/questions/24741575/passed-dbcontext-connection-string-to-dalayer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!