简体   繁体   English

实体框架 - 生成类

[英]Entity Framework - Generating Classes

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? 有没有办法使用Entity Framework从现有数据库生成类文件? If so how? 如果是这样的话? Can someone point me to a tutorial? 有人能指点我一个教程吗?

1) First you need to generate EDMX model using your database. 1)首先,您需要使用数据库生成EDMX模型。 To do that you should add new item to your project: 为此,您应该向项目添加新项目:

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

So now you have Model1.edmx file in your project. 所以现在你的项目中有Model1.edmx文件。

2) To generate classes using your model: 2)使用您的模型生成类:

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

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.tt (此模板为模型中的每个实体生成非常简单的POCO类)
  • Model1.Context.tt (This template generates a derived DbContext to use for querying and persisting data) 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. 不要忘记您需要4.x版本的EntityFramework。 You can download EF 4.1 here: Entity Framework 4.1 . 您可以在此处下载EF 4.1: Entity Framework 4.1

I found very nice solution. 我找到了很好的解决方案。 Microsoft released a beta version of Entity Framework Power Tools: Entity Framework Power Tools Beta 2 Microsoft发布了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. 在那里,您可以通过一些点击为现有数据库生成POCO类,派生DbContext和Code First映射。 It is very nice! 这是很不错的!

After installation some context menu options would be added to your Visual Studio. 安装后,一些上下文菜单选项将添加到Visual Studio中。

Right-click on a C# project. 右键单击C#项目。 Choose Entity Framework-> Reverse Engineer Code First (Generates POCO classes, derived DbContext and Code First mapping for an existing database): 选择Entity Framework-> Reverse Engineer Code First(为现有数据库生成POCO类,派生DbContext和Code First映射):

Visual Studio上下文菜单

Then choose your database and click OK. 然后选择数据库并单击“确定”。 That's all! 就这样! It is very easy. 这很容易。

  1. Open the EDMX model 打开EDMX模型
  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. 构建解决方案。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM