[英]Dynamic connection string for entity framework code first approach
我正在使用帶有代碼優先方法和工作單元模式的實體框架6.0。 我想動態地建立連接字符串並從數據庫中獲取數據。 如果我具有連接字符串的web.config標記,則數據庫連接可以正常工作。 但是,我不想為連接字符串使用web.config標記。 我看到了許多與此有關的問題,但沒有發現與代碼優先方法有關的問題。 請讓我知道如何實現這一目標。
-------------------- Repository.cs --------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Entity;
using System.Linq.Expressions;
using Commons.DTO;
namespace Repository
{
public class Repository<TEntity> where TEntity : class
{
internal CommonsDbContext context;
internal DbSet<TEntity> dbSet;
public Repository(CommonsDbContext context)
{
if (context == null)
throw new ArgumentNullException("context");
this.context = context;
this.dbSet = context.Set<TEntity>();
}
public virtual IEnumerable<TEntity> Get(Expression<Func<TEntity, bool>> filter = null, Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null, string includeProperties = "")
{
IQueryable<TEntity> query = dbSet;
if (filter != null)
query = query.Where(filter);
string[] properties = includeProperties.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
foreach (var includeProperty in properties)
query = query.Include(includeProperty);
if (orderBy != null)
return orderBy(query).ToList();
else
return query.ToList();
}
public virtual TEntity GetByID(object id)
{
return dbSet.Find(id);
}
public virtual void Insert(TEntity entity)
{
dbSet.Add(entity);
}
public virtual void Delete(object id)
{
TEntity entityToDelete = dbSet.Find(id);
Delete(entityToDelete);
}
public virtual void Delete(TEntity entityToDelete)
{
if (context.Entry(entityToDelete).State == EntityState.Detached)
dbSet.Attach(entityToDelete);
dbSet.Remove(entityToDelete);
}
public virtual void Update(TEntity entityToUpdate)
{
dbSet.Attach(entityToUpdate);
context.Entry(entityToUpdate).State = EntityState.Modified;
}
public virtual bool IsValid(TEntity entityToValid, Expression<Func<TEntity, bool>> filter)
{
return dbSet.Where(filter).Any();
}
}
}
-------------------- UnitOfWork.cs --------------------
using System;
using Commons.DTO;
namespace Repository
{
public class UnitOfWork : IDisposable
{
protected CommonsDbContext dbContext = null;
public UnitOfWork()
{
dbContext = new CommonsDbContext();
}
public UnitOfWork(CommonsDbContext context)
{
if (context == null)
throw new ArgumentNullException("context");
this.dbContext = context;
}
public virtual void Save()
{
dbContext.SaveChanges();
}
#region IDisposable Members
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
dbContext.Dispose();
dbContext = null;
}
}
~UnitOfWork()
{
Dispose(false);
}
#endregion
}
}
-------------CommonsDbContext.cs
using System.Data.Entity;
namespace Commons.DTO
{
public class CommonsDbContext : DbContext
{
public CommonsDbContext() : base("name=DbContext")
{
Database.SetInitializer<CommonsDbContext>(null);
}
public CommonsDbContext(string nameOrConnectionString) : base(nameOrConnectionString)
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new ApplicationMapper());
}
public DbSet<Application> Applications { get; set; }
}
}
您的問題是您要專門傳遞一個連接字符串名稱,該名稱只能在web.config
或app.config
上進行搜索。 如果不想使用web.config
請將其更改為完整的連接字符串:
public class CommonsDbContext : DbContext
{
//Approach 1:
private const connectionString = "...";
public CommonsDbContext() : base(connectionString)
{
Database.SetInitializer<CommonsDbContext>(null);
}
//Approach 2:
public static CommonsDbContext GetContext()
{
var builder = new SqlConnectionStringBuilder();
// add data here
return new CommonsDbContext(builder.ConnectionString);
}
private CommonsDbContext(string connString) : base(connString)
{
Database.SetInitializer<CommonsDbContext>(null);
}
}
請注意,但是,將連接字符串放置在web.config
比其他任何地方都要好得多。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.