How to add a foreign key in a Customer table (CreatedBy column) for an AspNetUser table (identifier column) in ASP.NET MVC 5 Identity 2.0 using First code

I created an empty MVC project (ASP.NET Web Application) using Visual Studio 2013 Update 2 RC & amp; then added AspNet authentication patterns using:

PM> Installation Package Microsoft.AspNet.Identity.Samples -Pre

I enabled and added migrations, and then updated the database that created the tables by default. enter image description here

I want to create a Customer table that contains 2 columns as foreign keys:

  • Group table (GroupId column)
  • AspNetUsers table (identifier column)

So, I created 2 classes Customer and Group and added foreign keys using data annotations as shown below:

namespace IdentitySample.Models
{
    // You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
    public class ApplicationUser : IdentityUser
    {
        public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
        {
            // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
            var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
            // Add custom user claims here
            return userIdentity;
        }
    }

    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext()
            : base("DefaultConnection", throwIfV1Schema: false)
        {
        }

        static ApplicationDbContext()
        {
            // Set the database intializer which is run once during application start
            // This seeds the database with admin user credentials and admin role
            Database.SetInitializer<ApplicationDbContext>(new ApplicationDbInitializer());
        }

        public static ApplicationDbContext Create()
        {
            return new ApplicationDbContext();
        }
        public DbSet<Customer> Customers { get; set; }
        public DbSet<Group> Groups { get; set; }
    }

    public class Customer
    {
        public int CustomerId { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int Age { get; set; }
        public int GroupId { get; set; }
        public string CreatedBy { get; set; }



        [ForeignKey("GroupId")]
        public Group Groups { get; set; }

        [ForeignKey("CreatedBy")]
        public ApplicationUser ApplicationUsers { get; set; }
    }

    public class Group
    {
        public int GroupId { get; set; }
        public string GroupName { get; set; }
    }
}

, ApplicationDbContext.edmx, EF Power, .

enter image description here

CustomerController, "MVC 5 Controller , Entity Framework".

enter image description here

( db.ApplicationUsers)

// GET: Customers/Create
public ActionResult Create()
{
    ViewBag.CreatedBy = new SelectList(db.ApplicationUsers, "Id", "Email");
    ViewBag.GroupId = new SelectList(db.Groups, "GroupId", "GroupName");
    return View();
}

enter image description here

: "IdentitySample.Models.ApplicationDbContext" "ApplicationUsers" "ApplicationUsers" , "IdentitySample.Models.ApplicationDbContext" ( using ?)

ApplicationDbContext

public DbSet<ApplicationUser> ApplicationUsers { get; set; }

:

. "ApplicationUsers" "Users" 'IdentitySample.Models.ApplicationUser'

CreatedBy AspNetUsers, , CreateBy, GroupId:

enter image description here

, Identity, , , , Identity. , "MVC 5 Controller , Entity Framework"?
( , Customers Groups, Customers ExternalIndkey GroupId)

+4
2

RTFE: ApplicationUsers ApplicationDbContext.

:

    public DbSet<ApplicationUser> ApplicationUsers { get; set; }
+2

Entity Framework , /. , , :

public class Customer
{
    public string CustomerId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int Age { get; set; }

    public int ApplicationUserId { get; set; }
    public virtual ApplicationUser ApplicationUser { get; set; } 
}

"Id" . , ForeignKey, EF , :

public class Customer
{
    public string CustomerId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int Age { get; set; }

    public int UserId { get; set; }
    [ForeignKey("UserId")]
    public ApplicationUser ApplicationUser { get; set; }
}

this .

+2

Source: https://habr.com/ru/post/1536862/


All Articles