[英]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.