This topic demonstrates how to create a simple XAF application with a business model in a DbContext context.

本主题演示如何在 DbContext 上下文中使用业务模型创建简单的 XAF 应用程序。

Note 注意
  • This topic demonstrates the code that can be generated automatically by the Solution Wizard. Proceed, if you want to implement the demonstrated functionality in the existing XAF solution. If you are creating a new XAF solution, use the wizard instead.
  • A more complex example is provided in the EFDemoCodeFirst application that is shipped with XAF.
  • 本主题演示解决方案向导可以自动生成的代码。如果要在现有 XAF 解决方案中实现演示的功能,请继续。如果要创建新的 XAF 解决方案,请使用向导。
  • 在随 XAF 附带的 EFDemoCodeFirst 应用程序中提供了更复杂的示例。
Tip 提示
A complete sample project is available in the DevExpress Code Examples database at http://www.devexpress.com/example=E4375
完整的示例项目可在 DevExpress 代码示例数据库中找到,http://www.devexpress.com/example=E4375

.

1. Create an XAF Application

1. 创建到 XAF 应用程序

Create a new XAF solution called MySolution using the DevExpress v19.2 XAF Solution Wizard. Select Entity Framework Code First at the Choose ORM step and click Finish.

使用 DevExpress v19.2 XAF 解决方案向导创建新的 XAF 解决方案,称为 MySolution。在"选择 ORM"步骤中首先选择实体框架代码,然后单击"完成"。

2. Add the Entity Data Model and Context

2. 添加实体数据模型和上下文

In this topic, we will not describe entities and context implementation in detail, as it is already described in MSDN (see Code First to a New Database ). Here, we assume that you are already familiar with creating an EF data model in code.

