MVC代码首先是数据库没有播种

时间:2016-01-15 12:39:40

标签: asp.net-mvc model-view-controller

当我在Global.asax中初始化数据库时 Database.SetInitializer(new Models.TeamInits());  但是当我运行数据库创建但没有表格的代码时,我尝试在VS 2013中使用的webconfig中初始化它,但是自从我升级到vs 2015后,它就不再有任何帮助了。

    protected override void Seed(TeamContext context)
    {
        var teams = new List<Team>
            {
                new Team(){TeamName = "Team1", Players = new List<Player> { new Player{PlayerName = "Bav", HighScore = 25 }, new Player {PlayerName = "NAD", HighScore = 35 } } },
                new Team(){TeamName = "Team2", Players = new List<Player> { new Player{PlayerName = "Bavva", HighScore = 25 }, new Player {PlayerName = "dads", HighScore = 3554 } } },
                new Team(){TeamName = "Team3", Players = new List<Player> { new Player { PlayerName = "Bav", HighScore = 25 }, new Player {PlayerName = "Mama", HighScore = 35 } }  },
                new Team(){TeamName = "Team4", Players = new List<Player> { new Player{PlayerName = "Bav", HighScore = 25 }, new Player {PlayerName = "waws", HighScore = 456156  }  } },
                new Team(){TeamName = "Team5" }

            };

        teams.ForEach(t => context.Teams.Add(t));
        context.SaveChanges();


    }

}

public class Team
{
    public int ID { get; set; }
    public string TeamName { get; set; }

    public virtual ICollection<Player> Players { get; set; }

}

public class Player
{
    public int ID { get; set; }
    public string PlayerName { get; set; }

    public int HighScore { get; set; }

    public virtual Team Team { get; set; }

}

public class TeamContext : DbContext
{

    public TeamContext()
        : base("TeamContext")
    { }

    public DbSet<Team> Teams { get; set; }
    public DbSet<Player> Players { get; set; }


}

0 个答案:

没有答案