How to define foriegn key in my own classes.

Imran Jalali 101 Reputation points
2021-10-10T13:37:07.36+00:00

Hi Folks,

I am not sure if this is the right place to ask my question but it is somehow related to EF Core & Identity.

I am using ASP.NET Core Identity in my project. I have added my own custom tables along with the tables created by Microsoft Identity automatically e.g
(ASPNetUsers, ASPNETRoles) etc..

Now my question is, I want to use the primary key of ASPNetUsers e.g Id in my own tables as foreign Key

Can anyone tell me how I can define that as Foreign Key in my own class.

Here is my code sample:

public partial class FileUserInfo
    {
        public FileUserInfo()
        {
            FileUserInfoDetails = new HashSet<FileUserInfoDetails>();

        }

        public int FileUserInfoId { get; set; }
        public string UserName { get; set; }
        public string RecipientEmailAddress { get; set; }

        // This is my foreign key.
        public string CreatedByEmployeeId { get; set; }

        public DateTime CreationDate { get; set; }

        // How to define this here?   as  the class is not created by Microsoft Identity by default.
        //public virtual AspNetUsers CreatedByEmployee { get; set; }

        public virtual ICollection<FileUserInfoDetails> FileUserInfoDetails { get; set; }
    }

I not willing to create separate dbcontext (Scaffolding) just for this reason....

Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
725 questions
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,380 questions
0 comments No comments
{count} votes

Accepted answer
  1. Zhi Lv - MSFT 32,091 Reputation points Microsoft Vendor
    2021-10-11T08:38:35.06+00:00

    Hi @Imran Jalali ,

     // How to define this here?   as  the class is not created by Microsoft Identity by default.  
     //public virtual AspNetUsers CreatedByEmployee { get; set; }  
    

    In Asp.net Core Identity, the IdentityUser class maps to the AspNetUsers table, so if you want to configure the foreign key, you can use the IdentityUser class, check the following code:

    public class FileUserInfo  
    {    
        [Key]  
        public int FileUserInfoId { get; set; }  
        public string UserName { get; set; }  
        public string RecipientEmailAddress { get; set; }  
    
        public DateTime CreationDate { get; set; }  
    
         // This is my foreign key. // use the ForieignKey attribute to assign the foreign key.  
        [ForeignKey("CreatedByEmployee")]  
        public string CreatedByEmployeeId { get; set; }  
        public virtual IdentityUser CreatedByEmployee { get; set; }  
    }  
    

    Adding the FileUserInfos in the DbContext

    public class ApplicationDbContext : IdentityDbContext  
    {  
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)  
            : base(options)  
        {  
        }   
        public DbSet<FileUserInfo> FileUserInfos { get; set; }  
    }  
    

    After migration, the result as below:

    139402-image.png

    More detail information about using Asp.net Core Identity, see Introduction to Identity on ASP.NET Core and Identity model customization in ASP.NET Core.


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Dillion


0 additional answers

Sort by: Most helpful