How to pass parameters to DbMigration.Sql() Method

2019-06-15 07:45发布

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?

2条回答
不美不萌又怎样
2楼-- · 2019-06-15 08:33

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

查看更多
小情绪 Triste *
3楼-- · 2019-06-15 08:36

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.

查看更多
登录 后发表回答