<connectionStrings><addname="MyDbContext"providerName="MySql.Data.MySqlClient"connectionString="server=127.0.0.1;port=3306;database=cc;user=root;password=123456;persistsecurityinfo = True;Character Set=utf8;"/></connectionStrings><system.data><DbProviderFactories><removeinvariant="MySql.Data.MySqlClient"/><addname="MySQL Data Provider"invariant="MySql.Data.MySqlClient"description=".Net Framework Data Provider for MySQL"type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=9.1.0.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"/></DbProviderFactories></system.data>
测试
publicpartialclassMainWindow:Window{publicMainWindow(){InitializeComponent();try{using(var context =newMyDbContext()){// 查询var entities = context.MyEntities.Where(e => e.Name =="example").ToList();// 添加var newEntity =newMyEntity{ Name ="new example"};context.MyEntities.Add(newEntity);context.SaveChanges();// 更新//var existingEntity = context.MyEntities.Find(newEntity.Id);//existingEntity.Name = "updated example";//context.SaveChanges();// 删除// context.MyEntities.Remove(existingEntity);//context.SaveChanges();}}catch(Exception ex){var d ="";}}}[DbConfigurationType(typeof(MySql.Data.EntityFramework.MySqlEFConfiguration))]publicclassMyDbContext:DbContext{publicMyDbContext():base("name=MyDbContext"){}publicDbSet<MyEntity> MyEntities {get;set;}}[Table("my_table")]publicclassMyEntity{[Key][DatabaseGenerated(DatabaseGeneratedOption.Identity)]publicint Id {get;set;}[Required][StringLength(100)]publicstring Name {get;set;}}