首页 新闻 会员 周边 捐助

C#控制台创建数据库中的表

0
悬赏园豆:30 [待解决问题]

用C#写一个类 ,用泛型调用下  ,就在数据库中创建了一个这个类的表 里面有对应类里面的字段

九两白菜粥的主页 九两白菜粥 | 初学一级 | 园豆:122
提问于:2017-12-22 15:51
< >
分享
所有回答(4)
0

反射,得到这个类型的所有属性,,,,

西漠以西 | 园豆:1675 (小虾三级) | 2017-12-22 16:00
0

Code First ?

~雨落忧伤~ | 园豆:336 (菜鸟二级) | 2017-12-22 16:58
0

你是陈述句.那问题是什么?

吴瑞祥 | 园豆:29449 (高人七级) | 2017-12-22 17:30
0

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();

 

 

拿去吧,增删改查都有了。

花飘水流兮 | 园豆:13615 (专家六级) | 2017-12-22 17:39
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册