---
title: "Assign permission for pages using Code first in asp.net MVC:"  
description: "Assign permission for pages using Code first in asp.net MVC:"  
author: "Anonymous User"  
published: 2012-12-26  
updated: 2012-12-26  
canonical: https://www.mindstick.com/forum/512/assign-permission-for-pages-using-code-first-in-asp-dot-net-mvc  
category: "asp.net mvc"  
tags: ["asp.net mvc"]  
reading_time: 7 minutes  

---

# Assign permission for pages using Code first in asp.net MVC:

Hi,

Here I have created a Model [Structure in asp.net](https://answers.mindstick.com/blog/301/implement-a-common-api-response-structure-in-asp-dot-net-core) mvc:

```
    public class UserModel

    {

        public int UserId { get; set; }

        public string UserName { get; set; }

        public string Password { get; set; }

        public List<Permission> Permissions { get; set; }

        public string FirstName { get; set; }

        public string LastName { get; set; }

    }

    public class Permission

    {

        public int PermissionID { get; set; }

        public bool IsPermit { get; set; }

        public string Name { get; set; }

    }
```

\
and setting some [default values](https://www.mindstick.com/blog/10881/default-values) in the list and while I am adding the user in the list I assign the [permission](https://www.mindstick.com/forum/159434/linux-service-permission-denied-error-adjust-permissions) for pages to that user through the UI (by checking permission checkboxes), so that user can access only the assigned pages:

```
 public static class Repository
    {
        public static List<UserModel> GetUsers()
        {
            List<UserModel> listUsers = new List<UserModel>
            {
                new UserModel
                {
                    UserId = 1,
                    UserName = "abc",
                    Password = "abc",
                    Permissions = new List<Permission>
                    {
                        new Permission
                        {
                            PermissionID = 1,
                            IsPermit = true,
                            Name = "Page1"
                        },
                        new Permission
                        {
                            PermissionID = 2,
                            IsPermit = false,
                            Name = "Page2"
                        },
                        new Permission
                        {
                            PermissionID = 3,
                            IsPermit = false,
                            Name = "Page3"
                        },
                        new Permission
                        {
                            PermissionID = 4,
                            IsPermit = false,
                            Name = "Page4"
                        }
                    },
                    FirstName = "Rohit",
                    LastName = "Sharma"
                },
                new UserModel
                {
                    UserId = 2,
                    UserName = "xyz",
                    Password = "xyz",
                    Permissions = new List<Permission>
                    {
                        new Permission
                        {
                            PermissionID = 1,
                            IsPermit = false,
                            Name = "Page1"
                        },
                        new Permission
                        {
                            PermissionID = 2,
                            IsPermit = true,
                            Name = "Page2"
                        },
                        new Permission
                        {
                            PermissionID = 3,
                            IsPermit = true,
                            Name = "Page3"
                        },
                        new Permission
                        {
                            PermissionID = 4,
                            IsPermit = true,
                            Name = "Page4"
                        }
                    },
                    FirstName = "Rahul",
                    LastName = "Sharma"
                }
            };
            return listUsers;
        }
    }   
```

\
Now I want to do the same by using [code first approach](https://www.mindstick.com/articles/1497/many-to-many-and-one-to-many-relationship-using-ef-code-first-approach) of database with the help of [DbContext](https://www.mindstick.com/forum/160257/what-is-the-role-of-the-dbcontext-class-in-database-migrations-with-entity-framework-core) class.

I have a static list of page permission in a [database table](https://www.mindstick.com/forum/159366/how-to-test-regular-expressions-in-sql-without-using-database-table) (Id =1, Name=Page1; Id =2, Name=Page2; Id =3, Name=Page3; Id =4, Name=Page4).

I am confused [while creating](https://www.mindstick.com/forum/105382/which-constraints-we-can-use-while-creating-a-database-in-sql) model structure for database. Please guide me how to create model structure and mapping of structure with the tables.

Please reply ASAP.

Thanks.


---

Original Source: https://www.mindstick.com/forum/512/assign-permission-for-pages-using-code-first-in-asp-dot-net-mvc

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
