I have 3 entities:
public class AspNetUser
{
public string Id {get; set;}
}
public class Event
{
public int Id {get; set;}
}
public class UserEvent
{
[Column(Order=0), Key, ForeignKey("AspNetUsers")]
public string UserId { get; set; }
[Column(Order=1), Key, ForeignKey("Events")]
public int EventId { get; set; }
public DateTime EnrolTime { get; set; }
public virtual AspNetUser User { get; set; }
public virtual Event Event { get; set; }
}
As you can see that, the UserEvent is just a relationship table which has both UserId and EventId from 2 tables.
My question is:
- Is annotation enough to tell EF to create the 2 foreign keys? Or I have to also use Fluent API to do it in the OnModelCreating() method in the DbContext class? Or I have to create a configuration class to do so? I saw something in this post(it makes me confused):
Entity Framework Multiple Column as Primary Key by Fluent Api
- ForeignKey("X") I guess the X should be the table name rather than the entity's name, right? E.g. X should be the AspNetUsers(which is in the database), rather than AspNetUser which is the entity name.
Thank you.