エラー CS1503
引数 2: は 'string' から 'System.FormattableString' へ変換することはできません
using Microsoft.EntityFrameworkCore;
namespace EntityFrameworkCoreOperateTableSqlServer
{
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button5_Click(object sender, EventArgs e)
{
MyDbContext cx = new MyDbContext();
cx.Database.ExecuteSql("DROP TABLE MyTable1");
textBox1.Text += "SQLを実行しました。\r\n";
}
}
}
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
namespace EntityFrameworkCoreOperateTableSqlServer
{
public class MyDbContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder options)
{
options.UseSqlServer("Data Source=(DBホスト名またはIPアドレス);Initial Catalog=iPentecSandBox3;User ID=(ユーザーID);Password=(パスワード);Encrypt=False");
}
}
}
System.FormattableString
のため、文字列型string
をそのまま渡すことはできません。"
の手前に $
を記述すると、FormattableString になり、ExecuteSqlメソッドに渡せます。 private void button5_Click(object sender, EventArgs e)
{
MyDbContext cx = new MyDbContext();
cx.Database.ExecuteSql($"DROP TABLE MyTable1");
textBox1.Text += "SQLを実行しました。\r\n";
}
private void button5_Click(object sender, EventArgs e)
{
MyDbContext cx = new MyDbContext();
cx.Database.ExecuteSql(FormattableStringFactory.Create("DROP TABLE MyTable1") );
textBox1.Text += "SQLを実行しました。\r\n";
}