我对Telerik和用于OpenAccess项目的数据库优先方法是新手。我在他们的网站上浏览了这个关于模型的教程视频:http://tv.telerik.com/watch/orm/building-a-mvc-3-application-database-first-with-openaccess-creating-model?seriesID=1529
我很想知道如何从Modal扩展域类和查询数据库?例如,我有一个生成的"Person“类&我用下面的类扩展它:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MVCApplication
{
public partial class Person
{
public string PersonName
{
get
{
return this.FirstName + " " + this.LastName;
}
}
}
}这与上面视频中的示例非常相似。我想知道是否可以从Person表或Person对象集合中检索满足特定条件的所有记录?我的"return“查询是怎样的?我在这个扩展模型类中没有可用的dbContext :(
public List<Person> GetAllPeople()
{
// return List here
}
public List<Person> GetAllPeopleFromLocationA(int locationID)
{
//return List here
}发布于 2013-04-10 16:48:59
通常,域类并不用于查询数据库,我建议您在域上下文的分部类中添加GetAllPeople和GetAllPeopleFromLocationA方法,如下所示:
public List<Person> GetAllPeople()
{
return this.People.ToList();
}
public List<Person> GetAllPeopleFromLocationA(int locationID)
{
return this.People.Where(p => p.LocationID == locationID).ToList();
}然后,您可以使用这些方法,如:
using (YourContextName context = new YourContextName())
{
foreach (Person person in context.GetAllPeople())
{
// you could access your custom person.PersonName property here
}
foreach (Person person in context.GetAllPeopleFromLocationA(someLocationID))
{
// you could access your custom person.PersonName property here
}
}https://stackoverflow.com/questions/15910830
复制相似问题