问题
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="data source=****;initial catalog=DB-Metro;user id=sa;password=****;MultipleActiveResultSets=True;App=EntityFramework"" 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(string connectionName)
: base(connectionName)
{
}
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; }
}
i clear the connection string in app.config
file ,but the application doesn't expect any connection string ,and it can't connect to database .why ?Where should i inject my connection string to dbcontext ?as you can see my constructor expects an input.I need to do this in UI layer
best regards
回答1:
Pass the name of the connection string, not the connection string itself, like so:
<connectionStrings>
<add name="ShirazRailwayEntities" connectionString="metadata=res://*/RailWay.csdl|res://*/RailWay.ssdl|res://*/RailWay.msl;provider=System.Data.SqlClient;provider connection string="data source=****;initial catalog=DB-Metro;user id=sa;password=****;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
var context = new ShirazRailwayEntities("ShirazRailwayEntities");
or
var context = new ShirazRailwayEntities("someOtherConnectionStringInAppConfig");
来源:https://stackoverflow.com/questions/24742709/my-dbcontext-doesnt-expect-any-connection-string-as-an-input