用C#写一个类 ,用泛型调用下 ,就在数据库中创建了一个这个类的表 里面有对应类里面的字段
反射,得到这个类型的所有属性,,,,
Code First ?
你是陈述句.那问题是什么?
public abstract class EntityControllerBase<T> : Controller , IEntityController where T : class
{
protected G5ManagerCenterEntities Db { get; } = new G5ManagerCenterEntities();
protected DbSet<T> DefaultTable => Db.Set<T>();
public string PrimaryKey { get; }
protected EntityControllerBase(string primaryKey = "ID") : base()
{
ViewBag.Title = GetType().Name.Replace("Controller", string.Empty);
PrimaryKey = primaryKey;
}
public virtual ActionResult Index()
{
return View();
}
public virtual LJsonResult List(GridSettings grid) => List<T>(grid, (queryable => queryable));
protected LJsonResult List<TFormatEntities>(GridSettings grid, Func<IQueryable<T>, IQueryable<TFormatEntities>> formatFun) where TFormatEntities : class
{
IQueryable<T> query = DefaultTable;
if (grid.IsSearch)//filtering
{
if (grid.Where.groupOp == "AND")//And
{
foreach (var rule in grid.Where.rules)
query = query.Where<T>(rule.field, rule.data, rule.op);
}
else//Or
{
var temp = (new List<T>()).AsQueryable();
foreach (var rule in grid.Where.rules)
{
var t = query.Where<T>(rule.field, rule.data, rule.op);
temp = temp.Concat<T>(t);
}
}
}
var extConditionQuery = formatFun.Invoke(query);
extConditionQuery = extConditionQuery.OrderBy(grid.SortColumn, grid.SortOrder);//sorting
var count = extConditionQuery.Count();//count
var data = extConditionQuery.Skip((grid.PageIndex - 1) * grid.PageSize).Take(grid.PageSize);//paging
var result = new ListParam<TFormatEntities>
{
total = (int)Math.Ceiling((double)count / grid.PageSize),
page = grid.PageIndex,
records = count,
rows = data,
};
return new LJsonResult(result);
}
public const string OperateAdd = "add";
public const string OperateModify = "edit";
public const string OperateDel = "del";
public virtual ActionResult Edit(T bean, string oper, string id)
{
switch (oper)
{
case OperateAdd:
using (var scope = Db.Database.BeginTransaction())
{
bean = Add(bean);
Db.SaveChanges();
scope.Commit();
}
return new LJsonResult(bean, 201);
case OperateModify:
using (var scope = Db.Database.BeginTransaction())
{
Update(bean);
Db.SaveChanges();
scope.Commit();
}
return new HttpStatusCodeResult(HttpStatusCode.OK);
case OperateDel:
using (var scope = Db.Database.BeginTransaction())
{
Delete(id);
Db.SaveChanges();
scope.Commit();
}
return new HttpStatusCodeResult(HttpStatusCode.OK);
default:
return new HttpStatusCodeResult(HttpStatusCode.NotFound);
}
}
protected virtual DbEntityEntry<T> BeforSaveAdd(T bean)
{
var entry = Db.Entry<T>(bean);
entry.State = EntityState.Added;
return entry;
}
protected virtual T Add(T bean)
{
bean = DefaultTable.Add(bean);
BeforSaveAdd(bean);
Db.SaveChanges();
return bean;
}
protected virtual DbEntityEntry<T> BeforSaveUpdate(T bean)
{
var entry = Db.Entry<T>(bean);
entry.State = EntityState.Modified;
return entry;
}
protected virtual void Update(T bean)
{
BeforSaveUpdate(bean);
Db.SaveChanges();
}
protected virtual void Delete(string idsContent)
{
var ids = idsContent.Split(',');
ids.ForEach(t => DeleteItem(Convert.ToInt64(t)));
}
protected virtual void DeleteItem(long id)
{
var item = DefaultTable.Find(id);
if (item != null) DefaultTable.Remove(item);
//Db.Database.ExecuteSqlCommand($"Delete from {typeof(T).Name} where {PrimaryKey}=@id", new SqlParameter("@id", id));
}
protected string GetPropertyName(Expression<Func<T, object>> expr)
{
return ClassEx.GetPropertyName(expr);
}
}
Database.SetInitializer(new CreateDatabaseIfNotExists<G5ManagerCenterEntities>());
var db = new G5ManagerCenterEntities();
拿去吧,增删改查都有了。