How to explicitly name the database when using Entity Framework Migrations 4.3

前端 未结 4 1016
孤城傲影
孤城傲影 2020-12-12 19:04

I\'ve recently started using Entity Framework migrations and noticed that the database name is not pulling through for me when I run the Update-Database command

4条回答
  •  长情又很酷
    2020-12-12 19:35

    I tried with Latest EF5 from Nuget.

    However Update-Database does not read the App.config from the project that contain the migrations (just like the answer 1 year ago) but it will only read *.config from start up project. It is great but I discover how Add-Migration and Update-Database find a suitable connection string here:

    1. It trying to get "DefaultConnection" connection string first
    2. Then it is trying to get the connection string name based on context class name. E.g. I have the MyContext class derived from DbContext so I can use the "MyContext" connection string name. Useful when I have multiple db connections.
    3. If both the above connection string names are not found, it will fail and show no "DefaultConnection" connection string unless you supply the -ConnectionStringName parameter. See get-help Update-Database to view the help page in the Package Manager Console.

    There is no retry or fallback attempt, so if the "DefaultConnection" contains a wrong connection string, it will simply show an error.

    If both DefaultConnection and context name exist in the connection strings, DefaultConnection will take precedence.

    I would prefer #2 become the first try because the name is more specific but the above steps is what EF5 Migrations do when trying to connect to the db.

提交回复
热议问题