1.定义泛型类 namespace Crm.Data.Logic.Repository{ public abstract class AbstractRepository<TC, T> : IDisposable where TC : DbContext, new() where T : class { private TC _entities = new TC(); private bool _disposed;
public class BaseRepository<T> where T : class { //实例化EF框架 DataModelContainer db = new DataModelContainer(); //添加 public T AddEntities(T entity) { db.Entry<T>(entity).State = EntityState.Added;
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { //创建数据库实体 ModelStudentContainer DemoDB = new ModelStudentContainer(); #region 增加数据
1.创建项目 创建Data文件夹 创建实体类Students/cs public class Students { public Guid Id { get; set; } public string Name { get; set; } public int Age { get; set; } } 创建MyDbContext public class MyDbContext:DbContext { public MyDbContext(DbContextOptions<MyDbContext>