The requested page is not available for the requested platform. You are viewing the content for .NET Standard 2.0+ platform.
You are viewing help content for pre-release software. This document and the features it describes are subject to change. Switch to the current version.

Getting Started with .NET Core

  • 5 min to read

This tutorial demonstrates how to create an XPO-based .NET Standard 2.0 console application and a .NET Core 3.0 WinForms/WPF application, which initialize the data layer and perform basic data operations.

Note

The complete .NET Standard 2.0 sample project is available at https://github.com/DevExpress/XPO/tree/master/Tutorials/Console. 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

Console

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

Desktop

Install the .NET Core 3.0 SDK and runtime Preview 8 or newer.

Create a Project

Console

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, add the '-lang vb' switch to the command above.

Desktop

Use one of the following approaches:

Get XPO for .NET Core

Console

Change the current directory to the project's directory.

cd MyXpoApp

Install the DevExpress.Xpo NuGet package.

dotnet add package DevExpress.Xpo

Desktop

Install one of the following DevExpress NuGet packages:

  • DevExpress.WindowsDesktop.Core
  • DevExpress.WindowsDesktop.Win
  • DevExpress.WindowsDesktop.Wpf

Install the Database Provider

Use the following command to install the Microsoft.Data.Sqlite package and use the local SQLite database:

dotnet add package Microsoft.Data.Sqlite

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

Define the Data Model

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

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 and run the application. Use the dotnet run command in the system console to start the console application.