DataObjects.Net is the object-relational mapper (ORM) and business logic layer (BLL) framework
$ dotnet add package Xtensive.OrmDataObjects.Net is a persistence and object-relational mapping framework for the Microsoft .NET. It allows developers to define persistent objects as well as business logic directly in C#, Visual Basic or F#. The persistent objects can be retrieved by LINQ queries. Persistent data can be stored in SQL Servers. In contrast to many other ORM frameworks the database model is generated and maintained automatically.
Supported databases:
Providers for the databases are available as separate packages and may be installed following way
dotnet add package Xtensive.Orm.SqlServer
dotnet add package Xtensive.Orm.Oracle
dotnet add package Xtensive.Orm.PostgreSQL
dotnet add package Xtensive.Orm.MySql
dotnet add package Xtensive.Orm.Firebird
dotnet add package Xtensive.Orm.Sqlite
The following code demonstrates basic usage of DataObjects.Net. For full tutorial configuring Domain, defining the model and querying data see our documentation.
Create a domain configuration configuration to connect to certain database
// create configuration with connection to Tests database on local instance of MS SQL Server
var domainConfiguration = new DomainConfiguration(@"sqlserver://localhost/Tests");
// register types form certain namespace which contains domain model persistent types -
// the types derrived from Xtensive.Orm.Entity and/or Xtensive.Orm.Structure
domainConfiguration.Types.Register(typeof (Person).Assembly, typeof (Person).Namespace);
// create database structure from scratch
domainConfiguration.UpgradeMode = DomainUpgradeMode.Recreate;
Build domain by the configuration created before. Usually, domain is built when the application starts and disposed just before the application shuts down.
var domain = Domain.Build(domainConfiguration);
Query data from database, modify results and/or create new entites
// open a session to database
using (var session = domain.OpenSession()) {
// and transaction
using (var transactionScope = session.OpenTransaction()) {
// query for existing Anton Chekhov
Author existingAuthor = session.Query.All<Author>()
.Where(author => author.FirstName=="Anton" && author.LastName=="Chekhov")
.FirstOrDefault();
//if Anton Pavlovich isn't in database yet then and him
if (existingAuthor==null) {
existingAuthor = new new Author(session) {
FirstName = "Anton",
LastName = "Chekhov";
}
}
// add new book and assign it with Anton Chekhov
existingAuthor.Books.Add(new Book(session) {Title = "The Cherry Orchard"});
// commit opened transaction to save changes made within it
transactionScope.Complete();
}
}
Dispose domain on application shut down
domain.Dispose();