0

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; }
    }
Ehsan Akbar
  • 6,977
  • 19
  • 96
  • 180

1 Answers1

3

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");
Neil Smith
  • 2,565
  • 1
  • 15
  • 18
  • Thank you .in my UI i should create this class? – Ehsan Akbar Jul 14 '14 at 17:12
  • 1
    @E.A. I would use an IoC container to inject the context into your repositories and your repositories into your presentation layer. I wouldn't use the DbContext directly from the UI. But also, I don't think I'd let users specify their connection string. Instead I'd make concrete DbContexts for every database you have and give the option for the user to select the DB from a list. – Neil Smith Jul 14 '14 at 17:15
  • Could you please explain this in detail ?which layer should i use this class – Ehsan Akbar Jul 14 '14 at 17:16
  • I add another constructor to my entity model ,but how can i inject my connection string to it in my ui ? – Ehsan Akbar Jul 14 '14 at 17:23
  • IoC is a pretty big topic and is outside the scope of the question. There's plenty of other questions here on SO and plenty of resources on google to point you in the right direction. If you're letting users give their own connection string, you can just pass that string they gave you into the Context's constructor. – Neil Smith Jul 14 '14 at 17:26
  • One more question ,is as you can see in my model i don't create an object of dbcontext ,i just create in my abstract class ,but it doesn't expect any connection string as an input why ? – Ehsan Akbar Jul 14 '14 at 17:43
  • 1
    @EA - I'm confused by the question. Are you asking why your repository doesn't expect a connection string? `GenericRepository` is the only abstract class I see. Also btw, it's really better for future readers to separate each question into individual SO questions. – Neil Smith Jul 14 '14 at 17:49
  • 1
    @EA - Sounds good. Give me a link here and I'll jump on that one after lunch. – Neil Smith Jul 14 '14 at 17:51
  • could you please take a look at this http://stackoverflow.com/questions/24742709/my-dbcontext-connection-string-doesnt-expect-any-input – Ehsan Akbar Jul 14 '14 at 17:54
  • The param is now, more appropriately, `nameOrConnectionString` – Mafii Apr 12 '18 at 10:35