Model FirstModel First

此视频和分步演练提供使用实体框架 Model First 开发的简介。This video and step-by-step walkthrough provide an introduction to Model First development using Entity Framework. Model First 允许使用 Entity Framework Designer 创建新模型,然后从该模型生成数据库架构。Model First allows you to create a new model using the Entity Framework Designer and then generate a database schema from the model. 模型存储在 EDMX 文件中 ( .edmx 扩展名) 并且可以在 Entity Framework Designer 中查看和编辑。The model is stored in an EDMX file (.edmx extension) and can be viewed and edited in the Entity Framework Designer. 在应用程序中与之交互的类将自动从 EDMX 文件生成。The classes that you interact with in your application are automatically generated from the EDMX file.

观看视频Watch the video

此视频和分步演练提供使用实体框架 Model First 开发的简介。This video and step-by-step walkthrough provide an introduction to Model First development using Entity Framework. Model First 允许使用 Entity Framework Designer 创建新模型,然后从该模型生成数据库架构。Model First allows you to create a new model using the Entity Framework Designer and then generate a database schema from the model. 模型存储在 EDMX 文件中 ( .edmx 扩展名) 并且可以在 Entity Framework Designer 中查看和编辑。The model is stored in an EDMX file (.edmx extension) and can be viewed and edited in the Entity Framework Designer. 在应用程序中与之交互的类将自动从 EDMX 文件生成。The classes that you interact with in your application are automatically generated from the EDMX file.

主讲人Rowan MillerPresented By: Rowan Miller

视频wmv | .wmv | wmv (ZIP) Video: WMV | MP4 | WMV (ZIP)

先决条件Pre-Requisites

你将需要安装 Visual Studio 2010 或 Visual Studio 2012 才能完成此演练。You will need to have Visual Studio 2010 or Visual Studio 2012 installed to complete this walkthrough.

如果你使用的是 Visual Studio 2010,你还需要安装 NuGetIf you are using Visual Studio 2010, you will also need to have NuGet installed.

1. 创建应用程序1. Create the Application

为了简单起见,我们将构建一个使用 Model First 执行数据访问的基本控制台应用程序:To keep things simple we’re going to build a basic console application that uses the Model First to perform data access:

  • 打开 Visual StudioOpen Visual Studio
  • 文件- > 新建- > 项目 .。。File -> New -> Project…
  • 从左侧菜单和控制台应用程序选择WindowsSelect Windows from the left menu and Console Application
  • 输入 ModelFirstSample 作为名称Enter ModelFirstSample as the name
  • 选择“确定”Select OK

2. 创建模型2. Create Model

我们将使用在 Visual Studio 中包含的 Entity Framework Designer 来创建模型。We’re going to make use of Entity Framework Designer, which is included as part of Visual Studio, to create our model.

  • 项目- > 添加新项 .。。Project -> Add New Item…

  • 从左侧菜单中选择 " 数据 ",然后 ADO.NET 实体数据模型Select Data from the left menu and then ADO.NET Entity Data Model

  • 输入 BloggingModel 作为名称,然后单击 "确定",这将启动实体数据模型向导Enter BloggingModel as the name and click OK, this launches the Entity Data Model Wizard

  • 选择 "空模型",然后单击 "完成"Select Empty Model and click Finish

    创建空模型

将使用空白模型打开 Entity Framework Designer。The Entity Framework Designer is opened with a blank model. 现在,我们可以开始向模型添加实体、属性和关联。Now we can start adding entities, properties and associations to the model.

  • 右键单击设计图面,然后选择 "属性"Right-click on the design surface and select Properties

  • 在属性窗口将实体容器名称更改为 "bloggingcontext" ,这是将为你生成的派生上下文的名称,上下文表示与数据库的会话,从而使我们能够查询和保存数据In the Properties window change the Entity Container Name to BloggingContext This is the name of the derived context that will be generated for you, the context represents a session with the database, allowing us to query and save data

  • 右键单击设计图面,然后选择 "添加新 > 实体 ... "Right-click on the design surface and select Add New -> Entity…

  • 以 "实体名称" 和 " BlogId " 作为键名称输入博客,然后单击 "确定"Enter Blog as the entity name and BlogId as the key name and click OK

    添加博客实体

  • 右键单击设计图面上的新实体,然后选择 " 添加新 > 标量属性",输入 name 作为属性的名称。Right-click on the new entity on the design surface and select Add New -> Scalar Property, enter Name as the name of the property.

  • 重复此过程以添加 Url 属性。Repeat this process to add a Url property.

  • 右键单击设计图面上的 " Url " 属性,然后选择 "属性",在 "属性窗口将可为 null的设置更改为True这允许我们将博客保存到数据库,而无需向其分配 UrlRight-click on the Url property on the design surface and select Properties, in the Properties window change the Nullable setting to True This allows us to save a Blog to the database without assigning it a Url

  • 使用刚才了解到的技术,添加具有PostId键属性的Post实体Using the techniques you just learnt, add a Post entity with a PostId key property

  • Post实体添加TitleContent标量属性Add Title and Content scalar properties to the Post entity

