Dragon Core is a strong typed ORM tool for .NET 5.
Use the Dragon ORM for .NET framework 2.x-4.x.
- V 2.2 Supports .NET 5, C# 9 positional record types
- V 2.1 Supports .NET 5, C# 9 record types
- V 2.0 Supports .NET Core 3.1
dotnet add package DragonCore
You can use positional record type from C# 9
public record User (int UserId, string Email);
Or use record type
public record User
{
public int UserId { get; init; }
public string Email { get; init; }
}
Or just use regular C# class
public class User
{
public int UserId { get; set; }
public string Email { get; set; }
}
Database database = Database.Open("Server=127.0.0.1;Database=test;User Id=<user-id>;Password=<YourStrong@Passw0rd>;");
var users = database.Query<User>("select UserId, Email from UserProfile where UserId=@UserId", new { UserId = 2 }).ToList();
Assert.AreEqual(2, users[0].UserId);
Assert.AreEqual("user@company.com", users[0].Email);
The Database class has four methods: Open, Execute, QueryValue, and Query.
- Open: opens database connection using a connection string.
- Execute: creates a command and runs ExecuteNonQuery.
- QueryValue: creates a command and runs ExecuteScalar.
- Query: creates a command with SQL statement(s) or stored procedure name and creates objects. The Query method supports up to 3 multiple record sets. The result objects are returned in a Tuple, E.g.
Database database = Database.Open(/*connection string*/);
var (users, members, roles) = database.Query<TestUser, TestMemberShip, TestUserRole>(@"
select * from dbo.UserProfile
Select * from dbo.webpages_Membership
select * from dbo.webpages_Roles");
Assert.AreEqual(1, users.ToList()[0].UserId);
Assert.AreEqual(0, members.Count());
Assert.AreEqual("Sysadmin", roles.ToList()[0].RoleName);
Use the TransactionScope, guaranteeing that database queries can commit or rollback as a single unit of work.
try
{
Database database = Database.Open(/*connection string*/);
using (TransactionScope scope = new TransactionScope())
{
database.execute(...);
database.execute(...);
database.execute(...);
// The Complete method commits the transaction. If an exception has been thrown, the transaction is rolled back.
scope.Complete();
}
}
catch (TransactionAbortedException ex)
{
writer.WriteLine("TransactionAbortedException Message: {0}", ex.Message);
}
}
The unit test project creates a test database on your SQL server and populates testing data. You can use an existing SQL Server / SQL Server LocalDB or use SQL Server from docker.
docker run -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=<YourStrong@Passw0rd>" -p 1433:1433 --name sql -h sql -d mcr.microsoft.com/mssql/server:2019-latest
Once you have identified the SQL server, change the connection string in DragonCore.Tests/DatabaseTest.cs.
static string connectionString = "Server=127.0.0.1;Database=test;User Id=<user-id>;Password=<YourStrong@Passw0rd>;";
And run the unit tests.
dotnet test
https://github.com/yysun/dragon-core
Pull Requests are welcome. Have fun coding.
(C) Copyright 2020, Yiyi Sun