How to pass parameters to DbMigration.Sql() Method

2019-06-15 07:38发布

问题:

When using Entity Framework Migrations, the DbMigration base class has a Sql method which takes parameters in an anonymous object

I cannot for the life of me figure out how to use it.

        Sql(@"
                UPDATE dbo.SlideSets 
                SET Name = @Name, 
            ", false, new {
                Name = "Foo"
            }
        );

Results in the error

System.Data.SqlClient.SqlException (0x80131904): Must declare the scalar variable "@Name"

What's the correct syntax of this statement?

回答1:

The standard generator for MSSQL is doing nothing with the anonymous object in this case. You need to put the parameter directly into command. Or you can derive your own and handle it.



回答2:

I dug into the EF source code and it seems that this parameter (or rather the MigrationOperation.AnonymousObject property that's created from it) is not used at all!

I've created a ticket on their issue tracker to either do something about it or remove the api