对象关系映射(英语:Object Relational Mapping,简称ORM,或O/RM,或O/R mapping),是一种程序设计技术,用于实现面向对象编程语言里不同类型系统的数据之间的转换。
先说下遇到的问题:
零、主键问题。EFCORE里面需要在重写OnModelCreating里面设置HasNokey。但是EF里面没有这个(也可能我没找到),去网上找了个解决方案,设置伪主键。具体代码里面会体现
一、EF
1:nuget引入:EntityFrameWork,System.Data.Sqlite
2:DBContext:
public class MyEFDbContext : DbContext { public MyEFDbContext() : base("SqliteTest") { } /// <summary> /// 使用DbConnection连接数据库 /// </summary> /// <param name="con"></param> public MyEFDbContext(DbConnection con) : base(con, true) { } /// <summary> /// 利用连接字符串连接数据库 /// </summary> public MyEFDbContext(string connStr) : base(connStr) { } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();//移除复数表名的契约 modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();//禁用一对多级联删除 modelBuilder.Entity<TaskInfoEx>().HasKey(x => new { x.TaskGuid, x.MetaType });//对于没有主键的实体可以设置伪主键 //modelBuilder.Entity.ha //base.OnModelCreating(modelBuilder); } //实体类 public DbSet<UserInfo> UserInfo { get; set; } public DbSet<TaskInfoEx> TaskInfoEx { get; set; } }
3:使用
//DbConnection连接数据库 System.Data.Common.DbConnection sqliteCon = System.Data.SQLite.EF6.SQLiteProviderFactory.Instance.CreateConnection(); sqliteCon.ConnectionString = @"Data Source=C:\Users\FanLin\Desktop\DB\Product\IngestMatchLessData.db;"; sqliteCon.Open(); using (EF.MyEFDbContext db = new EF.MyEFDbContext(sqliteCon)) { var u = db.UserInfo.Where(x => x.LoginName != "").FirstOrDefault(); var user = db.UserInfo.Where(x => x.UserCode == "rrr").FirstOrDefault(); if (user != null) { //增 db.Set<UserInfo>().Add(new UserInfo() { UserCode = "test" }); //删 db.Set<UserInfo>().Remove(user); //查 user = db.UserInfo.Where(x => x.UserCode == "test").FirstOrDefault(); user.Password = "asdasd"; //改 db.Entry<UserInfo>(user).State = System.Data.Entity.EntityState.Modified; //保存以上,这样数据库才会生效 db.SaveChanges(); } Console.WriteLine(u.Password); }
二、EFCore
EFCore和EF差不多的用法。
1:引入(注意版本一致性):
2:DBContext
public class TaskInfoEx { public string TaskGuid { get; set; } public string MetaType { get; set; } public string MetaValue { get; set; } public string MetaDesc { get; set; } } public class UserInfo { [Key] public int UserID { get; set; } public string UserCode { get; set; } public string Password { get; set; } public string LoginName { get; set; } public string UserName { get; set; } public string UserDesc { get; set; } public string Authority { get; set; } } //需要引用Microsoft.EntityFrameworkCore和Microsoft.EntityFrameworkCore.sqlite 版本需要对应 class MyEFDBContext : DbContext { public virtual DbSet<TaskInfoEx> TaskInfoEx { get; set; } public virtual DbSet<UserInfo> UserInfo { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlite(@"Data Source=C:\Users\FanLin\Desktop\DB\Product\IngestMatchLessData.db"); } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<TaskInfoEx>(entity => { entity.HasNoKey();//无主键 }); } }
3:使用(使用方法差不多)
using (MyEFDBContext db = new MyEFDBContext()) { //查 var user = db.UserInfo.Where(x => x.UserCode == "rrr").FirstOrDefault(); db.Set<UserInfo>().Add(new UserInfo() {UserCode="test" }); db.Set<UserInfo>().Remove(user); user = db.UserInfo.Where(x => x.UserCode == "test").FirstOrDefault(); user.Password = "asdasd"; db.Entry<UserInfo>(user).State = Microsoft.EntityFrameworkCore.EntityState.Modified; db.SaveChanges(); //db.Entry<UserInfo>(new UserInfo()).State = Microsoft.EntityFrameworkCore.EntityState.Added; // Console.WriteLine(user[0].LoginName);
三、SqlSugar
1:引入SqlSugar
2:client。SqlSugar的client可以写成静态的方便调用
public class SqlSugarDemo { public static SqlSugarClient client = new SqlSugarClient(new SqlSugar.ConnectionConfig { ConnectionString = @"Data Source=C:\Users\FanLin\Desktop\DB\Product\IngestMatchLessData.db;Version = 3;", DbType = DbType.Sqlite, IsAutoCloseConnection = true, InitKeyType = InitKeyType.Attribute }); }
3:使用
//sqlsugar var list = SqlSugarDemo.client.Queryable<TaskInfoEx>().ToList();//查询所有 var data = new BaseConfig() { ConfigName = "jack" }; //SqlSugarDemo.client.Insertable(data).ExecuteCommand(); //主键 data.ConfigName = "jack"; //修改的东西 data.ConfigValue = "asdada"; SqlSugarDemo.client.Updateable<BaseConfig>(data).ExecuteCommand(); /*删除*/ SqlSugarDemo.client.Deleteable<BaseConfig>("jack").ExecuteCommand();
SqlSugar的使用要简单的多,当然我也只是初窥门庭,若有不对及需修改之处,贻笑大方,还望指教。
由于平常没有怎么使用过这些框架,这也只是学习之后的一个汇总,若以后有使用的更深层次的东西,也会一并修改。