This topic demonstrates how to use the Model First entity model and a DbContext entity container in an XAF application.

本主题演示如何在 XAF 应用程序中使用模型第一实体模型和 DbContext 实体容器。

1. Add the Entity Data Model

In the Solution Explorer, right-click the MySolution.Module\BusinessObjects folder and create a model as described in the Create Model section of the Model First topic. Specify MySolutionModel as the new file's name. Design a data model that includes the Employee and Task entities, and generate a database from this model. The image below demonstrates the model that will be used in this example.

添加实体数据模型

在"解决方案资源管理器"中,右键单击 MySolution.Module_BusinessObjects 文件夹,并创建模型,如"模型第一"主题的"创建模型"部分中所述。将 MySolutionModel 指定为新文件的名称。设计包含员工和任务实体的数据模型,并使用此模型生成数据库。下图演示了此示例中使用的模型。

Note 注意
  • In this topic, the Entity Framework Designer is not described in detail. For information on using the Entity Framework Designer, refer to the following MSDN article: Model First
  • You can use the Generate from database feature of the Entity Data Model Wizard to reverse engineer an existing database instead of designing a data model manually.
  • 在本主题中,不详细介绍实体框架设计器。有关使用实体框架设计器的信息,请参阅以下 MSDN 文章:模型优先
  • 您可以使用实体数据模型向导的"从数据库生成"功能对现有数据库进行反向工程,而不是手动设计数据模型。

2. Apply Attributes to Entity Classes

XAF automatically collects entities declared in modules, and adds them to the Application Model when the DevExpress.ExpressApp.EF.v19.2.dll or DevExpress.ExpressApp.EF.45.v19.2.dll assembly is referenced. In the Model Editor, you can see List and Detail View nodes created for the Employee and Task objects. You can create navigation items to access these objects in the UI (see Add an Item to the Navigation Control).

将属性应用于实体类

XAF 自动收集模块中声明的实体,并在引用 DevExpress.ExpressApp.ef.v19.2.dll 或 DevExpress.ExpressApp.EF.45.v19.2.dll 程序集时将它们添加到应用程序模型中。在模型编辑器中,您可以看到为"员工"和"任务"对象创建的列表和详细视图节点。您可以创建导航项以在 UI 中访问这些对象(请参阅向导航控件添加项)。

Note 注意
Currently, the metadata information on EF classes and their properties is not propagated to the Model Editor at design time. As a result, tools like Filter Builder cannot display object structure. The workaround is provided in the EF - Display data model properties in the design time Model Editor ticket. We will consider supporting this scenario in future XAF versions.
目前,EF 类及其属性的元数据信息不会在设计时传播到模型编辑器。因此,筛选器生成器等工具无法显示对象结构。解决方法在设计时间模型编辑器票证中的 EF - 显示数据模型属性中提供。我们将考虑在将来的 XAF 版本中支持此方案。
 

However, you may want to customize the UI in code by applying XAF built-in attributes such as DefaultClassOptionsAttribute or ImageNameAttribute. In order to apply attributes to designed entity classes, you must first declare the partial Employee and Task classes that will supplement the designer-generated declarations. Once you have declared these classes, you can apply the required attributes.

但是,您可能希望通过应用 XAF 内置属性(如默认类选项属性或图像名称属性)来自定义代码中的 UI。为了将属性应用于设计中的实体类,必须首先声明将补充设计器生成的声明的部分员工类和任务类。声明这些类后,可以应用所需的属性。

using DevExpress.ExpressApp.DC;
using DevExpress.Persistent.Base;
using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations; namespace MySolution.Module.BusinessObjects { [MetadataType(typeof(EmployeeMetadata))]
[DefaultClassOptions, ImageName("BO_Employee")]
public partial class Employee { }
public class EmployeeMetadata {
[Browsable(false)]
public Int32 Id { get; set; }
} [MetadataType(typeof(TaskMetadata))]
[DefaultClassOptions, ImageName("BO_Task")]
public partial class Task { }
public class TaskMetadata {
[Browsable(false)]
public Int32 Id { get; set; } [FieldSize(FieldSizeAttribute.Unlimited)]
public String Description { get; set; }
}
}

To apply attributes to the Employee and Task object properties (e.g., apply the Browsable(false) attribute to the Id properties to hide the key values in the UI), utilize the approach demonstrated in the How to: Apply Attributes to Entity Properties when Using Model First topic.

要将属性应用于员工和任务对象属性(例如,将可浏览(false)属性应用于 ID 属性以隐藏 UI 中的键值),请使用"如何:使用模型时将属性应用于实体属性"中演示的方法第一个主题。

3. Run the Application

You can now run both WinForms and ASP.NET applications. The UI for each platform is automatically generated for your data model.

运行应用程序

您现在可以同时运行 WinForms 和ASP.NET应用程序。每个平台的 UI 都将自动生成为数据模型。

