NPrismy is lightweight ORM for ASP.NET Core Web Applications.
- Add MySql support
- Add Oracle Db support
- Add benchmark results
- Simple CRUD operations with sugar syntax.
- Using database transactions
- Customized entity mappings
- Private field mapping support
- Managing persistance concerns with a high level language
Install NPrismy NuGet package by executing following command on .NET Core CLI:
dotnet add package NPrismy
and
PM> Install-Package NPrismy
on NuGet Package Manager Console.
Step by step initialization.
Create a database class that inherited from NPrismy.Database abstract class. (i.e. CarFactoryDatabase) Database classes are represents the remote database. (Like EF DbContext)
public class WeatherForecastDatabase : NPrismy.Database
{
}
Insert the following code to your Startup.cs ConfigureServices() method to register your database object to ServiceCollection.
services.AddNPrismy<WeatherForecastDatabase>()
.UseProvider(PersistanceProvider.SqlServer)
.ConnectionString("#your-connection-string#");
- Specify the connection string by passing it to
ConnectionString()
method. - Specify the your persistance provider (SqlServer, Oracle DB, MySql) by calling
UseProvider()
method. (ONLY MICROSOFT SQL SERVER SUPPORTED AT THE MOMENT)
Add EntityTable<T>
properties to your database class to specify your database tables.
public class WeatherForecastDatabase : NPrismy.Database
{
public EntityTable<City> Cities { get; set; }
public EntityTable<Forecast> Forecasts { get; set; }
}
As default, NPrismy pluralizes the property types of database class. And assumes they are in the dbo
schema. In the above example, NPrismy assumes there are two different tables named dbo.Cities
and dbo.Forecasts
. Regardless of the names of properties, NPrismy pluralizes the types City
and Foreacast
.
If you override these definitions, simply put [TableName]
and [Schema]
attributes as following.
public class WeatherForecastDatabase : NPrismy.Database
{
[TableName("MyCities")]
[Schema("weatherForecast")]
public EntityTable<City> Cities { get; set; }
[TableName("AwesomeForecasts")]
[Schema("weatherForecast")]
public EntityTable<Forecast> Forecasts { get; set; }
}
Modify your controllers as accepts WeatherForecastDatabase
(how you name it) and you're done. Querying data can be performed as following:
var lovelyCities = _db.Cities.Query(c => c.Name == "Istanbul" || c.Name == "Copenhagen");
- Querying all data from table.
var cities = await _database.Cities.Query();
- Querying with where clause
var cities = await _database.Cities.Query(c => c.Name == 'Copenhagen');
- Getting by primary key
var city = await _database.Cities.FindByPrimaryKey(1); //Gets the city with ID '1' (or whatever the primary key)
var city = new City(name: "Paris", country: "FR");
_database.Cities.Add(city);
_database.Commit(); //Don't forget to commit
var city = await _database.Cities.FindByPrimaryKey(10);
city.Name = "Paris";
_database.Cities.Update(city);
_database.Commit();
- Deleting with LINQ expression
_database.Cities.Delete(c => c.Name == "Copenhagen");
_database.Commit();
- Deleting by Primary Key
_database.Cities.Delete(1); //Deletes the city with ID 1
_database.Commit();
Note: All these following specifications must be applied on
EntityTable<T>
properties of your Database class.
NOTE: NPrismy pluralizes the entity type as default table name. Use this attribute only you need a different name.
Table names can be specified with [TableName]
attribute.
Usage example:
[TableName("people")]
public EntityTable<AbstractPerson> People { get; set; }
According to the example above, specifying the table name as people
means your table name is dbo.people
instead of dbo.AbstractPeople
.
Schema name can be specified with [Schema]
attribute.
Usage example:
[Schema("usermanagement")]
public EntityTable<AbstractPerson> People { get; set; }
NOTE: NPrismy assumes
dbo
is the schema name as default. Use this specification only if your table schema is different fromdbo
.
Specify entity's primary key by using [PrimaryKey]
attribute.
Usage example:
[PrimaryKey("PolicyDefinitionId")]
[TableName("policies")]
public EntityTable<PolicyDefinition> Policies { get; set; }
NPrismy behaves the
Id
property of a entity as primary key as default. Use this specification only if your primary key column is different fromId
.
I will fill up here.