如何在EF中使用共享上下文

时间:2014-01-03 15:18:04

标签: 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.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
}

我确信Ef和sql之间的连接已建立。我的数据库中有数据。但是findbyId总是显示错误“找不到序列”。我检查并发现ef不会填充dbSet。任何建议

序列不包含匹配元素没有任何其他内部异常 EF版本6.0.2

1 个答案:

答案 0 :(得分:0)

您很可能需要在DbContext上定义DbSets的属性。即

public class SecurityDbContext : DbContextBase<SecurityDbContext>
{

    public DbSet<Permission> Permissions { get; set;}

    // or use IDbSet

    public IDbSet<CustomRole> CustomRoles { get; set;}

    // other stuff
}