Currently I am developing a C# project using Entity Framework with code-first approach. I am stuck with the use of static (reference) data.
Let we assume that there are the "movie" and "movie genre" entities as:
public class Movie
{
[Key]
public int ID;
public String Name;
public DateTime Year;
public String GenreCode;
[ForeignKey("GenreCode")]
public virtual Genre Genre { get; set; }
}
public class Genre
{
[Key]
public String Code;
//description of the code
public String Desc;
//Should I put this???
public virtual ICollection<Movie> Movies { get; set; }
}
A movie can have one genre but a genre can belong to multiple genres. In this case do I have to put "public virtual ICollection Movies { get; set; }" in the Genre class? I believe I should not put this since Genre is a static data.However I am not sure. Also I don't want to use enum for genre in the code since in the future new genres can be defined in the database.
What is the best way to handle this kind of relationship using entity framework?
Thank you.
Regards,
No, you don't have to put public virtual ICollection Movies { get; set; }
there, if you don't need to do something similar to the following:
Genre genre = ....;
var movies = genre.Movies.ToList();
The one-to-many relationship between Genre and Movie will be created no matter you have a Movies
collection in Genre or not.