using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MySQLuse
{
class Program
{
staticvoid Main(string[] args)
{
using (var entity = new ExampleDBContext())
{
//查询,删除
List<User> list = entity.Users.ToList<User>();
for (int i = 0; i < list.Count; i++)
{
entity.Entry(list[i]).State = EntityState.Deleted;
Console.WriteLine(list[i].Id);
entity.SaveChanges();
}
//增加
User user = new User() { Name = "雷克斯 阿帕奇", Email = "wutqqqqqq@gmail.com" };
user = entity.Users.Add(user);
entity.SaveChanges();
Console.ReadKey();
}
}
}
}
ExampleDBContext.cs
using MySql.Data.Entity;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespaceMySQLuse
{
[DbConfigurationType(typeof(MySqlEFConfiguration))]
publicclassExampleDBContext : DbContext
{public ExampleDBContext() : base("name=MyContext")
{
//Configuration.ProxyCreationEnabled = true;//Configuration.LazyLoadingEnabled = true;
Database.SetInitializer(new MigrateDatabaseToLatestVersion<ExampleDBContext, Migrations.Configuration>());
}
public ExampleDBContext(DbConnection existingConnection, bool contextOwnsConnection)
: base(existingConnection, contextOwnsConnection)
{
Configuration.ProxyCreationEnabled = true;
Configuration.LazyLoadingEnabled = true;
Configuration.AutoDetectChangesEnabled = true;
}
protectedoverridevoid OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
//modelBuilder.Entity<User>();
}
public DbSet<User> Users { get; set; }
}
publicclassUser
{publicint Id { get; set; }
public String Name { get; set; }
public String Email { get; set; }
}
}
Configuration.cs
namespace MySQLuse.Migrations
{
using System;
using System.Data.Entity;
using System.Data.Entity.Migrations;
using System.Linq;
internalsealedclass Configuration : DbMigrationsConfiguration<EF6CodeFirstMySQL.ExampleDBContext>
{
publicConfiguration()
{
AutomaticMigrationsEnabled = false;
//AutomaticMigrationDataLossAllowed = true;
}
protectedoverridevoidSeed(EF6CodeFirstMySQL.ExampleDBContext context)
{
// 迁移到最新版本后将调用此方法// 您可以使用 DbSet<T>.AddOrUpdate() 辅助扩展方法来避免创建重复的种子数据。 例如。 // context.People.AddOrUpdate(// p => p.FullName,// new Person { FullName = "Andrew Peters" },// new Person { FullName = "Brice Lambson" },// new Person { FullName = "Rowan Miller" }// );//
}
}
}
App.config
<?xml version="1.0" encoding="utf-8"?><configuration><configSections><sectionname="entityFramework"type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"requirePermission="false"/></configSections><connectionStrings><addname="ExampleStr"connectionString=" data source=(host);initial catalog=Example;persist security info=True;user id=user;password=pwd;MultipleActiveResultSets=True;App=EntityFramework"providerName="System.Data.SqlClient"/><addname="MyContext"providerName="MySql.Data.MySqlClient"connectionString="server=localhost;port=3306;database=mvcexample;uid=root;password=123456"/></connectionStrings><startup><supportedRuntimeversion="v4.0"sku=".NETFramework,Version=v4.5"/></startup><entityFramework><defaultConnectionFactorytype="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework"/><providers><providerinvariantName="MySql.Data.MySqlClient"type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6"/><providerinvariantName="System.Data.SqlClient"type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/></providers></entityFramework><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=6.9.9.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"/></DbProviderFactories></system.data></configuration>