在本主题中,我们将不详细描述实体和上下文实现,正如 MSDN 中已经描述的那样(请参阅新数据库的代码优先)。在这里,我们假设您已经熟悉在代码中创建 EF 数据模型。

  • In the module project, implement the following Employee and Task classes.

  • 在模块项目中,实现以下员工和任务类。
    Employee (C#)
Task (C#)
Employee (VB.NET)
Task (VB.NET) using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using DevExpress.Persistent.Base;
// ...
[DefaultClassOptions, ImageName("BO_Employee")]
public class Employee : INotifyPropertyChanged
{
private int id;
[Browsable(false)]
public int Id {
get { return id; }
protected set {
if (id != value) {
id = value;
OnPropertyChanged();
}
}
}
private string firstName;
public string FirstName {
get { return firstName; }
set {
if (firstName != value) {
firstName = value;
OnPropertyChanged();
}
}
}
private string lastName;
public string LastName {
get { return lastName; }
set {
if (lastName != value) {
lastName = value;
OnPropertyChanged();
}
}
}
private DateTime? birthday;
public DateTime? Birthday {
get { return birthday; }
set {
if (birthday != value) {
birthday = value;
OnPropertyChanged();
}
}
}
private List<Task> tasks;
public virtual List<Task> Tasks {
get { return tasks; }
set {
if (tasks != value) {
tasks = value;
OnPropertyChanged();
}
}
}
#region INotifyPropertyChanged members
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string propertyName = null) {
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
}

The INotify Property Changed interface allows you to receive notifications from business classes when their property values are changed.

INotify 属性更改界面允许您在业务类的属性值发生更改时接收来自业务类的通知。

In this code, you see the use of Data Annotations that influences UI generation - DefaultClassOptionsAttribute, ImageNameAttribute and FieldSizeAttribute, as well as the standard .NET Browsable

在此代码中,您将看到影响 UI 生成的数据注释的使用 - 默认类选项属性、图像名称属性和字段Size属性,以及标准 .NET 可浏览

  • attribute. As a result, the Employee and Task navigation items will be created, icons from the built-in image library will be used, the multiline editor will be displayed for the Task.Description property, and the service Id properties will be invisible in UI.

  • 属性。因此,将创建员工和任务导航项,使用内置图像库中的图标,将显示 Task.描述属性的多行编辑器,并且服务 Id 属性将在 UI 中不可见。
    Note 注意
    You can use EF Power Tools to reverse engineer an existing database instead of writing code manually.
    您可以使用 EF Power Tools 对现有数据库进行反向工程,而不是手动编写代码。
  • Implement the following DbContext descendant.

  • 实现以下 DbContext 后代。

    using System.Data.Entity;
    // ...
    public class MyDbContext : DbContext {
    public MyDbContext(string connectionString) : base(connectionString) { }
    public DbSet<Employee> Employees { get; set; }
    public DbSet<Task> Tasks { get; set; }
    }

Note that the MyDBContext class should implement a constructor that takes the connectionString string parameter. This constructor will be called by the EFObjectSpaceProvider Object Space Provider internally.

请注意,MyDBContext 类应实现采用连接字符串字符串参数的构造函数。此构造函数将由 EFObjectSpaceProvider 对象空间提供程序内部调用。

3. Use the Entity Framework Object Space Provider

3. 使用实体框架对象空间提供程序

To use the EFObjectSpace instances to access data in your application, modify the default implementation of the CreateDefaultObjectSpaceProvider method located in WinApplication.cs (WinApplication.vb) and WebApplication.cs (WebApplication.vb) files. For details on this code, refer to the Use the Entity Framework Data Model topic.

要使用 EFObjectSpace 实例访问应用程序中的数据,请修改位于WinApplication.cs (WinApplication.vb) 和WebApplication.cs (WebApplication.vb) 文件中的"创建默认对象空间提供程序"方法的默认实现。有关此代码的详细信息,请参阅使用实体框架数据模型主题。

using DevExpress.ExpressApp.DC;
using DevExpress.ExpressApp.EF;
// ...
protected override void CreateDefaultObjectSpaceProvider(CreateCustomObjectSpaceProviderEventArgs args) {
args.ObjectSpaceProvider = new EFObjectSpaceProvider(typeof(MyDbContext), args.ConnectionString);
}

4. Specify the Connection String to the Database

4. 指定到数据库的连接字符串

To connect the database that was generated for your data model, specify the XafApplication.ConnectionString property in code or use the corresponding option in the App.config and Web.config files located in the WinForms and ASP.NET application projects. Refer to the Connect an XAF Application to a Database Provider topic for details.

要连接为数据模型生成的数据库,请在代码中指定 XafApplication.ConnectionString 属性,或在 WinForms 和ASP.NET应用程序项目中的 App.config 和 Web.config 文件中使用相应的选项。有关详细信息,请参阅将 XAF 应用程序连接到数据库提供程序主题。

5. Run the Application

5. 运行应用程序

Now you can run both the Windows Forms and ASP.NET applications. You will see that a UI is automatically generated for your data model.

现在,您可以同时运行 Windows 窗体和ASP.NET应用程序。您将看到自动为数据模型生成 UI。

Windows Forms:

窗口窗体:

ASP.NET:

Note 注意
To learn how to fill the database with predefined data, refer to the How to: Supply Initial Data for the Entity Framework Data Model topic.
要了解如何使用预定义数据填充数据库,请参阅"如何:为实体框架数据模型提供初始数据"主题。

How to: Use the Entity Framework Code First in XAF 如何:在 XAF 中使用EF CodeFirst的更多相关文章

  1. Entity Framework Code First学习系列目录

    Entity Framework Code First学习系列说明:开发环境为Visual Studio 2010 + Entity Framework 5.0+MS SQL Server 2012, ...

  2. Entity Framework Code First数据库连接

    1. 安装Entity Framework 使用NuGet安装Entity Framework程序包:工具->库程序包管理器->程序包管理器控制台,执行以下语句: PM> Insta ...

  3. Entity Framework Code First属性映射约定

    Entity Framework Code First与数据表之间的映射方式有两种实现:Data Annotation和Fluent API.本文中采用创建Product类为例来说明tity Fram ...

  4. Entity Framework Code First关系映射约定

    本篇随笔目录: 1.外键列名默认约定 2.一对多关系 3.一对一关系 4.多对多关系 5.一对多自反关系 6.多对多自反关系 在关系数据库中,不同表之间往往不是全部都单独存在,而是相互存在关联的.两个 ...

  5. Entity Framework Code First执行SQL语句、视图及存储过程

    1.Entity Framework Code First查询视图 Entity Framework Code First目前还没有特别针对View操作的方法,但对于可更新的视图,可以采用与Table ...

  6. Entity Framework Code First使用DbContext查询

    DbContext.DbSet及DbQuery是Entity Framework Code First引入的3个新的类,其中DbContext用于保持数据库会话连接,实体变化跟踪及保存,DbSet用于 ...

  7. Entity Framework Code First添加修改及删除单独实体

    对于一个单独实体的通常操作有3种:添加新的实体.修改实体以及删除实体. 1.添加新的实体 Entity Framework Code First添加新的实体通过调用DbSet.Add()方法来实现. ...

  8. Entity Framework Code First实体对象变动跟踪

    Entity Framework Code First通过DbContext.ChangeTracker对实体对象的变动进行跟踪,实现跟踪的方式有两种:变动跟踪快照和变动跟踪代理. 变动跟踪快照:前面 ...

  9. 旧项目如何切换到Entity Framework Code First

    Entity Framework Code First固然是好东西,然而如果是已经存在的旧有项目,如何简单方便的使用切换呢? 这里介绍一个VS的插件Entity Framework Power Too ...

  10. Entity Framework Code First (三)Data Annotations

    Entity Framework Code First 利用一种被称为约定(Conventions)优于配置(Configuration)的编程模式允许你使用自己的 domain classes 来表 ...

随机推荐

  1. 阿里巴巴 Service Mesh 落地的架构与挑战

    点击下载<不一样的 双11 技术:阿里巴巴经济体云原生实践> 本文节选自<不一样的 双11 技术:阿里巴巴经济体云原生实践>一书,点击上方图片即可下载! 作者 | 方克明(溪翁 ...

  2. Eclipse 安装 ShellEd 不成功的解决办法

    我是Win7, 64位的操作系统,Eclipse 版本是 32位的eclipse-java-luna-SR1-win32.在安装 ShellEd 时,一直安装不成功,前后出现两种现象: 1. 在按照网 ...

  3. ZooKeeper初步

    ZooKeeper 简介: 顾名思义 zookeeper 就是动物园管理员,他是用来管 hadoop(大象).Hive(蜜蜂).pig(小猪)的管理员, Apache Hbase 和 Apache S ...

  4. [TimLinux] JavaScript 阻止父节点接收子节点事件的方法

    1. 事件 两种类型的事件:触发式.冒泡式 2. 冒泡式 触发式:事件从DOM结构的顶层往下走的事件触发过程: 冒泡式:事件从DOM结构的底层往上走的事件触发过程. 3. 父子节点 当父.子节点同时对 ...

  5. centos7下ldap+kerberos实现单点登陆

    一. LDAP概念 http://wiki.jabbercn.org/index.php/OpenLDAP2.4%E7%AE%A1%E7%90%86%E5%91%98%E6%8C%87%E5%8D%9 ...

  6. 云服务器+域名+hexo 搭建博客

    1 阿里云服务器安全组规则中启用80,4000,22端口, 记得出方向也要设置,否则... 2 域名指向服务器ip 3 安装git yum install git 4 安装node.js 下载地址为: ...

  7. for-in的缺点

    for-in用来循环对象中的属性,但是通过for-in循环输出的属性名的顺序是不可测的.具体来说,所有属性都会被返回一次,但返回的先后次序可能会因浏览器而异. ES5之前:如果表示要迭代的对象的变量值 ...

  8. 【RabbitMQ】显示耗时处理进度

    [RabbitMQ]显示耗时处理进度 通过网页提交一个耗时的请求,然后启动处理线程,请求返回.处理线程每完成一部分就给前台推送完成的数量,前端显示进度. 依赖jar <?xml version= ...

  9. 【设计模式】单例模式-Singleton

    [设计模式]单例模式-SingletonEnsure a class has only one instance, and provide a global point to access of it ...

  10. GoLand相同目录(包)下方法调用

    之前写的python,摸索go的时候发现相同目录下写的方法调用,在编译时竟然会报错,Mark~ 相同目录下方法调用,main.go文件调用tool.go中的add方法,要点如下: 1.两个文件的pak ...