Debug code-first Entity Framework migration codes

后端 未结 7 754
悲&欢浪女
悲&欢浪女 2020-11-27 10:16

I\'m using Entity Framework code first in my website and I\'m just wondering if there is any way to debug the migration codes. You know, like setting breakpoints and stuff l

7条回答
  •  星月不相逢
    2020-11-27 10:49

    My answer might be a bit silly but anyway here it goes. If you, like me, some times have problems in the Seed() method what I usually do is simply create a public method that calls the Protect Seed().

    public void SeedDebug(AppDbContext context)
    {
        Seed(context);
    }
    

    then in my HomeController I call this method in Debug mode.

    public class HomeController : Controller
    {
        var appDb = new AppDbContext();
        public ActionResult Index()
        {
            var config = new Configuration();
            config.SeedDebug(appDb);
            return View();
        }
    }
    

    I know it's a bit lame solution, but it's simple and quick. Of course this has to be done after the model been created. So step by step:

    1. comment the seed method and execute the update-database to create the model
    2. uncomment the method Seed() and plugin the "hack" I mentioned above.

    3. in the configuration disable Auto migrations

      AutomaticMigrationsEnabled = false;//if you have this disabled already skip this step

    4. Debug your application, fix the error and remove the "hack"

提交回复
热议问题