wpf mysql 框架_带有 WPF 和实体框架6的简单数据应用 - Visual Studio | Microsoft Docs

使用 WPF 和 Entity Framework 6 创建简单的数据应用程序Create a simple data application with WPF and Entity Framework 6

08/22/2017

本文内容

本演练演示如何在 Visual Studio 中创建基本的 "窗体 over 数据" 应用程序。This walkthrough shows how to create a basic "forms over data" application in Visual Studio. 应用使用 SQL Server LocalDB、Northwind 数据库、实体框架 6 (不 Entity Framework Core) 和 Windows Presentation Foundation .NET Framework (.NET Core) 。The app uses SQL Server LocalDB, the Northwind database, Entity Framework 6 (not Entity Framework Core), and Windows Presentation Foundation for .NET Framework (not .NET Core). 它演示了如何使用大纲-详细信息视图进行基本数据绑定,还提供了一个自定义绑定导航器,其中包含用于 移动 的按钮、" 上 移"、"移 至开头 "、" 结束 "、" 更新 " 和 " 删除 "。It shows how to do basic databinding with a master-detail view, and it also has a custom Binding Navigator with buttons for Move Next , Move Previous , Move to beginning , Move to end , Update and Delete.

本文重点介绍如何在 Visual Studio 中使用数据工具,并且不会尝试深入了解底层技术。This article focuses on using data tools in Visual Studio, and does not attempt to explain the underlying technologies in any depth. 假设您基本熟悉 XAML、实体框架和 SQL。It assumes that you have a basic familiarity with XAML, Entity Framework, and SQL. 此示例还不演示模型-视图-ViewModel (MVVM) 体系结构,这是 WPF 应用程序的标准。This example also does not demonstrate Model-View-ViewModel (MVVM) architecture, which is standard for WPF applications. 不过,你可以将此代码复制到你自己的 MVVM 应用程序中,只需修改少量内容。However, you can copy this code into your own MVVM application with few modifications.

安装并连接到 NorthwindInstall and connect to Northwind

此示例使用 SQL Server Express LocalDB 和 Northwind 示例数据库。This example uses SQL Server Express LocalDB and the Northwind sample database. 如果该产品的 ADO.NET 数据提供程序支持实体框架,则它也适用于其他 SQL 数据库产品。If the ADO.NET data provider for that product supports Entity Framework, it should work with other SQL database products just as well.

如果没有 SQL Server Express 的 LocalDB,请从 SQL Server Express 下载 "页或通过 Visual Studio 安装程序 安装它。If you don't have SQL Server Express LocalDB, install it either from the SQL Server Express download page, or through the Visual Studio Installer. 在 Visual Studio 安装程序中,可将 SQL Server Express LocalDB 作为 .NET 桌面开发工作负载的一部分安装,也可作为单独组件安装。In the Visual Studio Installer , you can install SQL Server Express LocalDB as part of the .NET desktop development workload or as an individual component.

按照以下步骤安装 Northwind 示例数据库:Install the Northwind sample database by following these steps:

在 Visual Studio 中,打开 " SQL Server 对象资源管理器 " 窗口。In Visual Studio, open the SQL Server Object Explorer window. ( SQL Server 对象资源管理器 在 Visual Studio 安装程序 的 数据存储和处理 工作负荷中安装。 ) 展开 SQL Server 节点。( SQL Server Object Explorer is installed as part of the Data storage and processing workload in the Visual Studio Installer.) Expand the SQL Server node. 右键单击 LocalDB 实例,然后选择 " 新建查询 "。Right-click on your LocalDB instance and select New Query.

此时将打开查询编辑器窗口。A query editor window opens.

此 T-sql 脚本从头开始创建 Northwind 数据库,并用数据填充它。This T-SQL script creates the Northwind database from scratch and populates it with data.

将 T-sql 脚本粘贴到查询编辑器中,然后选择 " 执行 " 按钮。Paste the T-SQL script into the query editor, and then choose the Execute button.

一小段时间后,查询将完成运行,并创建 Northwind 数据库。After a short time, the query finishes running and the Northwind database is created.

为 Northwind添加新连接。

配置项目Configure the project

在 Visual Studio 中,创建一个新的 c # WPF 应用程序 项目。In Visual Studio, create a new C# WPF App project.

为实体框架6添加 NuGet 包。Add the NuGet package for Entity Framework 6. 在 解决方案资源管理器 中,选择 "项目" 节点。In Solution Explorer , select the project node. 在主菜单中,选择 " 项目 " " > 管理 NuGet 包 "。In the main menu, choose Project > Manage NuGet Packages.

2cc7febbcf4c07622aa5f7fba24458ea.png

在 NuGet 包管理器 中,单击 " 浏览 " 链接。In the NuGet Package Manager , click on the Browse link. 实体框架可能是列表中的顶层包。Entity Framework is probably the top package in the list. 单击右窗格中的 " 安装 ",并按照提示进行操作。Click Install in the right pane and follow the prompts. "输出" 窗口将显示安装完成的时间。The Output window tells you when the install is finished.

15e88f19f8e90fad90aa1e3801be3e7c.png

现在,可以使用 Visual Studio 创建基于 Northwind 数据库的模型。Now you can use Visual Studio to create a model based on the Northwind database.

创建模型Create the model

右键单击 " 解决方案资源管理器 中的项目节点,然后选择" 添加 > 新项 "。Right-click on the project node in Solution Explorer and choose Add > New Item. 在左窗格中的 "c #" 节点下,选择 " 数据 ",然后在中间窗格中选择 " ADO.NET 实体数据模型 "。In the left pane, under the C# node, choose Data and in the middle pane, choose ADO.NET Entity Data Model.

66c6aa573c4d8b5ca962fc9dd3d1076b.png

调用模型 Northwind_model ,然后选择 "确定" 。Call the model Northwind_model and choose OK. 实体数据模型向导 将打开。The Entity Data Model Wizard opens. 从数据库中选择 EF 设计器 ,然后单击 " 下一步 "。Choose EF Designer from database and then click Next.

366b126f535891ceeb5036b36f3a6208.png

在下一个屏幕中,输入或选择 LocalDB Northwind 连接 (例如, (LocalDB) \MSSQLLocalDB) ,指定 Northwind 数据库,然后单击 " 下一步 "。In the next screen, enter or choose your LocalDB Northwind connection (for example, (localdb)\MSSQLLocalDB), specify the Northwind database, and click Next.

在向导的下一页上,选择要包含在实体框架模型中的表、存储过程和其他数据库对象。In the next page of the wizard, choose which tables, stored procedures, and other database objects to include in the Entity Framework model. 展开树视图中的 "dbo" 节点,然后选择 " 客户 "、" 订单 " 和 " 订单详细信息 "。Expand the dbo node in the tree view and choose Customers , Orders , and Order Details. 保留默认选中状态,并单击 " 完成 "。Leave the defaults checked and click Finish.

03e41d03ba1c5031d994aeb635ae5589.png

向导将生成表示实体框架模型的 c # 类。The wizard generates the C# classes that represent the Entity Framework model. 类是普通的旧 c # 类,并且是我们在 WPF 用户界面中进行 databind 的。The classes are plain old C# classes and they are what we databind to the WPF user interface. .Edmx 文件描述了关联类与数据库中的对象的其他元数据。The .edmx file describes the relationships and other metadata that associates the classes with objects in the database. Tt

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值