XAF Entity Framework
XAF Entity Framework
With version v2012 vol 1, XAF begins to support the Entity Framework as an ORM layer. Previously, XAF has been based on an eXpressPersistent Objects library (XPO) - our internal ORM system. Now we have made XAF core independent from XPO. All the classes that are required to use XPO as a data layer are located in the DevExpress.ExpressApp.Xpo.12.1.dll assembly. In addition, we have placed the classes that are required for using the Entity Framework as a data layer into the DevExpress.ExpressApp.EF.12.1.dll assembly. There are, however, a few modules left that are still XPO-dependent. So certain out-of-the-box functionalities cannot be used in Entity Framework-based applications. In this connection, we release the "Entity Framework Support" feature in the CTP version. Currently, you can only try using the Entity Data Model in XAF applications. In this topic, you will learn how to create an XAF application with an Entity Data Model.
Note If you prefer to watch a video rather than walk through these step-by-step instructions, you can access a corresponding tutorial in our YouTube Channel: XAF: The Entity Framework Data Model.
Five projects will be automatically created for you, if you chose the DXperience v12.1 XAF Cross-Platform Application template as displayed in the image above.
<SolutionName>.Module is a UI-independent module. Use it for UI-independent stuff. <SolutionName>.Module.Win is a module for the Windows Forms specific functionality. <SolutionName>.Module.Web is a module for the ASP.NET specific functionality. <SolutionName>.Win is a Windows Forms XAF application that utilizes the modules from the solution (with the exception of the ASP.NET ones) to present end-users with a Windows Forms user interface. <SolutionName>.Web is an ASP.NET application project that does almost the same thing as the Windows Forms application, except for generating a browserbased interface instead of Windows Forms.
If you choose the DXperience v12.1 XAF Windows Forms Application or DXperience v12.1 XAF ASP.NET Application template, your solution will include three projects - a common module, a platform-specific module and an application project.
It is doesn't matter whether you have an existing Entity Data Model or you wish to create a new one - add it to the common module. In this instance, both the ASP.NET and Windows Forms UI will be generated for the same Data Model, if your application is a cross-platform one.
Note Add your Data Model to modules only. If you add it to an application project, your entities will not be added to the process to automatic UI generation.
To apply required attributes (see Built-in Attributes) to your entities and their properties, add code files. In the code below, the following customizations are performed with the Department class. The DefaultClassOptionsAttribute attribute is applied to create a navigation item that will allow end-users to invoke a List View of Department business objects. The standard DefaultProperty attribute is applied to present Department objects by their Title property value. The standard Metadata attribute is applied to extend the Department entity with a public ID property and make it hidden in a UI.
C# VB
using System; using System.ComponentModel; using System.ComponentModel.DataAnnotations; using DevExpress.Persistent.Base; namespace XAFApplicationWithEntityDataModel.Module.BusinessObjects { [DefaultClassOptions] [DefaultProperty("Title")] [MetadataType(typeof(DepartmentMetadata))] public partial class Department { } public class DepartmentMetadata { [Browsable(false)] public Int32 ID { get; set; } } }
Change the Type of the ObjectSpaceProvider In the Entity Framework, the Object Context is used to create and manage data. In XPO, there is a Session class that has the same functions. In XAF, an Object Space is used to manipulate with data provided by a particular data layer. Generally, Object Space is a wrapper over the Object Context if the Entity Framework is used, or the Session if XPO as used. Object Space implements the IObjectSpace interface. There are two Object Space types supplied -
XPObjectSpace and EFObjectSpace. Both of them implement the IObjectSpace interface. To provide the creation of the EFObjectSpace instance in your application, modify the default implementation of the CreateDefaultObjectSpaceProvider method in your WinApplication.cs(WinApplication.vb) and WebApplication.cs(WebApplciation.vb) files. In this method, create a EFObjectSpaceProvider for the application. It is the object that will be used for creating an Object Space of the EFObjectSpace type when the application is running. The following code demonstrates the required CreateDefaultObjectSpaceProvider method implementation in the Windows Forms application.
C# VB
//Add references to the System.Data.Entity and DevExpress.ExpressApp.EF assemblies using DevExpress.ExpressApp.EF; using DevExpress.ExpressApp.DC; using XAFApplicationWithEntityDataModel.Module.BusinessObjects; using System.Data.Common; using XAFApplicationWithEntityDataModel.Module; // ... public partial class XAFApplicationWithEntityDataModelWindowsFormsApplication : WinApplication { protected override void CreateDefaultObjectSpaceProvider( CreateCustomObjectSpaceProviderEventArgs args) { if (args.Connection != null) { args.ObjectSpaceProvider = new EFObjectSpaceProvider(typeof(MyDataModelContainer), (TypesInfo)TypesInfo, (DbConnection)args.Connection, XAFApplicationWithEntityDataModelModule.Metadata, XAFApplicationWithEntityDataModelModule.Provider, null); } else { args.ObjectSpaceProvider = new EFObjectSpaceProvider(typeof(MyDataModelContainer), (TypesInfo)TypesInfo,
Here, the Metadata and Provider are the constants declared in the common module class. They are set to the corresponding values from the connection string provided for the .edmx file. These constants are declared in the following manner.
C# VB
public sealed partial class XAFApplicationWithEntityDataModelModule : ModuleBase { public static String Metadata = "res://*/BusinessObjects.MyDataModel.csdl|res://*/BusinessObjects.My DataModel.ssdl|" + "res://*/BusinessObjects.MyDataModel.msl"; public static String Provider = "System.Data.SqlClient"; public XAFApplicationWithEntityDataModelModule() { InitializeComponent(); } }
In the ASP.NET application, you can override the CreateDefaultObjectSpaceProvider method in the same way. Use the EFTypeInfoSource object to detect exported types
In the IsExportedType method of your common module class, use the EFTypeInfoSource object to detect exported types. The following code demonstrates this.
C# VB
//Add a reference to the DevExpress.ExpressApp.EF assembly using DevExpress.ExpressApp.EF; // ... public sealed partial class XAFApplicationWithEntityDataModelModule : ModuleBase { //... public override Boolean IsExportedType(Type type) { return EFTypeInfoSource.IsExportedType(type); } }