现在我们有了几个实体,接下来可以在它们之间添加关联 (或关系) 。Now that we have a couple of entities, it’s time to add an association (or relationship) between them.

  • 右键单击设计图面,然后选择 "添加新 > 关联 ... "Right-click on the design surface and select Add New -> Association…

  • 使关系点的一端成为使用1的重数的****博客,另一个终结点的重数, 这意味着,博客包含多篇文章,张贴内容属于一个博客Make one end of the relationship point to Blog with a multiplicity of One and the other end point to Post with a multiplicity of Many This means that a Blog has many Posts and a Post belongs to one Blog

  • 确保选中 "将外键属性添加到 ' Post ' 实体" 框,然后单击 "确定"Ensure the Add foreign key properties to 'Post' Entity box is checked and click OK

    添加 Association MF

现在,我们有了一个简单的模型,我们可以从它生成数据库并使用它来读取和写入数据。We now have a simple model that we can generate a database from and use to read and write data.

模型初始

Visual Studio 2010 中的其他步骤Additional Steps in Visual Studio 2010

如果使用的是 Visual Studio 2010,升级到最新版本的实体框架需要遵循一些额外步骤。If you are working in Visual Studio 2010 there are some additional steps you need to follow to upgrade to the latest version of Entity Framework. 升级很重要,因为它使你能够访问改进的 API 图面,更易于使用,并提供最新的 bug 修复。Upgrading is important because it gives you access to an improved API surface, that is much easier to use, as well as the latest bug fixes.

首先,我们需要从 NuGet 获取最新版本的实体框架。First up, we need to get the latest version of Entity Framework from NuGet.

  • 项目– >管理 NuGet 包 ... 如果你没有 "**管理 NuGet 包 ...* " 选项,则应安装最新版本的 nuget *Project –> Manage NuGet Packages… If you don’t have the Manage NuGet Packages… option you should install the latest version of NuGet
  • 选择 " 联机 " 选项卡Select the Online tab
  • 选择 EntityFrameworkSelect the EntityFramework package
  • 单击“安装” Click Install

接下来,我们需要交换模型,以生成使用 DbContext API 的代码,这些代码是在实体框架的更高版本中引入的。Next, we need to swap our model to generate code that makes use of the DbContext API, which was introduced in later versions of Entity Framework.

  • 在 EF 设计器中右键单击模型的空位置,然后选择 "添加代码生成项 ... "Right-click on an empty spot of your model in the EF Designer and select Add Code Generation Item…

  • 从左侧菜单中选择 " 联机模板 ",然后搜索 DbContextSelect Online Templates from the left menu and search for DbContext

  • 选择适用于 C # 的 EF DbContext 生成器,输入BloggingModel作为名称,然后单击 "添加"Select the EF 5.x DbContext Generator for C#, enter BloggingModel as the name and click Add

    DbContext 模板

3. 生成数据库3. Generating the Database

考虑到我们的模型,实体框架可以计算一个数据库架构,该架构将允许我们使用模型存储和检索数据。Given our model, Entity Framework can calculate a database schema that will allow us to store and retrieve data using the model.

随 Visual Studio 一起安装的数据库服务器因安装的 Visual Studio 版本而异:The database server that is installed with Visual Studio is different depending on the version of Visual Studio you have installed:

  • 如果使用的是 Visual Studio 2010,则将创建 SQL Express 数据库。If you are using Visual Studio 2010 you'll be creating a SQL Express database.
  • 如果使用的是 Visual Studio 2012,则将创建一个 LocalDB 数据库。If you are using Visual Studio 2012 then you'll be creating a LocalDB database.

接下来,生成数据库。Let's go ahead and generate the database.

  • 右键单击设计图面,然后选择 "从模型生成数据库 ... "Right-click on the design surface and select Generate Database from Model…

  • 单击 "新建连接 ... "Click New Connection… 并指定 LocalDB 或 SQL Express,具体取决于所使用的 Visual Studio 的版本,请输入 ModelFirst 作为数据库名称。and specify either LocalDB or SQL Express, depending on which version of Visual Studio you are using, enter ModelFirst.Blogging as the database name.

    LocalDB 连接 MF

    Sql Express 连接 MF

  • 选择 "确定" ,系统会询问您是否要创建新数据库,请选择 "是"Select OK and you will be asked if you want to create a new database, select Yes

  • 选择 " 下一步 ",Entity Framework Designer 将计算用于创建数据库架构的脚本Select Next and the Entity Framework Designer will calculate a script to create the database schema

  • 显示脚本后,单击 " 完成 ",脚本将添加到项目中并打开Once the script is displayed, click Finish and the script will be added to your project and opened

  • 右键单击该脚本,然后选择 " 执行",系统将提示您指定要连接到的数据库,指定 LocalDB 或 SQL Server Express,具体取决于您使用的 Visual Studio 的版本Right-click on the script and select Execute, you will be prompted to specify the database to connect to, specify LocalDB or SQL Server Express, depending on which version of Visual Studio you are using

4. 读取 & 写入数据4. Reading & Writing Data

