相关疑难解决方法(0)

实体框架6和工作单元......何时何地?它就像ado.net中的交易一样吗?

创建一个新的MVC项目,并喜欢数据层中的存储库的想法,所以我已经实现了它们.我还创建了一个Service层来处理所有业务逻辑和验证,该层依次使用适当的存储库.像这样的东西(我使用Simple Injector注入)

DAL LAYER

public class MyRepository {

    private DbContext _context;
    public MyRepository(DbContext context) {
        _context = context;
    }    

    public MyEntity Get(int id)
    {
        return _context.Set<MyEntity>().Find(id);
    }

    public TEntity Add(MyEntity t)
    {
        _context.Set<MyEntity>().Add(t);
        _context.SaveChanges();
        return t;
    }

    public TEntity Update(MyEntity updated, int key)
    {
        if (updated == null)
            return null;

        MyEntity existing = _context.Set<MyEntity>().Find(key);
        if (existing != null)
        {
            _context.Entry(existing).CurrentValues.SetValues(updated);
            _context.SaveChanges();
        }
        return existing;
    }

    public void Delete(MyEntity t)
    {
        _context.Set<MyEntity>().Remove(t);
        _context.SaveChanges();
    }
}
Run Code Online (Sandbox Code Playgroud)

服务层

public class MyService {
    private …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-mvc entity-framework unit-of-work

32
推荐指数
1
解决办法
3万
查看次数

标签 统计

asp.net-mvc ×1

c# ×1

entity-framework ×1

unit-of-work ×1