How to add a Foreign key in Customer table (CreatedBy column) for AspNetUser table (Id column) in ASP.NET MVC 5 Identity 2.0 using Code First

user3542245

I have created Empty MVC (ASP.NET Web Application) project using Visual Studio 2013 Update 2 RC & then added AspNet Identity Samples using:

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

I have enabled and added migrations & then updated database, which created default tables. enter image description here

I want to create Customer table which contains 2 columns as Foreign Keys to:

  • Groups table (GroupId column)
  • AspNetUsers table (Id column)

So I have created 2 classes Customer & 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; }
    }
}

Everything seems fine, the ApplicationDbContext.edmx diagram created using EF Power tools also looks fine & project builds properly.

enter image description here

I have then added CustomersController using "MVC 5 Controller with views, using Entity Framework" scaffolding template.

enter image description here

Now I am getting compilation error (at 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

Error details: 'IdentitySample.Models.ApplicationDbContext' does not contain a definition for 'ApplicationUsers' and no extension method 'ApplicationUsers' accepting a first argument of type 'IdentitySample.Models.ApplicationDbContext' could be found (are you missing a using directive or an assembly reference?)

When I add below code in ApplicationDbContext

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

I get error:

Multiple object sets per type are not supported. The object sets 'ApplicationUsers' and 'Users' can both contain instances of type 'IdentitySample.Models.ApplicationUser'

I want to add CreatedBy as foreign key to AspNetUsers, using which the template generate CreatedBy dropdown similar to GroupId dropdown:

enter image description here

How can we use Identity generated tables with other user created tables, where user created table have Foreign Key references to Identity generated tables. And have first class scaffolding code generation experience using "MVC 5 Controller with views, using Entity Framework"?
(Similar to what we have with Customers & Groups table, where Customers table have GroupId Foreign key reference)

Rudi

RTFE: you do not have a property ApplicationUsers on your ApplicationDbContext class.

Just add:

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

ASP .NET C# MVC 5 Code First Foreign Key Property/Column Names

From Dev

Add column in table with ASP.NET identity

From Dev

Bulk insert nested xml with foreign key as identity column of first table

From Dev

Bulk insert nested xml with foreign key as identity column of first table

From Dev

How to reference foreign key which does not reference a primary key column. Asp.net mvc code first?

From Dev

Is it possible to set a default value for a column when creating a table using ASP.net EF 5 code first?

From Dev

unable to set other specified column as primary key instead of Id column to table using entity framework code first

From Dev

C# Code first foreign key. The foreign key refers to the non valid column in table with references

From Dev

Using ASP.Net Identity EntityFramwork to Existing AspNetUsers table with ID Column set to UniqueIdentifier

From Dev

Reading a detail table to ASP.NET MVC 5 Identity 2

From Dev

How to add foreign key in table using existing column, without losing data?

From Dev

How to add foreign key in table using existing column, without losing data?

From Dev

Remove table name prefix in EF code first foreign key table column

From Dev

ASP.NET MVC 5 identity application user as foreign key

From Dev

SELECT a non ID column in a foreign key table (TABLE B) based on the foreign key in the primary table (TABLE

From Dev

how to add identity column for a table using mysql server 5.6?

From Dev

set the column code as foreign key on table with a reference to another table

From Dev

Extra column on user-roles table when using custom identity user(ASP.NET 5.2EF 6 and Identity 2)

From Dev

In Rails, how do I create a migration that will add a column/foreign key to a table that is of the same type as that table?

From Dev

How to create SecurityStamp for AspNetUser in ASP .NET MVC 5

From Dev

Foreign key relationship missing in ASP.NET MVC app using code-first approach with Entity Framework

From Dev

How to retrieve one column value from one table using asp .net mvc 4

From Dev

How to save auto generated primary key Id in foreign key column in same table

From Dev

What is the purpose of the ConcurrencyStamp column in the AspNetUsers table in the new ASP.NET MVC 6 identity?

From Dev

Asp.net MVC Export Table to Excel | Foreign Key Display

From Dev

ASP.Net Identity to MVC 6 with INT id Column

From Dev

How to insert current user id(in session) name into table column of createdby & updatedby

From Dev

How to Combine 2 Result (add Column) from Linq Select into 1 Table or CrossJoin in ASP.Net Core

From Dev

I can't save a foreign key into one to many table asp.net but data can be inserted asp.net mvc 5

Related Related

  1. 1

    ASP .NET C# MVC 5 Code First Foreign Key Property/Column Names

  2. 2

    Add column in table with ASP.NET identity

  3. 3

    Bulk insert nested xml with foreign key as identity column of first table

  4. 4

    Bulk insert nested xml with foreign key as identity column of first table

  5. 5

    How to reference foreign key which does not reference a primary key column. Asp.net mvc code first?

  6. 6

    Is it possible to set a default value for a column when creating a table using ASP.net EF 5 code first?

  7. 7

    unable to set other specified column as primary key instead of Id column to table using entity framework code first

  8. 8

    C# Code first foreign key. The foreign key refers to the non valid column in table with references

  9. 9

    Using ASP.Net Identity EntityFramwork to Existing AspNetUsers table with ID Column set to UniqueIdentifier

  10. 10

    Reading a detail table to ASP.NET MVC 5 Identity 2

  11. 11

    How to add foreign key in table using existing column, without losing data?

  12. 12

    How to add foreign key in table using existing column, without losing data?

  13. 13

    Remove table name prefix in EF code first foreign key table column

  14. 14

    ASP.NET MVC 5 identity application user as foreign key

  15. 15

    SELECT a non ID column in a foreign key table (TABLE B) based on the foreign key in the primary table (TABLE

  16. 16

    how to add identity column for a table using mysql server 5.6?

  17. 17

    set the column code as foreign key on table with a reference to another table

  18. 18

    Extra column on user-roles table when using custom identity user(ASP.NET 5.2EF 6 and Identity 2)

  19. 19

    In Rails, how do I create a migration that will add a column/foreign key to a table that is of the same type as that table?

  20. 20

    How to create SecurityStamp for AspNetUser in ASP .NET MVC 5

  21. 21

    Foreign key relationship missing in ASP.NET MVC app using code-first approach with Entity Framework

  22. 22

    How to retrieve one column value from one table using asp .net mvc 4

  23. 23

    How to save auto generated primary key Id in foreign key column in same table

  24. 24

    What is the purpose of the ConcurrencyStamp column in the AspNetUsers table in the new ASP.NET MVC 6 identity?

  25. 25

    Asp.net MVC Export Table to Excel | Foreign Key Display

  26. 26

    ASP.Net Identity to MVC 6 with INT id Column

  27. 27

    How to insert current user id(in session) name into table column of createdby & updatedby

  28. 28

    How to Combine 2 Result (add Column) from Linq Select into 1 Table or CrossJoin in ASP.Net Core

  29. 29

    I can't save a foreign key into one to many table asp.net but data can be inserted asp.net mvc 5

HotTag

Archive