简体   繁体   English

如何将我的存储库从EF代码优先转换为使用EDMX实体模型?

[英]How do I switch my repositories from EF code-first to using a EDMX entity model?

Currently my repositories consist of the following code: 目前,我的存储库由以下代码组成:

public interface IRepository<T> where T : class
{
    T Get(int id);
    IQueryable<T> GetAll();
    IQueryable<T> Where(Expression<Func<T, bool>> predicate);
    IQueryable<T> Query();
    void Add(T entity);
    void Remove(T entity);
    void Save();
}

public class Repository<T> : IRepository<T> where T : class
    {
        private readonly IContextProvider  _ctxProvider;
        protected BaseModelContext _ctx
        {
            get
            {
                return _ctxProvider.DataContext;
            }
        }

        public Repository(IContextProvider ctx)
        {
            _ctxProvider = ctx;
        }

        public T Get(int id)
        {
            return _ctx.Set<T>().Find(id);
        }

        public IQueryable<T> GetAll()
        {
            return _ctx.Set<T>();
        }

        public IQueryable<T> Where(Expression<Func<T, bool>> predicate)
        {
            return _ctx.Set<T>().Where(predicate);
        }

        public void Add(T entity)
        {
            _ctx.Set<T>().Add(entity);
        }

        public void Remove(T entity)
        {            
            _ctx.Set<T>().Remove(entity);
        }

        public IQueryable<T> Query()
        {
            return _ctx.Set<T>().AsQueryable();
        }

        public IQueryable<T> Eager(string path)
        {
            return _ctx.Set<T>().Include(path);
        }

        public void Save()
        {
            _ctx.SaveChanges();
        }
    }

I've created my EDMX model from the database, but I've noticed where you would use _ctx.Set to template against the code-first table per class pattern, the Set() method isn't available on the entities created by the generated .edmx file. 我已经从数据库创建了我的EDMX模型,但是我注意到您会在_ctx.Set的位置使用每个类模式的代码优先表进行模板化,Set()方法不适用于由生成的.edmx文件。 I would have thought that EF would be able to flip between the 2 types quite easily. 我以为EF可以很容易地在两种类型之间切换。 Do I have to re-write my repository class just so it will work with EDMX models? 我是否必须重新编写我的存储库类才能使其与EDMX模型一起使用?

By default for model first and database first approaches VS2010 generates ObjectContext based context and EntityObject based entities. 默认情况下,对于模型优先和数据库优先方法,VS2010生成基于ObjectContext的上下文和基于EntityObject的实体。 This changed in VS2012 where by default POCO entities and DbContext based context will be generated. 这在VS2012中已更改,默认情况下将在其中生成POCO实体和基于DbContext的上下文。 CodeFirst uses POCO entities and DbContext based context which is in line with VS2012 but not VS2010. CodeFirst使用POCO实体和基于DbContext的上下文,该上下文与VS2012一致,而不与VS2010一致。 However even with VS2010 you can generate DbContext and POCO entities. 但是,即使使用VS2010,您也可以生成DbContext和POCO实体。 First, right click on the designer surface and in the properties change code generation strategy from Default to None. 首先,右键单击设计器图面,然后在属性中将代码生成策略从“默认”更改为“无”。 Then right click on your project and "Add a new Item". 然后右键单击您的项目,然后单击“添加新项目”。 Click "Online" to the left. 点击左侧的“在线”。 You can type DbContext in the search dialog. 您可以在搜索对话框中键入DbContext。 Select "EF 4.x DbContext Generator for C#". 选择“用于C#的EF 4.x DbContext生成器”。 This will add T4 templates to your project that will generate DbContext based context and POCO entities from your edmx file. 这会将T4模板添加到您的项目中,该模板将从edmx文件生成基于DbContext的上下文和POCO实体。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在实体框架(EF6)代码优先中映射冗余关系 - How do I map a redundant relationship in Entity Framework (EF6) Code-First 我如何使用代码优先框架添加对另一个现有实体的引用,而无需先从数据库中提取现有实体 - How do I add a reference to another existing entity using code-first framework without first pulling the existing entity from the database 使用实体框架代码优先我是否需要让我的域模型贫血? - Using Entity Framework Code-first am I required to have the model of my domain anemic? 如何 map 实体框架代码优先 model 到单个 SQL 服务器架构? - How do I map an Entity Framework Code-First model to a single SQL Server Schema? 如何从脚本生成“代码优先”的EF类? - How do I generate “code-first” EF classes from a script? 如何在EF4.3代码优先的情况下映射到复杂类型? - How do I map to and from a complex type in EF4.3 code-first? 如何从Entity Framework 4.3代码优先模型生成DDL脚本? - How can I generate DDL scripts from Entity Framework 4.3 Code-First Model? 如何预编译实体框架代码优先查询? - How do I precompile an Entity Framework Code-First Query? 我什么时候应该在我的项目中使用EF代码优先或模型优先? - When should I use EF code-first or model-first in my projects? 如何使用Entity Framework代码优先映射带有复合键的继承表? - How do I map an inherited table with a composite key using Entity Framework code-first?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM