C# 如何在EF中使用共享上下文
我试图在EF中实现共享上下文练习。代码如下所示C# 如何在EF中使用共享上下文,c#,entity-framework,ef-code-first,C#,Entity Framework,Ef Code First,我试图在EF中实现共享上下文练习。代码如下所示 // Base class of all context public class DbContextBase<TContext> : DbContext, IDbContext where TContext : DbContext { public DbContextBase() : base("DataConnection") { Configuration.LazyLoadingE
// Base class of all context
public class DbContextBase<TContext> : DbContext, IDbContext where TContext : DbContext
{
public DbContextBase() :
base("DataConnection")
{
Configuration.LazyLoadingEnabled = false;
}
static DbContextBase()
{
Database.SetInitializer<TContext>(null);
}
public new IDbSet<T> Set<T>() where T : class
{
return base.Set<T>();
}
}
// My one splitted context
public class SecurityDbContext : DbContextBase<SecurityDbContext>
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new MenivaModuleConfiguration());
modelBuilder.Configurations.Add(new ModuleItemConfiguration());
modelBuilder.Configurations.Add(new ItemControllerConfiguration());
modelBuilder.Configurations.Add(new PermissionListConfiguration());
modelBuilder.Configurations.Add(new CustomRoleConfiguration());
base.OnModelCreating(modelBuilder);
}
}
// IDbInterface implimented by all splited context
public interface IDbContext
{
IDbSet<T> Set<T>() where T : class;
int SaveChanges();
DbEntityEntry Entry(object o);
void Dispose();
}
// constructor of my unit work; my unit of work class detentions few things removed (dispose)
public UnitOfWork(IDbContext dbContext)
{
_context = dbContext;
}
// I am using activator pattern to get corresponding repositories its the code is given below .
public IRepository<T> Repository<T>() where T : class
{
if (_repositories == null)
_repositories = new Hashtable();
var type = typeof(T).Name;
if (!_repositories.ContainsKey(type))
{
var repositoryType = typeof(BaseRepository<>);
var repositoryInstance =
Activator.CreateInstance(repositoryType
.MakeGenericType(typeof(T)), _context);
_repositories.Add(type, repositoryInstance);
}
return (IRepository<T>)_repositories[type];
}
// Unit of work save
public void Save()
{
_context.SaveChanges();
}
// This is my Base repository generic repository
public class BaseRepository<TEntity> : IRepository<TEntity> where TEntity : class
{
IDbContext Context;
IDbSet<TEntity> DbSet;
public BaseRepository(IDbContext context)
{
Context = context;
DbSet = context.Set<TEntity>();
}
#region Implementation of IRepository<TEntity>
public TEntity FindById(object id)
{
return DbSet.Find(id);
}
public void Add(TEntity entity)
{
DbSet.Add(entity);
}
public void Update(TEntity entity)
{
Context.Entry(entity).State = EntityState.Modified;
DbSet.Attach(entity);
}
public void Delete(TEntity entity)
{
Context.Entry(entity).State=EntityState.Deleted;
DbSet.Remove(entity);
}
public IQueryable<TEntity> GetAll()
{
return DbSet;
}
public IQueryable<TEntity> Query(CompositeSpecification<TEntity> specification)
{
return DbSet.Where(specification.IsSatisfiedBy).AsQueryable();
}
#endregion
}
//所有上下文的基类
公共类DbContextBase:DbContext,IDbContext其中TContext:DbContext
{
public DbContextBase():
基本(“数据连接”)
{
Configuration.LazyLoadingEnabled=false;
}
静态DbContextBase()
{
Database.SetInitializer(null);
}
public new IDbSet Set(),其中T:class
{
返回base.Set();
}
}
//我的一个分裂的背景
公共类SecurityDbContext:DbContextBase
{
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
添加(新的MenivaModuleConfiguration());
添加(新模块ItemConfiguration());
添加(新的ItemControllerConfiguration());
添加(新的PermissionListConfiguration());
添加(新的CustomRoleConfiguration());
基于模型创建(modelBuilder);
}
}
//由所有拆分的上下文实现的IDB接口
公共接口上下文
{
IDbSet Set(),其中T:class;
int SaveChanges();
DbEntityEntry(对象o);
无效处置();
}
//我单位工程的建设者;我单位的工人阶级拘留几件东西被移走(处置)
公共工作单元(IDbContext dbContext)
{
_context=dbContext;
}
//我正在使用activator模式获取相应的存储库,其代码如下所示。
公共IRepository存储库(),其中T:class
{
如果(_==null)
_repositories=newhashtable();
var type=typeof(T).名称;
if(!\u repositories.ContainsKey(类型))
{
var repositoryType=typeof(BaseRepository);
var repositoryInstance=
Activator.CreateInstance(repositoryType
.MakeGenericType(typeof(T)),_context);
_Add(类型,repositoryInstance);
}
返回(IRepository)_存储库[类型];
}
//工作单位节省
公共作废保存()
{
_SaveChanges();
}
//这是我的基本存储库通用存储库
公共类BaseRepository:IRepository,其中tenty:class
{
语境;
IDbSet-DbSet;
公共BaseRepository(IDbContext上下文)
{
上下文=上下文;
DbSet=context.Set();
}
#IRepository的区域实施
public tenty FindById(对象id)
{
返回DbSet.Find(id);
}
公共无效添加(TEntity实体)
{
添加(实体);
}
公共无效更新(TEntity实体)
{
Context.Entry(entity.State=EntityState.Modified;
附加数据集(实体);
}
公共作废删除(可撤销实体)
{
Context.Entry(entity.State=EntityState.Deleted;
删除(实体);
}
公共IQueryable GetAll()
{
返回DbSet;
}
公共IQueryable查询(复合规范)
{
返回DbSet.Where(specification.issatifiedby).AsQueryable();
}
#端区
}
我确信Ef和sql之间的连接已经建立。我的数据库里有数据。但是findbyId总是显示错误“找不到序列”。我检查并发现ef不会填充数据库集。有什么建议吗
序列不包含匹配元素没有任何额外的内部异常
EF版本6.0.2您很可能需要在DbContext上定义数据库集的属性。i、 e
public class SecurityDbContext : DbContextBase<SecurityDbContext>
{
public DbSet<Permission> Permissions { get; set;}
// or use IDbSet
public IDbSet<CustomRole> CustomRoles { get; set;}
// other stuff
}
公共类SecurityDbContext:DbContextBase
{
公共数据库集权限{get;set;}
//或者使用IDbSet
公共IDbSet自定义角色{get;set;}
//其他东西
}
很抱歉问这个问题。上面的代码运行良好,但我的映射文件(配置)包含一个错误columnType(nvarchar类型为narchar)。浪费了至少13个小时和2GB或更多数据。