Welcome to the DevExpress Learn Now series.Quick turorials to help you get started using DevExpress
Controls.
New for DXperience 12.1, XAF now supports the Entity Framework as an ORM layer.
Currently, this feature is released in the CTP version.
In this video, we’ll go over the steps required to create an XAF application using an existing
Entity Data Model.
So let’s get started…
We’ll start with a brand new XAF solution.
From the startup menu, we’ll choose the XAF Cross-Platform Application template so
we can create both Windows Form and ASP.NET applications.
Our solution includes five projects: two application projects and three projects that we call modules.
Application projects are only intended to set up applications.
The Win and Web modules are intended for the win and web-specific stuff, respectively.
The MySolution.Module project is referenced both in the Windows Forms and ASP.NET application
projects.
We’ll use this module to implement platform-independent stuff.
For this demo, we’ll put the Entity Data Model in this module because we want our entities
to be available on both UI platforms.
Since we’ll need parameters of the Entity Data Model connection string, let’s declare
the Metadata and Provider constants, for the sake of ease of use of these parameters.
Here’s our Entity Data Model.
It includes two entities: an Employee and a Task.
They’re related by the One-To-Many relationship.
We have custom data classes for our entities, so let’s add them to the common module to
apply some XAF attributes.
We’ll need to add necessary references.
Now, let’s apply the DefaultClassOptions attribute to both classes to have the Employee
and Task items added to the main form's navigation.
To specify the image that will represent the Employee and Task objects in a UI, let’s
apply the ImageName attribute.
“BO_Employee” and “BO_Task” are the names of images from the built-in Images library.
As you can see, the Description property of the Task class may have a maximum integer
value.
If this happens, it’s better if a memo edit is used to display it.
Now that we’re finished with our Data Model, we should set the ORM tool that we want to
use.
Currently, XPO is used by default.
To use the Entity Framework, modify the implementation of the CreateDefaultObjectSpaceProvider method
in your WinApplication and WebApplciation descendants.
In this method, create an object of the EFObjectSpaceProvider type.
This is the object that’ll be used for creating an Object Space of the EFObjectSpace type
when the application is running.
First, let’s add a reference to the Devexpress.ExpressApp.EF assembly.
Now pass the Object Context type used in our application, along with the TypesInfo object,
connection (we can get it from the object passed as a parameter), metadata,
and provider (we can get them from the connection string created for our Entity DataModel).
Let’s modify the CreateDefaultObjectSpaceProvider method of the WebApplciation class descendant
in the same manner.
Now let’s build the solution.
There are errors found.
Let’s add one more reference.
Now, in the IsExportedType method of the common module class, use the EFTypeInfoSource object
to detect exported types, because an XPTypeInfoSource object is used
by default.
Finally, let’s specify an appropriate connection string to connect to an existing database.
OK… let’s run the Windows Forms application and check to see if everything is working
fine.
As you can see, a UI is automatically generated for our Entity Data Model.
I can create new business objects, As well as Delete them,
As you can see, there’s a lot of built-in functionality in the autogenerated interface.
The eXpressApp Framework also allows custom autogenerated UI, As well as the ability to
add new features and use built-in modules, but we’ll cover that in a different video.
Now let’s run the ASP.NET application and see how it’s generated using our Entity
Data Model.
As you can see, it contains the same basic functionality as the Windows Forms application.
Thanks for watching, let’s see what develops.