ADO .net实体框架从代码背后自动生成模型 [英] ADO .net entity framework Autogenerate model from code behind

查看:86
本文介绍了ADO .net实体框架从代码背后自动生成模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将ADO .net实体框架添加到我的项目中,因此我看到了EDMX文件.当我添加它时,有一个弹出窗口(实体设计器???)要求我选择一个数据源,并且我已经读过,如果指向一个现有的数据库,必要的对象类/实体将在我的项目中自动生成. .但是,我关闭了对话,以为以后再设置数据源.那么在关闭对话框后,如何设置数据源并立即使其自动生成实体?

有没有一种方法可以使ADO .net实体框架使用生成前/生成后事件自动生成实体,或者可以从后面的代码中自动生成实体?
如果我将空白的edmx嵌入到类库中并编译该库,然后在我的主(父)项目中引用该库,该如何通过编写代码通过代码自动生成与父项目数据库相对应的实体,该怎么办?像:

I have added ADO .net Entity Framework to my project and so I see the EDMX file. When I added it, there was this pop-up (Entity Designer???) asking me to choose a datasource, and I had read that if pointed to an existing DB, the necessary Object classes/entities will be auto generated in my project. But, I closed the dialogue thinking that I will set the datasource later. So how do I set the datasource and get it to autogenerate the entities now, after closing the dialogue?

Is there a way to get ADO .net entity framework to autogenerate the entities using a pre/post build event, or maybe from code behind?
What if I embed a blank edmx into a class library and compile the library, then refer the library in my main (parent) project, can I get it to autogenerate the entities corrosponding to the parent project''s database through code by writing something like:

myLibrary.myEntityModel.functionToAutoGenerateEntities();


在父项目的开始/入口点的某个位置,然后运行父项目,以便执行该项目并创建必要的文件,之后我注释掉上一行,以便后续的构建和运行不会再次生成/修改实体? br/>
OP的其他信息从下面的非解决方案中移出
亲爱的prataph,
我的是数据库优先方法.
说,我有一个数据库,其中有一个名为person的表.
它有2列:
VARCHAR姓氏& INT年龄

现在,在我们的程序中,我们将创建一个对象类
Person.cs


somewhere in the parent project start/entry point, then run the parent project so that this gets executed and creates the necessary files, after which I comment out the above line so that successive builds and runs do not generate/modify the entities again?

OP''s additional information moved from non-solution below
dear prataph,
Mine is a DB first approach.
Say, i have a DB with a table named person.
It has 2 columns:
VARCHAR FirstName & INT Age

Now, in our program, we will create an object class
Person.cs

Class Person
{
  string _firstName;
  int _age;

  public string FirstName
  {
    get{return _firstName;}
    set{_firstName = value}
  }

  public string Age
  {
    get{return _age;}
    set{_age = value}
  }
}


或类似的东西.当数据库很复杂并且有许多表和列时,创建这些类变得很困难.

我在某处读到ADO .Net Entity Framework可以自动为您生成这些对象/类.cs文件.这种方式使开发人员的工作更轻松,并且他/她的时间更加高效.

如何从后面的代码中调用ADO .net的自动生成对象类"功能?
还是只能在Visual Studio UI中通过右键单击或添加新的.edmx文件来调用它?


or something similar. When the DB is complex and has many tables and columns, it gets difficult to create these classes.

I read somewhere that ADO .Net Entity Framework can automatically generate these object/class .cs files for you. That kind of makes a developers work easier and his/her time more productive...

How can this ''autogenerate object class'' functionality of ADO .net be called from code behind?
Or can it be called only from the visual studio UI by right-click or when adding a new .edmx file?

推荐答案

如果要使用现有数据库表在您的项目中,然后无需使用自动生成实体,而无需使用将.EDMX文件添加到项目中时弹出的设计器向导.

