C# 实体框架 - 生成类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10126871/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 12:32:18  来源:igfitidea点击:

Entity Framework - Generating Classes

c#entity-framework

提问by JavaScript Developer

I have an existing database. I was hoping there was a way to generate class files from this database. However, I seem to see a lot of generating the database from the class files.

我有一个现有的数据库。我希望有一种方法可以从此数据库生成类文件。但是,我似乎看到很多从类文件生成数据库。

Is there a way to generate class files from an existing database using the Entity Framework? If so how? Can someone point me to a tutorial?

有没有办法使用实体框架从现有数据库生成类文件?如果是这样怎么办?有人可以给我指点教程吗?

采纳答案by algreat

1) First you need to generate EDMXmodel using your database. To do that you should add new item to your project:

1) 首先,您需要EDMX使用数据库生成模型。为此,您应该向项目添加新项目:

  • Select ADO.NET Entity Data Modelfrom the Templates list.
  • On the Choose Model Contents page, select the Generate from Database option and click Next.
  • Choose your database.
  • On the Choose Your Database Objects page, check the Tables. Choose Views or Stored Procedures if you need.
  • ADO.NET Entity Data Model从模板列表中选择。
  • 在选择模型内容页面上,选择从数据库生成选项并单击下一步。
  • 选择您的数据库。
  • 在选择您的数据库对象页面上,检查表。如果需要,请选择“视图”或“存储过程”。

So now you have Model1.edmxfile in your project.

所以现在你Model1.edmx的项目中有文件。

2) To generate classes using your model:

2)使用您的模型生成类:

  • Open your EDMXmodel designer.
  • On the design surface Right Click –> Add Code Generation Item…
  • Select Online templates.
  • Select EF 4.x DbContext Generator for C#.
  • Click ‘Add'.
  • 打开您的EDMX模型设计器。
  • 在设计图面上右键单击-> 添加代码生成项...
  • 选择在线模板。
  • 选择EF 4.x DbContext Generator for C#
  • 点击“添加”。

Notice that two items are added to your project:

请注意,您的项目中添加了两个项目:

  • Model1.tt(This template generates very simple POCO classes for each entity in your model)
  • Model1.Context.tt(This template generates a derived DbContext to use for querying and persisting data)
  • Model1.tt(此模板为模型中的每个实体生成非常简单的 POCO 类)
  • Model1.Context.tt(此模板生成派生的 DbContext 以用于查询和持久化数据)

3) Read/Write Data example:

3) 读/写数据示例:

 var dbContext = new YourModelClass(); //class derived from DbContext
 var contacts = from c in dbContext.Contacts select c; //read data
 contacts.FirstOrDefault().FirstName = "Alex"; //edit data
 dbContext.SaveChanges(); //save data to DB

Don't forget that you need 4.x version of EntityFramework. You can download EF 4.1 here: Entity Framework 4.1.

不要忘记您需要 EntityFramework 的 4.x 版本。您可以在此处下载 EF 4.1:实体框架 4.1

回答by algreat

I found very nice solution. Microsoft released a beta version of Entity Framework Power Tools: Entity Framework Power Tools Beta 2

我找到了非常好的解决方案。微软发布Entity Framework Power Tools 测试版Entity Framework Power Tools Beta 2

There you can generate POCO classes, derived DbContext and Code First mapping for an existing database in some clicks. It is very nice!

在那里,您可以通过单击为现有数据库生成 POCO 类、派生的 DbContext 和 Code First 映射。这是很不错的!

After installation some context menu options would be added to your Visual Studio.

安装后,一些上下文菜单选项将添加到您的 Visual Studio。

Right-click on a C# project. Choose Entity Framework-> Reverse Engineer Code First (Generates POCO classes, derived DbContext and Code First mapping for an existing database):

右键单击 C# 项目。选择 Entity Framework-> Reverse Engineer Code First(为现有数据库生成 POCO 类、派生 DbContext 和 Code First 映射):

Visual Studio Context Menu

Visual Studio 上下文菜单

Then choose your database and click OK. That's all! It is very easy.

然后选择您的数据库并单击确定。就这样!这很容易。

回答by Shubh

  1. Open the EDMX model
  2. Right click -> Update Model from Browser -> Stored Procedure -> Select your stored procedure -> Finish
  3. See the Model Browser popping up next to Solution Explorer.
  4. Go to Function Imports -> Right click on your Stored Procedure -> Add Function Import
  5. Select the Entities under Return a Collection of -> Select your Entity name from the drop down
  6. Build your Solution.
  1. 打开 EDMX 模型
  2. 右键单击 -> 从浏览器更新模型 -> 存储过程 -> 选择您的存储过程 -> 完成
  3. 查看解决方案资源管理器旁边弹出的模型浏览器。
  4. 转到函数导入 -> 右键单击​​您的存储过程 -> 添加函数导入
  5. 选择 Return a Collection of 下的实体 -> 从下拉列表中选择您的实体名称
  6. 构建您的解决方案。

回答by Nolm? Informatique

EDMX model won't work with EF7 but I've found a Community/Professional product which seems to be very powerfull : http://www.devart.com/entitydeveloper/editions.html

EDMX 模型不适用于 EF7,但我发现了一个社区/专业产品,它似乎非常强大:http: //www.devart.com/entitydeveloper/editions.html