I would like to insert values into PostgreSql table using code first migration. My code so far:
public override void Up()
{
var test = "TestValue";
Sql(@"Insert into dbo.""Company"" (""Id"", ""Name"") values ('2', test)");
}
This is sample code. What I am trying to achieve is insert precomputed value to Name
column. The above code will fail with the message there is no column named test
but my intention is to substitute the value of test
. Please help me with the proper syntax.
You have 2 possibilities either using string concatenation or the String.Format. If you need formatted output e.g. number of decimals in a number etc. then you should use String.Format.
var test = "TestValue";
string sqlString = @"Insert into dbo.""Company"" (""Id"", ""Name"") values ('2', '" + test + @"')";
Sql(sqlString);
var test = "TestValue";
string sqlString = string.Format(@"Insert into dbo.""Company"" (""Id"", ""Name"") values ('2', '{0}')", test);
Sql(sqlString);
Your sqlString
will contain:
Insert into dbo."Company" ("Id", "Name") values ('2', 'TestValue')
after it.