How to Declare Foreign Key in Entity Framework

There are 2 ways to declare foreign key in entity framework. Assuming we have 2 entities aspnet_Users and Person, which are link via UserId from aspnet_Users. 1.)
class Person {
  public Guid UserId { get;set; }
  [ForeignKey("UserId")]
  public virtual AspnetUser AspnetUser { get; set; }
}
2.)
  [ForeignKey("AspnetUser")]
  public Guid UserId { get;set; }  
  public virtual AspnetUser AspnetUser { get; set; }

0 Comments

Post a Comment

Post a Comment (0)

Previous Post Next Post
NERV Open Source
Building production Spring Boot systems?

Explore NERV — open-source Java libraries for audit trails, persistence, exception handling, and reliable event-driven architecture.