AspNetUsers' ID as Foreign key in separate table, one-to-one relationship

asp.net c# database entity-framework one-to-one

Question

I have searched high and low and attempted every possible method of storing a foreign key from the AspNetUser table in a different Customer table. Despite the fact that I'm still a beginner with ASP.NET and the Entity Framework, I've read a lot of postings and material.

I currently just have this.

MODELS

public class Customer
{
    [Display (Name="Customer ID")]
    public int CustomerID { get; set; }

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

}


 public class ApplicationUser : IdentityUser
{
    public virtual Customer Customer { get; set; }
}

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public DbSet<Customer> Customers { get; set; }

    public ApplicationDbContext()
        : base("DefaultConnection")
    {
    }

}

I encounter this flaw, quote

Unable to determine the principal end of an association between the types 'TestApplication.Models.Customer' and 'TestApplication.Models.ApplicationUser'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.

I also used the technique described by this individual here: The relationship fluent API or data annotations must be used to explicitly configure the principal end of this association.

I followed the person's advice and used the "modelBuilder" method, commenting out the ForeignKey annotations. And after I updated my database, the CustomerID as a ForeignKey was also in the AspNetUsers table, which is not what I wanted. However, the "Id" from the AspNetUsers table was in the Customers table (which is okay).

I need the AspNetUsers' "Id" to be a ForeignKey in the Customers table.

1
16
5/23/2017 12:17:26 PM

Accepted Answer

The "child" table in a one-to-one relation, in your exampleCustomer , which is the foreign key, ought to share the same primary key as the associated table.

Your provided code sample indicates that, inCustomer Your PK will be namedCustomerID which contrasts withUserId .

This ought to operate in your situation (untested):

public class Customer
{
    [Key]
    public string UserId { get; set; }

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

public class ApplicationUser : IdentityUser
{
    public virtual Customer Customer { get; set; }
}

Edit:

ForeignKeyAttribute MSDN reports:

If you add the ForeigKey attribute to a foreign key property, you should specify the name of the associated navigation property. If you add the ForeigKey attribute to a navigation property, you should specify the name of the associated foreign key(s).

I read this to mean that either adding the ForeignKey-attribute to the navigation property or the foreign key property should work, but that doesn't seem to be the case. It should work after being moved as shown below.

public class Customer
{
    [Key, ForeignKey("ApplicationUser")]
    public string UserId { get; set; }
    public virtual ApplicationUser ApplicationUser { get; set; }
}

public class ApplicationUser : IdentityUser
{
    public virtual Customer Customer { get; set; }
}
15
9/17/2014 9:56:51 AM

Popular Answer

Although this post was published two years ago, using Fluent API to set the foreign key might be a better option.[ForeignKey] in the Customer class you're using. Here's how you'd go about it:

public class Customer
{
    public int CustomerID { get; set; }
    public virtual ApplicationUser ApplicationUser { get; set; }
}

public class ApplicationUser : IdentityUser
{
    public virtual Customer Customer { get; set; }
}

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

    public DbSet<Customer> Customers { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        // one-to-zero or one relationship between ApplicationUser and Customer
        // UserId column in Customers table will be foreign key
        modelBuilder.Entity<ApplicationUser>()
            .HasOptional(m => m.Customer)
            .WithRequired(m => m.ApplicationUser)
            .Map(p => p.MapKey("UserId"));
    }

}

It would result in aUserId table in yourCustomers a table to which it is a foreign keyAspNetUsers table. You may omit..Map(p => p.MayKey("UserId")) EF will then identify the foreign key.ApplicationUser_Id by tradition.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow