EF Core Adapter is the EF Core adapter for Casbin. With this library, Casbin can load policy from EF Core supported database or save policy to it.
The current version supported all databases which EF Core supported, there is a part list:
- SQL Server 2012 onwards
- SQLite 3.7 onwards
- Azure Cosmos DB SQL API
- PostgreSQL
- MySQL, MariaDB
- Oracle DB
- Db2, Informix
- And more...
You can see all the list at Database Providers.
dotnet add package Casbin.NET.Adapter.EFCore
using Casbin.NET.Adapter.EFCore;
using Microsoft.EntityFrameworkCore;
using NetCasbin;
namespace ConsoleAppExample
{
public class Program
{
public static void Main(string[] args)
{
// You should build a DbContextOptions for CasbinDbContext<TKey>.
// The example use the SQLite database named "casbin_example.sqlite3".
var options = new DbContextOptionsBuilder<CasbinDbContext<int>>()
.UseSqlite("Data Source=casbin_example.sqlite3")
.Options;
var context = new CasbinDbContext<int>(options);
// If it doesn't exist, you can use this to create it automatically.
context.Database.EnsureCreated();
// Initialize a EF Core adapter and use it in a Casbin enforcer:
var efCoreAdapter = new CasbinDbAdapter<int>(context);
var e = new Enforcer("examples/rbac_model.conf", efCoreAdapter);
// Load the policy from DB.
e.LoadPolicy();
// Check the permission.
e.Enforce("alice", "data1", "read");
// Modify the policy.
// e.AddPolicy(...)
// e.RemovePolicy(...)
// Save the policy back to DB.
e.SavePolicy();
}
}
}
This project is under Apache 2.0 License. See the LICENSE file for the full license text.