因此,如果选择一个空的.EDMX文件,请右键单击设计器,然后单击从数据库更新模型",以便可以在需要时选择所需的表和存储过程.

下面,我解释了如何创建实体,并为初学者提供了一个网站,如有需要,请通过 http://www.entityframeworktutorial.net/ [^ ]


您可以使用Entity Framework 4.1拥有多种建模技术,例如代码优先,模型优先或数据库优先.


代码优先------------模型优先-------数据库优先


代码优先-http://blogs.msdn.com/b/adonet/archive/2009/06/22/feature-ctp-walkthrough-code-only-for-the-entity-framework.aspx
-------------------

在代码优先"方法中,避免完全使用可视模型设计器(EDMX).您首先编写POCO类,然后根据这些POCO类创建数据库.遵循域驱动设计(DDD)原理的开发人员更喜欢先对类进行编码,然后生成持久化数据所需的数据库.

要了解的重要一件事是为代码优先方法引入了两种新类型,即DbContext和DbSet. DbContext是ObjectContext的简化替代方案,并且是使用特定模型与数据库进行交互的主要对象. DbSet(Of TEntity)是ObjectSet(Of TEntity)的简化替代方案,用于在Code First方法中针对模型中的特定类型执行CRUD操作.

型号优先
----------------------

在模型优先"方法中,您可以直接在EDMX的设计图面上创建实体,关系和继承层次结构.因此,在模型优先"方法中,添加ADO.NET实体数据模型时,应选择空模型"而不是从数据库生成".

在空模型的设计图面上创建所需的实体,关联和继承之后,您可以使用设计器的上下文菜单选项从模型生成数据库".但是,不要与名称混淆.它不会从模型生成新的数据库.它只会让您在现有数据库中执行DDL.

数据库优先
----------------------------

我们在第一个EDM示例中已经看到了这种方法,该示例从现有数据库创建了模型和类.因此,当您从现有数据库生成EDMX时,这是数据库优先"的方法.
If you want to use the existing database tables in your project then forget about AutoGenerating Entities without using the designer wizard which pops up when .EDMX file is added to the project.

So if you choose an empty .EDMX file, then right click on the designer and click on Update model from database so that you can choose the required tables and stored procedures when needed.

Below i had explained how to create entities and there is a site for beginners, if needed go through it http://www.entityframeworktutorial.net/[^]


You can have multiple modeling techniques using Entity Framework 4.1 like code first, model first or database first.


Code First ------------ Model First ------- Database first


CODE FIRST -http://blogs.msdn.com/b/adonet/archive/2009/06/22/feature-ctp-walkthrough-code-only-for-the-entity-framework.aspx
-------------------

In Code First approach, you avoid working with visual model designer (EDMX) completely. You write your POCO classes first and then create database from these POCO classes. Developers who follow the path of Domain-Driven Design (DDD) principles prefer to begin by coding their classes first and then generating the database required to persist their data.

One important thing to understand is that there are two new types introduced for Code First approach, DbContext and DbSet. DbContext is a simplified alternative to ObjectContext and is the primary object for interacting with a database using a specific model. DbSet(Of TEntity) is a simplified alternative to ObjectSet(Of TEntity) and is used to perform CRUD operations against a specific type from the model in Code First approach.

MODEL FIRST
----------------------

In Model First approach, you create Entities, relationships, and inheritance hierarchies directly on the design surface of EDMX. So in Model First approach, when you add ADO.NET Entity Data Model, you should select ‘Empty Model’ instead of ‘Generate from database’.

After creating required entities, associations and inheritance on design surface of the empty model, you can use designer’s context menu option ‘Generate database from model’. However don’t get confused with name. it will not generate new database from model. It will only give you DDL to execute in existing database.

DATABASE FIRST
----------------------------

We have seen this approach in our first EDM sample where we created model and classes from existing database. So when you generate EDMX from existing database then it is a Database First approach.


这篇关于ADO .net实体框架从代码背后自动生成模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