I have a table:
public class TestModel
{
public int Id { get; set; }
public string UserId { get; set; }
public string Name { get; set; }
}
I want UserId column to be a foreign key, Mvc Membership's User's Id column. How can I achive this?
My IdentityModels:
public class ApplicationUser : IdentityUser
{
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection")
{
}
}
Add a reference to the ApplicationUser
and specify the ForeignKey:
public class TestModel
{
public int Id { get; set; }
public string Name { get; set; }
public string UserId { get; set; }
[ForeignKey("UserId")]
public ApplicationUser User { get; set; }
}
Add the new model to your DbContext:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public DbSet<TestModel> TestModels { get; set; }
/* rest of class */
}
And you should be good-to-go (less migrations/database updates).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With