User, Role, permission database design ABP

Source: Internet
Author: User

The user, Role, permission database design of ABP (another way to use entityframework inheritance)

Recently in the group (134710707) of friends are discussing ABP source code, I have recently learned the contents of the record, but also share to everyone, I hope that the ABP source of friends have some help.

The introduction of the ABP of the multi-language, the main introduction of the rights of the database design, with EntityFramework has been a period of time, based on the ABP such a design or the first time to see, specific application Scenarios 1:N,ABP permissions design, menu permissions can be configured to the role of can also be assigned directly to the user.

Another application scenario can also be the order system: the customer can through the order inquiry to the customer all order details, the order details and the customer has no relationship, if you want to directly view the customer's order details, can also be designed in this way, the specific design method is as follows

DbContext Design Class (Isolated demo)

  

public class Test:dbcontext {//Your context is configured to be from your application's configuration file (app. config or Web. config)//using the "Test" connection string.        By default, this connection string is for the//"Study.EF.Test" database on your LocalDb instance.        If you want to target other databases and/or database providers, modify the "Test"//connection string in the application configuration file. Public Test (): Base ("Name=test") {}//adds DbSet for each entity type you want to include in the model.        For more information about configuring and using the Code first model//, see http://go.microsoft.com/fwlink/?LinkId=390109.        Public virtual dbset<permission> Permission {get; set;}        Public virtual dbset<userpermission> UserPermission {get; set;}        Public virtual dbset<rolepermission> rolepermission {get; set;}        Public virtual dbset<user> User {get; set;}    Public virtual dbset<role> Role {get; set;}        public class Permission {public int Id {get; set;}    public string Name {get; set;}  public class Userpermission:permission {public int UserId {get; set;} //user-based permissions} public class Rolepermission:permission {public int roleid {get; set;} Role-based permissions} public class User {

Public User ()
{
Permissions = new hashset<userpermission> ();
}

public int Id {get; set;}        public string Name {get; set;}        [ForeignKey ("UserId")]        Public virtual icollection<userpermission> Permissions {get; set;}  User-based permission list    } public    class Role    {

Public Role ()
{
Permissions = new hashset<rolepermission> ();
}

public int Id {get; set;}        public string Name {get; set;}        [ForeignKey ("Roleid")]        Public virtual icollection<rolepermission> Permissions {get; set;}  Role-based permissions list    }

The generated database is 3 tables, not 5 tables, such as

EF Add Data method:

using (var text = new Test ())            {                var u = new User ();                U.name = "Test";                var up = new UserPermission ();                Up. Name = "UserPermission";                U.permissions.add (up);  Add user rights                text. User.add (u);                var role = new role ();                Role. Name = "Roletest";                var rp = new Rolepermission ();                Rp. Name = "Rolepermission";                Role. Permissions.add (RP); Add role Permissions                text. Role.add (role);                Text. SaveChanges ();            }

Execution results, field discriminator is the field generated by EF itself, used to differentiate the data source

User, Role, permission database design ABP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.