Note: It is much easier to use the Web API Service with integrated authorization & CRUD operations based on ASP.NET Core OData 8.0 (OData v4) powered by EF Core and XPO ORM library instead. For more information, see A 1-Click Solution for CRUD Web API Services with Role-based Access Control via EF Core & XPO (FREE).
This example demonstrates how to create an ASP.NET Core 3.1 Web API project and provide a simple REST API using the XPO ORM for data access. For the .NET Framework-based example, refer to How to Implement OData v4 Service with XPO (.NET Framework).
Prerequisites
- Visual Studio 2019 with the following workloads:
- ASP.NET and web development
- .NET Core cross-platform development
- .NET Core SDK 3.1 or later
Steps To Implement
Step 1: Create Solution and Add Required Dependencies
- Create a new ASP.NET Core Web Application project and select the API project template.
- Install the following NuGet packages:
- DevExpress.Xpo
- Microsoft.AspNetCore.OData
- Add files from the CS\ODataService\Helpers folder in this example to your project (Quick Tip: Add files to Visual Studio projects the easy way). These files contain helpers for demo data generation, LINQ and OData API extensions that will be used later.
Step 2: Define XPO and EDM Data Model
- Create an XPO data model or add files from the CS\ODataService\Models folder in this example to your project. For more information on how to create your own data model, refer to the following articles: Create Persistent Class | Map to Existing Tables. Make sure that the CS\ODataService\Helpers\ConnectionHelper.cs file includes your custom persistent types.
- Add the SingletonEdmModel.cs file to your project. The SingletonEdmModel.cs file demonstrates two ways to populate the EDM model with XPO classes. Also, in this file, we demonstrate how to add custom actions and functions (InitializeDatabase and TotalSalesByYear). For additional information, refer to the Convention model builder and Actions and Functions in OData v4 Using ASP.NET Web API 2.2 articles.
Step 3. Initialize Data Layer and Configure ASP.NET Core Middleware
- Specify a connection string for your database in the CS\ODataService\appsettings.json file (Microsoft SQL Server LocalDB is used by default).
Code"ConnectionStrings": {
"MSSqlServer": "XpoProvider=MSSqlServer;data source=(LocalDB)\\MSSQLLocalDB;Integrated Security=true;MultipleActiveResultSets=true;initial catalog=ODataTest"
}
- Modify the
ConfigureServices()
method in the Startup.cs file to initialize the data layer and register XPO UnitOfWork and OData services in Dependency Injection.
C#public void ConfigureServices(IServiceCollection services) {
services.AddOData();
services.AddODataQueryFilter();
services.AddMvc(options => {
options.EnableEndpointRouting = false;
options.ModelValidatorProviders.Clear();
});
services.AddSingleton<IObjectModelValidator, CustomModelValidator>();
services.AddXpoDefaultUnitOfWork(true, (DataLayerOptionsBuilder options) =>
options.UseConnectionString(Configuration.GetConnectionString("MSSqlServer"))
.UseAutoCreationOption(AutoCreateOption.DatabaseAndSchema) // debug only
.UseEntityTypes(ConnectionHelper.GetPersistentTypes()));
}
- Modify the
Configure()
method in the Startup.cs file to add middleware for OData services and specify mapping for the service route. Note that we will pass the EDM model defined on the second step as a parameter (SingletonEdmModel.GetEdmModel()
).
C#public void Configure(IApplicationBuilder app, IWebHostEnvironment env) {
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseODataBatching();
app.UseMvc(b =>
{
b.Count().Filter().OrderBy().Expand().Select().MaxTop(null);
b.MapODataServiceRoute("odata", "odata", SingletonEdmModel.GetEdmModel(), new DefaultODataBatchHandler());
});
}
Step 4: Implement OData Controllers for CRUD and Actions/Functions
- In the Controllers folder, add classes inherited from Microsoft.AspNet.OData.ODataController for each data model class created on the second step.
- Implement the required methods in OData controllers (e.g.,
Get
,Post
,Put
,Patch
,Delete
, etc.) as shown in this example (for instance, CS\ODataService\Controllers\CustomersController.cs). - Implement methods in an OData Controller for required OData Actions and Functions as shown in CS\ODataService\Controllers\ActionsController.cs.
Does this example address your development requirements/objectives?
(you will be redirected to DevExpress.com to submit your response)
Example Code
C#using DevExpress.Data.Filtering;
using DevExpress.Xpo;
using DevExpress.Xpo.Metadata;
using Microsoft.AspNet.OData.Builder;
using Microsoft.OData.Edm;
using ODataService.Helpers;
using System;
using System.Collections.Generic;
using System.Linq;
namespace ODataService.Models {
internal class SingletonEdmModel {
static IEdmModel edmModel;
public static IEdmModel GetEdmModel() {
if(edmModel != null) {
return edmModel;
}
var builder = new ODataConventionModelBuilder();
// Approach 1: Automatically add all persistent classes to EDM
// This approach has a naming convention: an OData controller
// name must match the corresponding XPO class name
var dictionary = new ReflectionDictionary();
foreach(var type in ConnectionHelper.GetPersistentTypes()) {
XPClassInfo classInfo = dictionary.GetClassInfo(type);
CreateEntitySet(classInfo, builder);
}
// Approach 2: Manually add persistent classes to EDM
/*var documents = builder.EntitySet<BaseDocument>("BaseDocument");
var customers = builder.EntitySet<Customer>("Customer");
var orders = builder.EntitySet<Order>("Order");
var contracts = builder.EntitySet<Contract>("Contract");
var products = builder.EntitySet<Product>("Product");
var orderDetails = builder.EntitySet<OrderDetail>("OrderDetail");
documents.EntityType.HasKey(t => t.ID);
customers.EntityType.HasKey(t => t.CustomerID);
products.EntityType.HasKey(t => t.ProductID);
orderDetails.EntityType.HasKey(t => t.OrderDetailID);
orders.EntityType.DerivesFrom<BaseDocument>();
contracts.EntityType.DerivesFrom<BaseDocument>();*/
// Add actions and functions to EDM
builder.Action("InitializeDatabase");
builder.Function("TotalSalesByYear")
.Returns<decimal>()
.Parameter<int>("year");
edmModel = builder.GetEdmModel();
return edmModel;
}
static EntitySetConfiguration CreateEntitySet(XPClassInfo classInfo, ODataModelBuilder builder) {
EntitySetConfiguration entitySetConfig = builder.EntitySets.FirstOrDefault(t => t.EntityType.ClrType == classInfo.ClassType);
if(entitySetConfig != null) {
return entitySetConfig;
}
EntityTypeConfiguration entityTypeConfig = builder.AddEntityType(classInfo.ClassType);
entitySetConfig = builder.AddEntitySet(classInfo.ClassType.Name, entityTypeConfig);
if(classInfo.PersistentBaseClass != null) {
EntitySetConfiguration baseClassEntitySetConfig = CreateEntitySet(classInfo.PersistentBaseClass, builder);
entityTypeConfig.DerivesFrom(baseClassEntitySetConfig.EntityType);
} else {
if(classInfo.KeyProperty is ReflectionFieldInfo) {
foreach(XPMemberInfo mi in classInfo.Members) {
if(mi.IsAliased) {
string aliasedExpr = ((PersistentAliasAttribute)mi.GetAttributeInfo(typeof(PersistentAliasAttribute))).AliasExpression;
var aliasedCriteria = CriteriaOperator.Parse(aliasedExpr) as OperandProperty;
if(!ReferenceEquals(null, aliasedCriteria) && aliasedCriteria.PropertyName == classInfo.KeyProperty.Name) {
entityTypeConfig.HasKey(classInfo.ClassType.GetProperty(mi.Name));
break;
}
}
}
} else {
entityTypeConfig.HasKey(classInfo.ClassType.GetProperty(classInfo.KeyProperty.Name));
}
}
return entitySetConfig;
}
}
}