WinForms:

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 Model First in XAF 如何:在 XAF 中使用EF ModelFirst的更多相关文章

  1. Entity Framework Model First下改变数据库脚本的生成方式

    在Entity Framework Model First下, 一个非常常见的需求是改变数据库脚本的生成方式.这个应用场景是指,当用户在Designer上单击鼠标右键,然后选择Generate Dat ...

  2. Entity Framework 之Database first(数据库优先)&Model First(模型优先)

    一.什么是Entity Framework 1.1 实体框架(EF)是一个对象关系映射器,使.NET开发人员使用特定于域的对象与关系数据.它消除了需要开发人员通常需要编写的大部分数据访问代码.简化了原 ...

  3. C# ORM—Entity Framework 之Database first(数据库优先)&Model First(模型优先)(一)

    一.什么是Entity Framework 1.1 实体框架(EF)是一个对象关系映射器,使.NET开发人员使用特定于域的对象与关系数据.它消除了需要开发人员通常需要编写的大部分数据访问代码.简化了原 ...

  4. 【转】MVC Model建模及Entity Framework Power Tool使用

    MVC如使用Code-First代码优先约定,先建实体类,再根据实体类创建数据库. 在创建实体类后,新建一个数据上下文类,如下: publicclassMusicStoreDB : DbContext ...

  5. [转]Creating an Entity Framework Data Model for an ASP.NET MVC Application (1 of 10)

    本文转自:http://www.asp.net/mvc/overview/older-versions/getting-started-with-ef-5-using-mvc-4/creating-a ...

  6. How to: Supply Initial Data for the Entity Framework Data Model 如何:为EF数据模型提供初始数据

    After you have introduced a data model, you may need to have the application populate the database w ...

  7. ASP.NET MVC with Entity Framework and CSS一书翻译系列文章之第二章:利用模型类创建视图、控制器和数据库

    在这一章中,我们将直接进入项目,并且为产品和分类添加一些基本的模型类.我们将在Entity Framework的代码优先模式下,利用这些模型类创建一个数据库.我们还将学习如何在代码中创建数据库上下文类 ...

  8. Entity Framework 与 面向对象

    说要分享,我了个*,写了一半放草稿箱了两个星期都快发霉了,趁着周末写完发出来吧. 文章分为五部分: 基础.类讲述的是用到的一些EF与面向对象的基础: 业务是讲怎么划分设计业务: 设计模式和工作模式讲述 ...

  9. Entity Framework 6 Recipes 2nd Edition(9-1)译->用Web Api更新单独分离的实体

    第九章 在N层结构的应用程序中使用EF 不是所有的应用都能完全地写入到一个单个的过程中(就是驻留在一个单一的物理层中),实际上,在当今不断发展的网络世界,大量的应用程序的结构包含经典的表现层,应用程, ...

随机推荐

  1. java调用webservice,比较简单方便的方法。

    首先,请同学们自行了解webservice的基础知识. 个人理解,webservice约等于使用http+xml技术进行跨平台的数据交互. http和xml我们都很熟悉了,把他们两个组合到一起就是we ...

  2. 输入URL按下enter键后发生的事

    输入URL按下enter键后浏览器和服务器各自发生的事. 浏览器 1.用户在浏览器中输入URL地址 2.浏览器解析用户输入的URL地址=>域名+端口 3.浏览器检查本地缓存中是否存在这个域名=& ...

  3. 【nodejs原理&源码赏析(1)】Express中间件系统的基本实现

    一直觉得express的中间件系统这种流式处理非常形象,就好像加工流水线一样,每个环节都在针对同一个产品的不同部分完成自己的工作,最后得到一个成品.今天就来实现一个简易的[中间件队列]. 一. API ...

  4. Spring Quartz定时任务设置

    这里主要记录一下定时任务的配置,偏向于记录型的一个教程,这里不阐述Quartz的原理. 首先,在Spring配置文件里配置一个自己写好的一个包含执行任务方法的一个类. <bean id=&quo ...

  5. IOS开发中制作属于自己的静态库.a、资源库.bundle、.framework

    一.什么是库        库实际上是一种代码共享的方式,主要用于代码重用和源码隐藏,通常分为动态库和静态库. 静态库:链接时完整的拷贝至可执行文件中,被多次使用就有多份冗余拷贝. 动态库:链接时不复 ...

  6. Orleans 序列化遇到的坑

    真的是巨坑 搞明白问题的我简直无法用言语来描述我的心情 先上架构图 理想中的架构 服务随便上 网关只负责分发 然后跟随官方教程写遇到了序列化问题 以前有经验,不慌,以前稀里糊涂就搞定了. 再然后遇到一 ...

  7. 贝壳2020——Java校招笔试题

    算法题4道: 题目描述: 给出n个正整数,要求找出相邻两个数字中差的绝对值最小的一对数字,如果有差的绝对值相同的,则输出最前面的一对数.(2<n<=100,正整数都在10^16范围内) 输 ...

  8. 大数据学习笔记——Spark完全分布式完整部署教程

    Spark完全分布式完整部署教程 继Mapreduce之后,作为新一代并且是主流的计算引擎,学好Spark是非常重要的,这一篇博客会专门介绍如何部署一个分布式的Spark计算框架,在之后的博客中,更会 ...

  9. 实习生4面美团Java岗,已拿offer!(框架+多线程+集合+JVM)

    美团技术一面 1.自我介绍 说了很多遍了,很流畅捡重点介绍完. 2.问我数据结构算法好不好 挺好的(其实心还是有点虚,不过最近刷了很多题也只能壮着胆子充胖子了) 3.找到单链表的三等分点,如果单链表是 ...

  10. CSS3新特性简单总结(持续补充常用到的情景)

    1.CSS3边框border-radius 左上右下box-shadow box-shadow: 水平阴影(可负值,必) 垂直阴影(可负值,必) 模糊距离 阴影尺寸 颜色颜色 inset(将外部阴影改 ...