现在,我们有了一个模型,可以使用它来访问某些数据了。Now that we have a model it’s time to use it to access some data. 要用于访问数据的类将根据 EDMX 文件自动生成。The classes we are going to use to access data are being automatically generated for you based on the EDMX file.

此屏幕快照来自 Visual Studio 2012,如果你使用的是 Visual Studio 2010,则 BloggingModel.tt 和 BloggingModel.Context.tt 文件将直接位于你的项目下,而不是在 EDMX 文件下嵌套。This screen shot is from Visual Studio 2012, if you are using Visual Studio 2010 the BloggingModel.tt and BloggingModel.Context.tt files will be directly under your project rather than nested under the EDMX file.

生成的类

在 Program.cs 中实现 Main 方法,如下所示。Implement the Main method in Program.cs as shown below. 此代码创建一个新的上下文实例,然后使用它来插入新的博客。This code creates a new instance of our context and then uses it to insert a new Blog. 然后,它使用 LINQ 查询从按标题字母顺序排序的数据库中检索所有博客。Then it uses a LINQ query to retrieve all Blogs from the database ordered alphabetically by Title.

class Program
{
    static void Main(string[] args)
    {
        using (var db = new BloggingContext())
        {
            // Create and save a new Blog
            Console.Write("Enter a name for a new Blog: ");
            var name = Console.ReadLine();

            var blog = new Blog { Name = name };
            db.Blogs.Add(blog);
            db.SaveChanges();

            // Display all Blogs from the database
            var query = from b in db.Blogs
                        orderby b.Name
                        select b;

            Console.WriteLine("All blogs in the database:");
            foreach (var item in query)
            {
                Console.WriteLine(item.Name);
            }

            Console.WriteLine("Press any key to exit...");
            Console.ReadKey();
        }
    }
}

你现在可以运行该应用程序并对其进行测试。You can now run the application and test it out.

Enter a name for a new Blog: ADO.NET Blog
All blogs in the database:
ADO.NET Blog
Press any key to exit...

5. 处理模型更改5. Dealing with Model Changes

现在,可以对模型进行一些更改,当我们进行这些更改时,我们还需要更新数据库架构。Now it’s time to make some changes to our model, when we make these changes we also need to update the database schema.

首先向模型添加新的用户实体。We’ll start by adding a new User entity to our model.

  • 添加新的 用户 实体名称,其中 用户名 为密钥名称, 字符串 用作密钥的属性类型Add a new User entity name with Username as the key name and String as the property type for the key

    添加用户实体

  • 右键单击设计图面上的 "用户名" 属性,然后选择 "属性",在 "属性窗口将" MaxLength "设置更改为" 50", 这会将用户名中存储的数据限制为50个字符Right-click on the Username property on the design surface and select Properties, In the Properties window change the MaxLength setting to 50 This restricts the data that can be stored in username to 50 characters

  • 用户实体添加DisplayName标量属性Add a DisplayName scalar property to the User entity

现在,我们有了一个更新的模型,我们可以更新数据库以适应新的用户实体类型。We now have an updated model and we are ready to update the database to accommodate our new User entity type.

  • 右键单击设计图面,然后选择 " 从模型生成数据库 ...",实体框架将计算一个脚本以根据更新后的模型重新创建架构。Right-click on the design surface and select Generate Database from Model…, Entity Framework will calculate a script to recreate a schema based on the updated model.
  • 单击“完成”Click Finish
  • 你可能会收到有关覆盖模型的现有 DDL 脚本以及映射和存储部分的警告,请单击 "是" 以显示这两个警告You may receive warnings about overwriting the existing DDL script and the mapping and storage parts of the model, click Yes for both these warnings
  • 将为您打开用于创建数据库的更新的 SQL 脚本The updated SQL script to create the database is opened for you
    生成的脚本将删除所有现有的表,然后从头开始重新创建该架构。这可能适用于本地开发,但并不适合将更改推送到已部署的数据库。如果需要将更改发布到已部署的数据库,则需要编辑该脚本或使用架构比较工具来计算迁移脚本。The script that is generated will drop all existing tables and then recreate the schema from scratch. This may work for local development but is not a viable for pushing changes to a database that has already been deployed. If you need to publish changes to a database that has already been deployed, you will need to edit the script or use a schema compare tool to calculate a migration script.
  • 右键单击该脚本,然后选择 " 执行",系统将提示您指定要连接到的数据库,指定 LocalDB 或 SQL Server Express,具体取决于您使用的 Visual Studio 的版本Right-click on the script and select Execute, you will be prompted to specify the database to connect to, specify LocalDB or SQL Server Express, depending on which version of Visual Studio you are using

总结Summary

在本演练中,我们介绍 Model First 开发,这允许我们在 EF 设计器中创建模型,然后从该模型生成数据库。In this walkthrough we looked at Model First development, which allowed us to create a model in the EF Designer and then generate a database from that model. 然后,使用该模型从数据库中读取和写入一些数据。We then used the model to read and write some data from the database. 最后,我们更新了模型,然后重新创建了数据库架构以匹配模型。Finally, we updated the model and then recreated the database schema to match the model.