Can I inject dependency into migration (using EF-Core code-first migrations)?

前端 未结 3 1123
感动是毒
感动是毒 2021-02-19 03:20

I tried to inject IConfiguration into the migration (in constructor), and got exception: \"No parameterless constructor defined for this object.\"

any worka

3条回答
  •  佛祖请我去吃肉
    2021-02-19 03:58

    you cannot, the migrations need to be able to run outside the context of your application.

    Since the Entity-framework command-line tool analyzes your code but does not run the startup.cs class.

    Also it is not advisable. your migrations should be plain simple and not depend on anything. if it would, it could lead to major runtime side-effects where missing config could lead to missing tables or columns in production.

    additional advise

    If it involves a lot of small/equal/manual changes. Best way is to generate your migration file. Why? This way your migration will be deterministic: you know what the outcome will be. If a line in your migration fails, it is simple and clear why that is and easily(er) fixable.

提交回复
热议问题