0% found this document useful (0 votes)
639 views7 pages

XAF Entity Framework

The document discusses how to create an XAF application that uses the Entity Framework as the data access layer. It describes adding an Entity Data Model to the common module, configuring the application to use EFObjectSpace instead of XPObjectSpace, and setting the required connection string to the database. The key steps are to switch the ObjectSpaceProvider to EFObjectSpaceProvider, use EFTypeInfoSource to detect exported types, and specify the connection string in the application designer.

Uploaded by

Bin Rashid
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
639 views7 pages

XAF Entity Framework

The document discusses how to create an XAF application that uses the Entity Framework as the data access layer. It describes adding an Entity Data Model to the common module, configuring the application to use EFObjectSpace instead of XPObjectSpace, and setting the required connection string to the database. The key steps are to switch the ObjectSpaceProvider to EFObjectSpaceProvider, use EFTypeInfoSource to detect exported types, and specify the connection string in the application designer.

Uploaded by

Bin Rashid
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

How to: Use the Entity Framework Data Model in an XAF Application

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.

Create an XAF Application


The eXpressApp Framework comes with integration into Visual Studio. So to create an eXpressApp Framework solution, choose File | New | Project... from the Visual Studio menu. In the invoked New Project dialog, choose an appropriate eXpressApp Framework application solution template, enter a name and a location for the solution in the usual way before confirming everything with the OK button.

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.

Add an Entity Data Model

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; } } }

Switch the Application to Using the Entity Framework

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,

args.ConnectionString, XAFApplicationWithEntityDataModelModule.Metadata, XAFApplicationWithEntityDataModelModule.Provider, null); } ((TypesInfo)TypesInfo).AddEntityStore(args.ObjectSpaceProvider.Entit yStore); } //... }

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); } }

Set the Required Connection String to the Database


By default, the Windows Forms and ASP.NET applications we have just created are configured to use an instance of the Microsoft SQL Server Express Edition on the local system.You can use another supported DBMS (Microsoft SQL Server, MySQL, etc.) or use the remote server. In this case, you need to change the connection string settings for your Windows Forms and ASP.NET Web applications. To change these settings, invoke the Application Designer for each application, drag the required connection from the Toolbox to the Connection section in the Designer, and specify the ConnectionString property in the Properties window. For details on connection strings, refer to the Tutorial topic. Now you can run both the Windows Forms and ASP.NET applications. Right-click the Windows Forms or ASP.NET project in the Solution Explorer, and select the Set as StartUp Project item from the context menu. Then run the application. You will see that a UI is automatically generated for your Data Model.

You might also like