DataAnnotation and Code First to Create Foreign Key to ApplicationUser using Identity 2.2.1

RoLYroLLs

Intro

I'm trying to build a code first table which will have a 3rd table relating it to the ApplicationUser. I've been searching SO and Google, as I always do, to find a solution to my problem as I'm sure I'm not the first to run into this, but all the solutions I've tried are not working. Maybe it's because of the version of ASP.NET Identity (v 2.2.1) I'm using requires a different solution than what I'm finding or There are concepts I'm not familiar with yet.

Essentially, what I'm trying to build is exactly what the IdentityUserRole class is with different meaning. I want to build it using DataAnnotations and not the method they used. While I do appreciate other solutions and ways of doing the same thing, and I'm working on a real project, I do like to learn and at the very least want to learn how to do it with DataAnnotation.

With that said, here's the good stuff:

Classes

Sections class, which would be relative to the IdentityRole class

public class Sections {
    [Key]
    [StringLength(128)]
    public virtual String Id { get; set; }

    [Required]
    [Index("SectionsNameIndex", IsUnique = true)]
    [MaxLength(256)]
    [Display(Name = "Section Name")]
    public virtual String Name { get; set; }
}

Here is the UserSections class, which would be relative to the IdentityUserRole class

Version 1

public class UserSections {
    [Key, Column(Order = 1)]
    [Index]
    [StringLength(128)]
    [ForeignKey("User")]
    public virtual String UserId { get; set; }
    
    public virtual ApplicationUser User { get; set; }
    
    [Key, Column(Order = 2)]
    [Index]
    [StringLength(128)]
    [ForeignKey("Section")]
    public virtual String SectionId { get; set; }
    
    public virtual Sections Section { get; set; }
}

Version 2

public class UserSections {
    [Key, Column(Order = 1)]
    [Index]
    [StringLength(128)]
    public virtual String UserId { get; set; }
    
    [ForeignKey("UserId")]
    public virtual ApplicationUser User { get; set; }
    
    [Key, Column(Order = 2)]
    [Index]
    [StringLength(128)]
    public virtual String SectionId { get; set; }
    
    [ForeignKey("SectionId")]
    public virtual Sections Section { get; set; }
}

Problem

Problem is for either version I get the following error:

One or more validation errors were detected during model generation:

{MyProjectName}.DataContexts.IdentityUserLogin: : EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType.

{MyProjectName}.DataContexts.IdentityUserRole: : EntityType 'IdentityUserRole' has no key defined. Define the key for this EntityType.

IdentityUserLogins: EntityType: EntitySet 'IdentityUserLogins' is based on type 'IdentityUserLogin' that has no keys defined.

IdentityUserRoles: EntityType: EntitySet 'IdentityUserRoles' is based on type 'IdentityUserRole' that has no keys defined.

Question(s)

How can I use DataAnnotations to get this to work like the IdentityUserRoles without, if possible, creating a third class to extend the ApplicationUser class?

Update #1

Based on answers given, here is some more info. I did create a secondary context to keep it away from the identity context. When I tried using the same context used with identity portion it worked. However, is there a way to do this using a different context?

RoLYroLLs

As commented by Stephen Reindl and Steve Greene, the solution to my problem was to use the same context that I was using for the ApplicationUser.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Create a Foreign Key Using "Code First" but actually have the DB First

From Dev

How do I create multiple 1:1 foreign key relationships in Entity Framework 6 Code First?

From Dev

Psycopg2 to create a record using a foreign key

From Dev

Can I create a bidirectional foreign key relationship using Entity Framework Code First data annotations?

From Dev

.net core 2 Entity Framework - ApplicationUser Extended Foreign Key object not populating on get

From Dev

How to create foreign key for 2 tables?

From Dev

How to create foreign key for 2 tables?

From Dev

Create a foreign key to a table that is not part of the context in Code First

From Dev

Entity framework code first cant create primary and foreign key relationship

From Dev

Relate Code-First to Identity ApplicationUser in .NET Core 2.0

From Dev

How to define a table that its primary key is constructed from 2 foreign keys with EF code-first

From Dev

MySQL 1 foreign key referencing 2 primary

From Dev

Circular foreign key code first

From Dev

Get foreign key value using Entity Framework code first

From Dev

Foreign key is created incorrrectly using EF code first

From Dev

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

From Dev

How can I reference a Foreign Key from db1 in db2 using MySQL

From Dev

Is it possible to create a Foreign Key on 2 columns with differing Collations?

From Dev

Entity Framework Code First Foreign Key issue

From Dev

Disable Foreign Key Constraint Code First EF

From Dev

Code first foreign key association MVC Entity

From Dev

EF Code First Foreign Key Same Table

From Dev

Entity Framework Code First Foreign Key issue

From Dev

EF Code First foreign key with multiple keys

From Dev

Foreign Key in Code First Entity Framework

From Dev

Foreign Key with Fluent API - Code First

From Dev

Entity Framework: Foreign Key in code first

From Dev

Code first approach not creating foreign key

From Dev

EF Code First Foreign Key Relationship

Related Related

  1. 1

    Create a Foreign Key Using "Code First" but actually have the DB First

  2. 2

    How do I create multiple 1:1 foreign key relationships in Entity Framework 6 Code First?

  3. 3

    Psycopg2 to create a record using a foreign key

  4. 4

    Can I create a bidirectional foreign key relationship using Entity Framework Code First data annotations?

  5. 5

    .net core 2 Entity Framework - ApplicationUser Extended Foreign Key object not populating on get

  6. 6

    How to create foreign key for 2 tables?

  7. 7

    How to create foreign key for 2 tables?

  8. 8

    Create a foreign key to a table that is not part of the context in Code First

  9. 9

    Entity framework code first cant create primary and foreign key relationship

  10. 10

    Relate Code-First to Identity ApplicationUser in .NET Core 2.0

  11. 11

    How to define a table that its primary key is constructed from 2 foreign keys with EF code-first

  12. 12

    MySQL 1 foreign key referencing 2 primary

  13. 13

    Circular foreign key code first

  14. 14

    Get foreign key value using Entity Framework code first

  15. 15

    Foreign key is created incorrrectly using EF code first

  16. 16

    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

  17. 17

    How can I reference a Foreign Key from db1 in db2 using MySQL

  18. 18

    Is it possible to create a Foreign Key on 2 columns with differing Collations?

  19. 19

    Entity Framework Code First Foreign Key issue

  20. 20

    Disable Foreign Key Constraint Code First EF

  21. 21

    Code first foreign key association MVC Entity

  22. 22

    EF Code First Foreign Key Same Table

  23. 23

    Entity Framework Code First Foreign Key issue

  24. 24

    EF Code First foreign key with multiple keys

  25. 25

    Foreign Key in Code First Entity Framework

  26. 26

    Foreign Key with Fluent API - Code First

  27. 27

    Entity Framework: Foreign Key in code first

  28. 28

    Code first approach not creating foreign key

  29. 29

    EF Code First Foreign Key Relationship

HotTag

Archive