我使用实体框架6.0,代码第一种方法和工作单元模式。我想动态构建连接字符串并从数据库中获取数据。如果我有连接字符串的web.config标记,数据库连接可以正常工作。但是,我不想为连接字符串添加web.config标记。我看到了许多与此相关的问题,但我没有找到与codefirst方法相关的问题。请告诉我如何实现这一目标。
-------------------- 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; }
}
}
答案 0 :(得分:0)
您的问题是您专门传递的连接字符串名称只会在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
上的连接字符串放在其他任何地方都是一种更好的做法。