1
我正在構建一個asp.net應用程序。我有兩節課,一節是電影和一節課,在那裏我實現了多方面的關係。表中沒有顯示在asp.net中的關係mvc5
主任級
public class Director
{
private ICollection<Film> _films;
public Director()
{
_films = new List<Film>();
}
public int DirectorID { get; set; }
public string fName { get; set; }
public string lName { get; set; }
public int Age { get; set; }
public virtual ICollection<Film> Films
{
get { return _films; }
set { _films = value; }
}
}
薄膜級
public class Film
{
private ICollection<Director> _directors;
public Film()
{
_directors = new List<Director>();
}
public int FilmId { get; set; }
public string Name { get; set; }
public int Minutes { get; set; }
public string AgeClassification { get; set; }
public virtual ICollection<Director> Directors
{
get { return _directors; }
set { _directors = value; }
}
}
種子法
protected override void Seed(FilmNetwork.Models.FilmNetworkContext context)
{
var films = new List<Film>
{
new Film() { FilmId = 1, Name = "The Dark Knight", Minutes = 152, AgeClassification = "15A" },
new Film() { FilmId = 2, Name = "The Social Network", Minutes = 120, AgeClassification = "15A" },
new Film() { FilmId = 3, Name = "Up In The Air", Minutes = 109, AgeClassification = "15A" },
new Film() { FilmId = 4, Name = "Slumdog Millionaire", Minutes = 120, AgeClassification = "15A" },
new Film() { FilmId = 5, Name = "The Wolf Of Wall Street", Minutes = 180, AgeClassification = "18" },
new Film() { FilmId = 6, Name = "King Kong", Minutes = 221, AgeClassification = "12" },
new Film() { FilmId = 7, Name = "Schindler's List", Minutes = 195, AgeClassification = "18" }
};
films.ForEach(f => context.Films.AddOrUpdate(film => film.Name, f));
context.SaveChanges();
var directors = new List<Director>
{
new Director() { DirectorID = 1, fName = "Christopher", lName = "Nolan", Age = 46,
Films = films.Where(f => (f.Name == "The Dark Knight")).ToList()},
new Director() { DirectorID = 2, fName = "David", lName = "Fincher", Age = 54,
Films = films.Where(f => (f.Name == "The Social Network")).ToList() },
new Director() { DirectorID = 3, fName = "Jason", lName = "Reitman", Age = 39,
Films = films.Where(f => (f.Name == "Up In The Air")).ToList() },
new Director() { DirectorID = 4, fName = "Danny", lName = "Boyle", Age = 60,
Films = films.Where(f => (f.Name == "Slumdog Millionaire")).ToList() },
new Director() { DirectorID = 5, fName = "Martin", lName = "Scorsese", Age = 74,
Films = films.Where(f => (f.Name == "The Wolf Of Wall Street")).ToList() },
new Director() { DirectorID = 6, fName = "Peter", lName = "Jackson", Age = 55,
Films = films.Where(f => (f.Name == "King Kong")).ToList() },
new Director() { DirectorID = 7, fName = "Steven", lName = "Spielberg", Age = 70,
Films = films.Where(f => (f.Name == "Schindler's List")).ToList() },
};
directors.ForEach(d => context.Directors.AddOrUpdate(director => director.lName, d));
context.SaveChanges();
我有一個電影製片基於ID的AJAX搜索。它返回電影,導演領域的董事是空白的,因爲這種關係沒有被執行。
我不需要多到很多,但我不知道如何implment一對多:) :) – andre
我編輯我的答案希望它有幫助。 – Geoman