MySqlを接続するためにエンティティフレームワークを使用しています。MySql データベースを使用してエンティティ データ モーダルを作成しました。web.config に接続文字列が自動的に生成されます。これは、私の場合は次のようになります。
<add name="entityframework1" connectionString="metadata=res://*/EntityFramework.csdl|res://*/EntityFramework.ssdl|res://*/EntityFramework.msl;provider=MySql.Data.MySqlClient;provider connection string="server=localhost;User Id=root;database=entityframework"" providerName="System.Data.EntityClient" />
今、データベースにレコードを保存したいのですが、コードは次のとおりです:-
employee emp = new employee();
emp.Name = txtName.Text;
emp.Age = Convert.ToInt32(txtAge.Text);
using (entityframework context =
new entityframework())
{
context.AddToemployees(emp);
if (context.SaveChanges() == 1)
{
lblMsg.Text = "Saved Successfully.";
}
}
それはエラーを与える
「context.SaveChanges」を持つif条件で「オブジェクト参照がオブジェクトのインスタンスに設定されていません」。
コンテキスト作成時に接続文字列も渡すようにしました
string connectionstring = "SERVER=localhost;DATABASE= entityframework ;UID= root;";
しかし、「Keyword not supported: '"server'.」というエラーが表示されます。これは、エンティティ フレームワークでの私の最初の試みです。