Asp.net mvc 2 实体框架4,动态查询

Asp.net mvc 2 实体框架4,动态查询,asp.net-mvc-2,linq-to-entities,entity-framework-4,Asp.net Mvc 2,Linq To Entities,Entity Framework 4,是否可以使用实体框架创建动态查询。我有18张桌子,每一张都有相同的结构。如何创建一个动态查询以对每个表重用相同的查询。我想要创建读取更新删除的通用查询。 read包含相同的“Where”子句。 感谢您的帮助。这里是纯积垢场景的简单示例。创建包含查询共享属性的接口。在所有实体类中实现此接口。而不是创建存储库。存储库通常定义为泛型,但在您的案例中,我将每个方法定义为泛型,以便您可以对所有实体使用相同的存储库说明 public interface IWellKnownEntity { int Ty

是否可以使用实体框架创建动态查询。我有18张桌子,每一张都有相同的结构。如何创建一个动态查询以对每个表重用相同的查询。我想要创建读取更新删除的通用查询。 read包含相同的“Where”子句。
感谢您的帮助。

这里是纯积垢场景的简单示例。创建包含查询共享属性的接口。在所有实体类中实现此接口。而不是创建存储库。存储库通常定义为泛型,但在您的案例中,我将每个方法定义为泛型,以便您可以对所有实体使用相同的存储库说明

public interface IWellKnownEntity
{
  int Type { get; set; }
}

public class Repository
{
  public T GetEntityByWellKnownQuery<T>() where T : IWellKnownEntity
  {
    using (var context = new MyContext())
    {
      return context.CreateObjectSet<T>().FirstOrDefault(e => e.Type == 1);
    }
  }

  public IEnumerable<T> GetEntitiesByCustomQuery<T>(Expression<Func<T, bool>> where)
  {
    using (var context = new MyContext())
    {
      return context.CreateObjectSet<T>().Where(where).ToList();
    }
  }

  public void Create<T>(T entity) where T : IWellKnownEntity
  {
    using (var context = new MyContext())
    {
      context.AddObject(entity);
      context.SaveChanges();
    }
  }

  public void Update<T>(T entity) where T : IWellKnownEntity
  {
    using (var context = new MyContext())
    {
      context.Attach(entity);
      context.ObjectStateManager.ChageObjecState(entity, EntityState.Modified);
      context.SaveChanges();
    }
  }

  public void Delete<T>(T entity) where T : IWellKnownEntity
  {
    using (var context = new MyContext())
    {
      context.Attach(entity);
      context.DeleteObject(entity);
      context.SaveChanges();
    }
  }
}
公共接口iWellKnownenty
{
int类型{get;set;}
}
公共类存储库
{
公共T GetEntityByWellKnownQuery(),其中T:IWellKnownEntity
{
使用(var context=new MyContext())
{
返回context.CreateObjectSet().FirstOrDefault(e=>e.Type==1);
}
}
public IEnumerable GetEntitiesByCustomQuery(表达式,其中)
{
使用(var context=new MyContext())
{
返回context.CreateObjectSet().Where(Where).ToList();
}
}
公共无效创建(T实体),其中T:IwellKnownenty
{
使用(var context=new MyContext())
{
AddObject(实体);
SaveChanges();
}
}
公共无效更新(T实体),其中T:IwellKnownenty
{
使用(var context=new MyContext())
{
上下文。附加(实体);
context.ObjectStateManager.chageobjnegate(entity,EntityState.Modified);
SaveChanges();
}
}
公共无效删除(T实体),其中T:IwellKnownenty
{
使用(var context=new MyContext())
{
上下文。附加(实体);
删除对象(实体);
SaveChanges();
}
}
}
比你假设你有实体产品和类别,这推动了众所周知的接口。您只需拨打:

var repository = new Repository();

var product = repository.GetEntityByWellKnownQuery<Product>();
product.Name = "Updated";
repository.Update<Product>(product);

var category = repository.GetEntitiesByCustomQuery<Category>(c => c.Id == 1).First();
repository.Delete<Category>(category);
var repository=newrepository();
var product=repository.GetEntityByWellKnownQuery();
product.Name=“已更新”;
更新(产品);
var category=repository.GetEntitiesByCustomQuery(c=>c.Id==1.First();
删除(类别);

您可以进一步改进示例代码。此代码不使用共享上下文,因此更适合断开连接的场景(web应用程序)。如果使用WinForms应用程序或批处理应用程序等连接场景,则可以在存储库上实现IDisposable,并在所有方法之间共享上下文。存储库上的Dispose方法将处理上下文。更新和删除方法的代码将不同,因为不需要将实体附加回上下文或设置实体状态。

这里有一个纯CRUD场景的简单示例。创建包含查询共享属性的接口。在所有实体类中实现此接口。而不是创建存储库。存储库通常定义为泛型,但在您的案例中,我将每个方法定义为泛型,以便您可以对所有实体使用相同的存储库说明

public interface IWellKnownEntity
{
  int Type { get; set; }
}

public class Repository
{
  public T GetEntityByWellKnownQuery<T>() where T : IWellKnownEntity
  {
    using (var context = new MyContext())
    {
      return context.CreateObjectSet<T>().FirstOrDefault(e => e.Type == 1);
    }
  }

  public IEnumerable<T> GetEntitiesByCustomQuery<T>(Expression<Func<T, bool>> where)
  {
    using (var context = new MyContext())
    {
      return context.CreateObjectSet<T>().Where(where).ToList();
    }
  }

  public void Create<T>(T entity) where T : IWellKnownEntity
  {
    using (var context = new MyContext())
    {
      context.AddObject(entity);
      context.SaveChanges();
    }
  }

  public void Update<T>(T entity) where T : IWellKnownEntity
  {
    using (var context = new MyContext())
    {
      context.Attach(entity);
      context.ObjectStateManager.ChageObjecState(entity, EntityState.Modified);
      context.SaveChanges();
    }
  }

  public void Delete<T>(T entity) where T : IWellKnownEntity
  {
    using (var context = new MyContext())
    {
      context.Attach(entity);
      context.DeleteObject(entity);
      context.SaveChanges();
    }
  }
}
公共接口iWellKnownenty
{
int类型{get;set;}
}
公共类存储库
{
公共T GetEntityByWellKnownQuery(),其中T:IWellKnownEntity
{
使用(var context=new MyContext())
{
返回context.CreateObjectSet().FirstOrDefault(e=>e.Type==1);
}
}
public IEnumerable GetEntitiesByCustomQuery(表达式,其中)
{
使用(var context=new MyContext())
{
返回context.CreateObjectSet().Where(Where).ToList();
}
}
公共无效创建(T实体),其中T:IwellKnownenty
{
使用(var context=new MyContext())
{
AddObject(实体);
SaveChanges();
}
}
公共无效更新(T实体),其中T:IwellKnownenty
{
使用(var context=new MyContext())
{
上下文。附加(实体);
context.ObjectStateManager.chageobjnegate(entity,EntityState.Modified);
SaveChanges();
}
}
公共无效删除(T实体),其中T:IwellKnownenty
{
使用(var context=new MyContext())
{
上下文。附加(实体);
删除对象(实体);
SaveChanges();
}
}
}
比你假设你有实体产品和类别,这推动了众所周知的接口。您只需拨打:

var repository = new Repository();

var product = repository.GetEntityByWellKnownQuery<Product>();
product.Name = "Updated";
repository.Update<Product>(product);

var category = repository.GetEntitiesByCustomQuery<Category>(c => c.Id == 1).First();
repository.Delete<Category>(category);
var repository=newrepository();
var product=repository.GetEntityByWellKnownQuery();
product.Name=“已更新”;
更新(产品);
var category=repository.GetEntitiesByCustomQuery(c=>c.Id==1.First();
删除(类别);

您可以进一步改进示例代码。此代码不使用共享上下文,因此更适合断开连接的场景(web应用程序)。如果使用WinForms应用程序或批处理应用程序等连接场景,则可以在存储库上实现IDisposable,并在所有方法之间共享上下文。存储库上的Dispose方法将处理上下文。Update和Delete方法的代码将不同,因为不需要将实体附加回上下文或设置实体状态。

我已经完成了您所说的操作,但出现了以下异常:无法将类型“MyNamespace.MyEntity”强制转换为类型“MyNamespace.IWellKnownEntity”。LINQ to Entities仅支持转换实体数据模型基元类型。您可以发布一些代码吗?您不应该使用IwellKnowlementity,而应该直接使用MyEntity而不使用任何强制转换。我再次检查了我的代码,唯一的区别是在查询您使用的时,使用了CreateObjectSet,但我使用了CreateQuery。我还没有检查这是否是问题所在。但我有一个问题,当EF为我的模型生成ObjectContext时,它会创建对象集。当您在GetEntityByWellKnownQuery方法上再次使用CreateObjectSet时,它不会创建t的另一个实例吗?另一件事是CreateObjectSet的定义是“public ObjectSet CreateObjectSet(),其中tenty:class;