1。怎么去通过nuget去获取entityframework的,就不说了,
2。DbContext,代表数据库文档,就可以理解为一个数据库,
3。数据库中含有多个表,也有各种操作,多种业务
public class NewscontentContext : DbContext { public NewscontentContext() : base("connectdata")//这里的connectdata是对应web.config的一个配置,
//如configuration/connectionStrings下: <add name="connectdata" providerName="System.Data.SqlClient" connectionString="Data Source=.;Initial Catalog=db;User ID=sa;Password=123;Integrated Security=True" /> {
}
/// <summary> /// 数据库对象 对应 newsModel /// </summary> public virtual IDbSet<newsModel> newsInfo { get; set; }
/// <summary> /// 数据库对象 对应 newsclassModel /// </summary> public virtual IDbSet<newsclassModel> newsclassInfo { get; set; }
/// <summary> /// 重写 模型创建方法 /// </summary> /// <param name="modelBuilder"></param>
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{ //return; // TODO: 在这里写Fluent API //公约 modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); modelBuilder.Conventions.Remove<IncludeMetadataConvention>();
modelBuilder.Configurations.Add(new newsMapping());
modelBuilder.Configurations.Add(new newsclassMapping());
base.OnModelCreating(modelBuilder);
}
}
model:
Newscontent.Model{ [Table("t_news")] public class newsModel {
/// <summary> /// /// </summary> [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity), Column("id")] public long id { get; set; } /// <summary> /// /// </summary> [Column("newsclass_id")] public long newsclass_id { get; set; } /// <summary> /// /// </summary> [MaxLength(255), Column("title")] public string title { get; set; }
}
Mappping://这个类作用暂时也没怎么用到,它也可以做一些模型对象的配置,但似乎没必要在这里做,这里暂时保留
Newscontent.Mapping{ public class newsMapping : EntityTypeConfiguration<newsModel> { }
}
DAL:
Newscontent.DAL{ internal class newsDAL { #region standard function public int Add(newsModel model) { using (var ctx = new NewscontentContext()) { ctx.newsInfo.Add(model); return ctx.SaveChanges(); } }
public int Add(List<newsModel> model_list) { int iadd = 0; using (var ctx = new NewscontentContext()) { for (int i = 0; i < model_list.Count; i++) { ctx.newsInfo.Add(model_list[i]); } iadd = ctx.SaveChanges(); } return iadd; }
public int Update(newsModel model) { using (var ctx = new NewscontentContext()) { ctx.Entry(model).State = System.Data.Entity.EntityState.Modified; return ctx.SaveChanges(); } }
public newsModel GetModel(long id) { using (var ctx = new NewscontentContext()) { var datas = ctx.newsInfo.Where(q => q.id == id).ToList(); if (datas.Count == 0) return null; return datas[0]; } }
public int Delete(newsModel model) { using (var ctx = new NewscontentContext()) { ctx.Entry(model).State = System.Data.Entity.EntityState.Deleted; ctx.newsInfo.Remove(model); return ctx.SaveChanges(); } }
#endregion
}
既然有了,DAL,BLL也就可以有了,具体略
就简单的说这些,
转载于:https://www.cnblogs.com/ijunxiong/articles/6849675.html
