using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MySQL
{
publicclass UserModel
{
[Key]
publicint UserId { get; set; }
[Required]
[DataType(DataType.Text)]
[Display(Name = "Username")]
publicstring UserName { get; set; }
[Required]
[DataType(DataType.Text)]
[Display(Name = "Gender")]
publicstring Gender { get; set; }
}
}
Configuration.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity.Migrations;
namespace MySQL
{
internalsealedclass Configuration : DbMigrationsConfiguration<ApplicationDbContext>
{
publicConfiguration()
{
AutomaticMigrationsEnabled = true;
ContextKey = "MySQL.ApplicationDbContext";
}
protectedoverridevoidSeed(ApplicationDbContext context)
{
}
}
internalsealedclass MySQLConfiguration : DbMigrationsConfiguration<MySQLDbContext>
{
publicMySQLConfiguration()
{
AutomaticMigrationsEnabled = true;
ContextKey = "MySQL.MySQLDbContext";
// 注册mysql代码生成器
SetSqlGenerator("MySql.Data.MySqlClient", new MySql.Data.Entity.MySqlMigrationSqlGenerator());
}
protectedoverridevoidSeed(MySQLDbContext context)
{
}
}
}
ApplicationDbContext.cs
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MySQL
{
publicclass ApplicationDbContext : DbContext
{
publicApplicationDbContext(string connectionString)
: base(connectionString)
{
}
public DbSet<UserModel> UserModels { get; set; }
}
}
MySQLDbContext.cs
using System;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Data.Common;
using MySql.Data.Entity;
namespaceMySQL {
[DbConfigurationType(typeof(MySqlEFConfiguration))]
publicclassMySQLDbContext : DbContext {public MySQLDbContext(): base("Server=localhost;Database=test;uid=root;pwd=123456;")
{ }
public DbSet<UserModel> UserModels { get; set; }
}
}
IRepository.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MySQL
{
publicinterface IRepository<T> : IDisposable
where T : class
{
IEnumerable<T> GetAll();
T Get(int id);
void Create(T item);
void Update(T item);
void Delete(int id);
void Save();
}
}
SQLRepository.cs
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MySQL
{
publicclass SQLRepository : IRepository<UserModel>
{
private ApplicationDbContext db;
publicSQLRepository(string connString)
{
this.db = new ApplicationDbContext(connString);
}
public IEnumerable<UserModel> GetAll()
{
return db.UserModels;
}
public UserModel Get(int id)
{
return db.UserModels.Find(id);
}
publicvoidCreate(UserModel item)
{
db.UserModels.Add(item);
}
publicvoidUpdate(UserModel item)
{
db.Entry(item).State = EntityState.Modified;
}
publicvoidDelete(int id)
{
UserModel item = db.UserModels.Find(id);
if (item != null)
db.UserModels.Remove(item);
}
publicvoidSave()
{
db.SaveChanges();
}
privatebool disposed = false;
publicvirtualvoidDispose(bool disposing)
{
if (!this.disposed)
{
if (disposing)
{
db.Dispose();
}
}
this.disposed = true;
}
publicvoidDispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
}
MySQLRepository.cs
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MySQL
{
publicclass MySQLRepository : IRepository<UserModel>
{
private MySQLDbContext db;
publicMySQLRepository()
{
this.db = new MySQLDbContext();
}
public IEnumerable<UserModel> GetAll()
{
return db.UserModels;
}
public UserModel Get(int id)
{
return db.UserModels.Find(id);
}
publicvoidCreate(UserModel item)
{
db.UserModels.Add(item);
}
publicvoidUpdate(UserModel item)
{
db.Entry(item).State = EntityState.Modified;
}
publicvoidDelete(int id)
{
UserModel item = db.UserModels.Find(id);
if (item != null)
db.UserModels.Remove(item);
}
publicvoidSave()
{
db.SaveChanges();
}
privatebool disposed = false;
publicvirtualvoidDispose(bool disposing)
{
if (!this.disposed)
{
if (disposing)
{
db.Dispose();
}
}
this.disposed = true;
}
publicvoidDispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
}
Program.cs
using System;
using System.Collections.Generic;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.Migrations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MySQL
{
class Program
{
privatestaticstring connectionStr = "Server=WU-PC;Database=Test;user id=sa;Password =123456;MultipleActiveResultSets=true";
privatestaticstring connectionMySQLStr = "Server=localhost;Database=test;uid=root;pwd=123456;";
privatestatic IRepository<UserModel> repo;
privatestaticbool useMySQl = true;
staticvoid Main(string[] args)
{
// 创建SQL数据库
MigrateDb();
// 创建MySQL数据库
MigrateMySQLDb();
// 选择 repositoryif (useMySQl)
{
repo = new MySQLRepository();
}
else
{
repo = new SQLRepository(connectionStr);
}
// 使用 repositoryusing (repo)
{
repo.Create(new UserModel { UserName = "Test", Gender = "Test" });
repo.Save();
foreach (var u in repo.GetAll())
{
//Console.WriteLine($"{u.UserId}.{u.UserName} - {u.Gender}");
Console.WriteLine(string.Format("{0}.{1} - {2}", u.UserId, u.UserName, u.Gender));
}
}
Console.Read();
}
publicstaticvoidMigrateDb()
{
var configuration = new Configuration();
configuration.TargetDatabase = new DbConnectionInfo(connectionStr, "System.Data.SqlClient");
var migrator = new DbMigrator(configuration);
migrator.Update();
}
publicstaticvoidMigrateMySQLDb()
{
var configuration = new MySQLConfiguration();
configuration.TargetDatabase = new DbConnectionInfo(connectionMySQLStr, "MySql.Data.MySqlClient");
var migrator = new DbMigrator(configuration);
migrator.Update();
}
}
}