What you are getting is the default behavior of EF Code First in terms of mapping a 1 to Many association to the database.
For example you have a ICollection<File>
on Person class, as a result, EF will create a FK on Files table (PersonId) and map it to Id PK on Persons table.
Now, my guess is that you like to have a many to many relationship between File and Person, so that each file can relates to many Persons and each Person can have many files (same story for Event object as well). One way to achieve this is to put navigation properties on File class pointing to Event and Person classes. So, your model should be changed to this:
public class File {
public int Id { get; set; }
public string Path { get; set; }
public virtual ICollection<Event> Events { get; set; }
public virtual ICollection<Person> Persons { get; set; }
}
public class Event {
public int Id { get; set; }
public string EventName { get; set; }
public virtual ICollection<File> Files {get;set;}
}
public class Person {
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<File> Files { get; set; }
}
public class MyContext : DbContext {
public DbSet<Person> Persons { get; set; }
public DbSet<Event> Events { get; set; }
public DbSet<File> Files { get; set; }
}
As a result, EF will create link tables (Events_Files and Files_Persons) to map these many to many associations to the database.
Update:
When using POCOs with EF, if you mark your navigation properties as virtual you will opt-in to some of the additional EF supports like Lazy Loading and Relationship Fixup. So in general have a virtual keyword in navigation properties considered to be a good practice.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…