使用实体框架将SQL Server数据库中的表拖到c#中的列表中

时间:2023-01-04 09:44:29

I'm a programmer who just began working in a Windows environment and i'm new to web development and have poor previous database experience. Currently at my internship I was asked to pull the information from a large SQL Server database and store it into a list and make a website to display the information. Being new to most of these concepts, I've been reading up on MVC, Linq, and EF since that is what they primarily use.

我是一名程序员,刚开始在Windows环境下工作,我对web开发很陌生,以前的数据库经验也很差。目前,在实习期间,我被要求从一个大型SQL Server数据库中提取信息,并将其存储到一个列表中,并制作一个网站来显示这些信息。作为这些概念的新成员,我一直在阅读MVC、Linq和EF,因为它们主要使用这些概念。

I am trying to figure out how to pull the table into a list, but so far the internet hasn't been much help. Most solutions are for a database you create in the project or they use the CodeFirst method. The solution i'm working on in Visual Studio (2012) is MASSIVE and the database is already included in the project.

我正在想办法把这个表格列成一个列表,但是到目前为止,互联网并没有多大帮助。大多数解决方案是针对您在项目中创建的数据库,或者使用CodeFirst方法。我在Visual Studio(2012)中正在开发的解决方案非常庞大,并且项目中已经包含了数据库。

From what I gathered, I should make a class for the table and make an object for each column and then store that class in a List<>. The part of querying the database and storing all the elements is what eludes me. I've been reading for a few days, but keep having trouble grasping exactly how to do it. Should I be using interfaces? (Also, I searched to the source code and they don't really use DbConnect or anything... half the reason i'm confused.)

根据我的收集,我应该为表创建一个类,并为每个列创建一个对象,然后将该类存储在一个列表<>中。查询数据库和存储所有元素的部分让我感到困惑。我已经读了几天书了,但一直不知道怎么做。我应该使用接口吗?(另外,我搜索了源代码,他们并没有真正使用DbConnect之类的东西……)我感到困惑的一半原因。

Sorry that I have to real code to show, but I would appreciate any insight! I can clarify anything you'd like. Thanks!

很抱歉,我必须展示真正的代码,但是我希望能有任何的见解!我可以澄清任何你想要的东西。谢谢!

2 个解决方案

#1


5  

List<YourTable> list;
using (YourDataBaseEntities context = new YourDataBaseEntities())
{
    list = context.YourTables.toList();
}

Will get you every object from the table you specified in your database. YourTable will be the object associated with that table and YourDataBaseEntities will be the object for your database as generated by the Entity Framework model that you have.

将从数据库中指定的表中获取每个对象。您的表将是与该表相关联的对象,您的数据库实体将是由您拥有的实体框架模型生成的数据库对象。

#2


0  

After you create your EDMX, Entity framework will create a class for each table you have on your database.

创建EDMX之后,实体框架将为数据库中的每个表创建一个类。

and you can access the information like this using lambda expressions:

你可以使用lambda表达式来访问这些信息:

List<TableName> Content = context.TableName.Select(x=> x).ToList<TableName>();

#1


5  

List<YourTable> list;
using (YourDataBaseEntities context = new YourDataBaseEntities())
{
    list = context.YourTables.toList();
}

Will get you every object from the table you specified in your database. YourTable will be the object associated with that table and YourDataBaseEntities will be the object for your database as generated by the Entity Framework model that you have.

将从数据库中指定的表中获取每个对象。您的表将是与该表相关联的对象,您的数据库实体将是由您拥有的实体框架模型生成的数据库对象。

#2


0  

After you create your EDMX, Entity framework will create a class for each table you have on your database.

创建EDMX之后,实体框架将为数据库中的每个表创建一个类。

and you can access the information like this using lambda expressions:

你可以使用lambda表达式来访问这些信息:

List<TableName> Content = context.TableName.Select(x=> x).ToList<TableName>();