I have a table with 9 columns in database and I want to be able to load only some fields of it if I need.
我在数据库中有一个包含9列的表,如果需要的话,我想只加载其中的一些字段。
How can I do this with Entity Framework 4 please?
请告诉我如何使用实体框架4来实现这一点?
e.g. My table has these fields:
我的桌子上有这些字段:
ID, FirstName, LastName, FotherName, BirthDate, Mobile, Email
and I want to be able to fetch just these columns:
我希望能取到这些列
ID, FirstName, LastName
My project is an ASP.NET MVC 3
application, with SQLServer 2008 Express
and EF 4.1
.
我的项目是ASP。NET MVC 3应用程序,带有SQLServer 2008 Express和EF 4.1。
2 个解决方案
#1
25
Assume you have a table with this model:
假设您有一个与此模型相关的表:
public class User{
public int ID {get; set;}
public string NickName {get; set;}
public string FirstName {get; set;}
public string LastName {get; set;}
public string FotherName {get; set;}
public DateTime BirthDate {get; set;}
public string Mobile {get; set;}
public string Email {get; set;}
public string Password {get; set;}
}
Now, you want fetch just ID
, FirstName
, LastName
, and FotherName
. You can do it in 2 way; The first way is fetch them as an anonymous
object, look:
现在,您需要获取ID、FirstName、LastName和FotherName。你可以用两种方法;第一种方法是将它们作为匿名对象获取,看:
var user = entityContext.Users.Where(u => u.ID == id)
.Select(u => new {
ID = u.ID,
FirstName = u.FirstName,
LastName = u.LastName,
FotherName = u.FotherName
}).Single();
Now, your return-value-type is anonymous
, you can work with it such as:
现在,您的收益价值类型是匿名的,您可以使用它,例如:
var i = user.ID;
// or
var s = user.FirstName;
In another way (for example when you want to pass the object as an Model to a View), you can define a new class, (i.e. UserViewModel
), and when you select the object, select it as a UserViewModel
. look:
以另一种方式(例如,当您想将对象作为模型传递给视图时),您可以定义一个新的类(例如,UserViewModel),当您选择对象时,选择它作为UserViewModel。看:
public class UserViewModel{
public int ID {get; set;}
public string NickName {get; set;}
public string FirstName {get; set;}
public string LastName {get; set;}
public string FotherName {get; set;}
}
and in query, take this:
在查询中,取这个:
var user = entityContext.Users.Where(u => u.ID == id)
.Select(u => new UserViewModel {
ID = u.ID,
FirstName = u.FirstName,
LastName = u.LastName,
FotherName = u.FotherName
}).Single();
Look that just ONE difference is between them, in labda expression, instead of u => new {}
we are using u => new UserViewModel{}
. Good luck.
看,在labda表达式中,它们之间只有一个区别,而不是u => new{}我们使用u => new UserViewModel{}。祝你好运。
#2
0
There can be many ways to do this job, but using Automapper
NuGet package is the most simple one I have experienced.
有很多方法可以完成这项工作,但是使用Automapper NuGet包是我经历过的最简单的方法。
-
First: Install
Autmapper
NuGet package for your project from NuGet package explorer. - 首先:从NuGet package explorer为您的项目安装Autmapper NuGet包。
-
Second: Make a simple
ViewModel
, which contains only required attributes:第二:创建一个简单的ViewModel,其中只包含必需的属性:
public class UserViewModel { public int ID {get; set;} public string FirstName {get; set;} public string LastName {get; set;} }
-
Third: Initialize your your mapper only for once in
app_start
class like:第三:在app_start类中只初始化一次映射器,比如:
namespace SampleProject.App_Start { public class AutoMapperConfig { public static void Initializer() { AutoMapper.Mapper.Initialize(cfg => { cfg.CreateMap<User, UserViewModel>() }); } } }
-
Fourth: Add it's entry in
Global.asax.cs
:第四:加入Global.asax.cs:
namespace SampleProject { public class MvcApplication : System.Web.HttpApplication { protected void Application_Start() { AreaRegistration.RegisterAllAreas(); GlobalConfiguration.Configure(WebApiConfig.Register); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); // AutoMapper Initializer App_Start.AutoMapperConfig.Initializer(); } } }
-
Fifth: Use it in your controller where you want like this:
第五:在你想要的控制器中使用:
namespace SampleProject.Controllers { public class UsersController : Controller { private DataContext db = new DataContext(); public ActionResult Index()( var model = AutoMapper.Mapper.Map<List<UserViewModel>>(db.User.ToList()); return View(model); } } }
-
Last: You can create as many maps as you want between your
Models
andViewModels
by initializing them once in theapp_start
'sAutoMapperConfig
class and use them where you want with a simple line of code.最后:您可以在app_start的AutoMapperConfig类中初始化它们,并使用简单的代码行在您想要的任何地方创建模型和视图模型之间的任意数量的映射。
Also you can find a lot of help about Automapper
if you search about it. Its main website is here.
如果你搜索的话,你会发现很多关于Automapper的帮助。它的主要网站在这里。
Important:
I am a developer of ASP.NET MVC 5
. Automapper
works fine for me every time. I cannot check it on MVC 3
or older than MVC 5
.
我是ASP的开发人员。净MVC 5。自动贩卖机每次对我都很好用。我不能在MVC 3或更老的MVC 5上检查它。
#1
25
Assume you have a table with this model:
假设您有一个与此模型相关的表:
public class User{
public int ID {get; set;}
public string NickName {get; set;}
public string FirstName {get; set;}
public string LastName {get; set;}
public string FotherName {get; set;}
public DateTime BirthDate {get; set;}
public string Mobile {get; set;}
public string Email {get; set;}
public string Password {get; set;}
}
Now, you want fetch just ID
, FirstName
, LastName
, and FotherName
. You can do it in 2 way; The first way is fetch them as an anonymous
object, look:
现在,您需要获取ID、FirstName、LastName和FotherName。你可以用两种方法;第一种方法是将它们作为匿名对象获取,看:
var user = entityContext.Users.Where(u => u.ID == id)
.Select(u => new {
ID = u.ID,
FirstName = u.FirstName,
LastName = u.LastName,
FotherName = u.FotherName
}).Single();
Now, your return-value-type is anonymous
, you can work with it such as:
现在,您的收益价值类型是匿名的,您可以使用它,例如:
var i = user.ID;
// or
var s = user.FirstName;
In another way (for example when you want to pass the object as an Model to a View), you can define a new class, (i.e. UserViewModel
), and when you select the object, select it as a UserViewModel
. look:
以另一种方式(例如,当您想将对象作为模型传递给视图时),您可以定义一个新的类(例如,UserViewModel),当您选择对象时,选择它作为UserViewModel。看:
public class UserViewModel{
public int ID {get; set;}
public string NickName {get; set;}
public string FirstName {get; set;}
public string LastName {get; set;}
public string FotherName {get; set;}
}
and in query, take this:
在查询中,取这个:
var user = entityContext.Users.Where(u => u.ID == id)
.Select(u => new UserViewModel {
ID = u.ID,
FirstName = u.FirstName,
LastName = u.LastName,
FotherName = u.FotherName
}).Single();
Look that just ONE difference is between them, in labda expression, instead of u => new {}
we are using u => new UserViewModel{}
. Good luck.
看,在labda表达式中,它们之间只有一个区别,而不是u => new{}我们使用u => new UserViewModel{}。祝你好运。
#2
0
There can be many ways to do this job, but using Automapper
NuGet package is the most simple one I have experienced.
有很多方法可以完成这项工作,但是使用Automapper NuGet包是我经历过的最简单的方法。
-
First: Install
Autmapper
NuGet package for your project from NuGet package explorer. - 首先:从NuGet package explorer为您的项目安装Autmapper NuGet包。
-
Second: Make a simple
ViewModel
, which contains only required attributes:第二:创建一个简单的ViewModel,其中只包含必需的属性:
public class UserViewModel { public int ID {get; set;} public string FirstName {get; set;} public string LastName {get; set;} }
-
Third: Initialize your your mapper only for once in
app_start
class like:第三:在app_start类中只初始化一次映射器,比如:
namespace SampleProject.App_Start { public class AutoMapperConfig { public static void Initializer() { AutoMapper.Mapper.Initialize(cfg => { cfg.CreateMap<User, UserViewModel>() }); } } }
-
Fourth: Add it's entry in
Global.asax.cs
:第四:加入Global.asax.cs:
namespace SampleProject { public class MvcApplication : System.Web.HttpApplication { protected void Application_Start() { AreaRegistration.RegisterAllAreas(); GlobalConfiguration.Configure(WebApiConfig.Register); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); // AutoMapper Initializer App_Start.AutoMapperConfig.Initializer(); } } }
-
Fifth: Use it in your controller where you want like this:
第五:在你想要的控制器中使用:
namespace SampleProject.Controllers { public class UsersController : Controller { private DataContext db = new DataContext(); public ActionResult Index()( var model = AutoMapper.Mapper.Map<List<UserViewModel>>(db.User.ToList()); return View(model); } } }
-
Last: You can create as many maps as you want between your
Models
andViewModels
by initializing them once in theapp_start
'sAutoMapperConfig
class and use them where you want with a simple line of code.最后:您可以在app_start的AutoMapperConfig类中初始化它们,并使用简单的代码行在您想要的任何地方创建模型和视图模型之间的任意数量的映射。
Also you can find a lot of help about Automapper
if you search about it. Its main website is here.
如果你搜索的话,你会发现很多关于Automapper的帮助。它的主要网站在这里。
Important:
I am a developer of ASP.NET MVC 5
. Automapper
works fine for me every time. I cannot check it on MVC 3
or older than MVC 5
.
我是ASP的开发人员。净MVC 5。自动贩卖机每次对我都很好用。我不能在MVC 3或更老的MVC 5上检查它。