Getting Started with .NET Core

This tutorial demonstrates how to create an XPO-based .NET Standard 2.0 console application that initializes the data layer and performs basic data operations.

Note

The complete sample project is available at https://github.com/DevExpress/XpoNetCoreDemos/tree/master/XpoConsoleCoreDemo. You can also find examples of XPO-based ASP.NET Core and Xamarin.Forms applications in the DevExpress XPO for .NET Standard 2.0 Demos GitHub repository.

Prerequisites

.NET Core 2.0 SDK for Windows, Linux or Mac.

Create a Project

Open the system console and execute the following command to create a new C# project (you can substitute MyXpoApp with your project name):

dotnet new console -o MyXpoApp

To create a VB.NET project instead, add the '-lang vb' switch to the command.

Get XPO for .NET Core

Change the current directory to the project's directory.

cd MyXpoApp

Install the DevExpress.Xpo NuGet package.

dotnet add package DevExpress.Xpo

Install the Database Provider

Install the Microsoft.Data.Sqlite package with the following command to use the local SQLite database:

dotnet add package Microsoft.Data.Sqlite

You can use any other .NET Standard 2.0 compatible provider XPO supports (see Database Systems Supported by XPO).

Define the Data Model

In general, using XPO for .NET Core is the same as .NET Framework 3.5+. APIs, demonstrated in the XPO tutorials and examples, are in the same namespaces (DevExpress.Xpo, DevExpress.Xpo.DB, etc.). You can use the same classes to define the data model and query data: XPObject, UnitOfWork, Session, XPCollection, etc.

Edit the MyXpoApp/Program.cs(vb) file (or add a new code file to the project) and implement the following StatisticInfo persistent class with the Key, Info and Date properties. The class is mapped to the StatisticInfo table with the Key, Info and Date columns.


using DevExpress.Xpo;
// ...
public class StatisticInfo : XPLiteObject {
    public StatisticInfo(Session session)
        : base(session) {
    }
    Guid key;
    [Key(true)]
    public Guid Key {
        get { return key; }
        set { SetPropertyValue(nameof(Key), ref key, value); }
    }
    string info;
    [Size(255)]
    public string Info {
        get { return info; }
        set { SetPropertyValue(nameof(Info), ref info, value); }
    }
    DateTime date;
    public DateTime Date {
        get { return date; }
        set { SetPropertyValue(nameof(Date), ref date, value); }
    }
}

Setup the Data Layer

In the Program.cs(vb) file, modify the Program.Main method. Pass the connection string to the XpoDefault.GetDataLayer method and assign the created IDataLayer object to the XpoDefault.DataLayer property.


using System.IO;
using DevExpress.Xpo;
using DevExpress.Xpo.DB;
// ...
class Program {
    static void Main(string[] args) {
        string appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
        string connectionString = SQLiteConnectionProvider.GetConnectionString(Path.Combine(appDataPath, "myXpoApp.db"));
        XpoDefault.DataLayer = XpoDefault.GetDataLayer(connectionString, AutoCreateOption.DatabaseAndSchema);
    }
}

Make CRUD Operations with Data

You can use the Unit of Work API to execute create, read, update and delete (CRUD) operations. For example, you can add the following code after the data layer initialization:


using System.Linq;
using DevExpress.Xpo;
// ...
// Create data:
Console.WriteLine($"Type some text to create a new 'StatisticInfo' record.");
string userInput = Console.ReadLine();
using (UnitOfWork uow = new UnitOfWork()) {
    StatisticInfo newInfo = new StatisticInfo(uow);
    newInfo.Info = userInput;
    newInfo.Date = DateTime.Now;
    uow.CommitChanges();

}
// Read data:
Console.WriteLine($"Your text is saved. The 'StatisticInfo' table now contains the following records:");
using (UnitOfWork uow = new UnitOfWork()) {
    var query = uow.Query<StatisticInfo>()
        .OrderBy(info => info.Date)
        .Select(info => $"[{info.Date}] {info.Info}");
    foreach (var line in query) {
        Console.WriteLine(line);
    }
}
// Delete data:
using (UnitOfWork uow = new UnitOfWork()) {
    var itemsToDelete = uow.Query<StatisticInfo>().ToList();
    Console.Write($"Records count is {itemsToDelete.Count}. Do you want to delete all records (y/N)?: ");
    if (Console.ReadLine().ToLowerInvariant() == "y") {
        uow.Delete(itemsToDelete);
        uow.CommitChanges();
        Console.WriteLine($"Done.");
    }
}

Save the changes in the Program.cs file. Now you can run the application using the dotnet run command in the system console.