微小黑开发笔记
一.说明
用c#基于.net 平台的MVC框架开发的微网站。
二.对象实体映射框架
1.Entity Framework:https://msdn.microsoft.com/en-us/library/jj592674(v=vs.113).aspxCodeFirst技术
1.1常用的数据库操作
using (var context = new BloggingContext()) { var blog = new Blog { Name = "ADO.NET Blog" }; context.Entry(blog).State = EntityState.Added; context.SaveChanges(); }新增
var existingBlog = new Blog { BlogId = , Name = "ADO.NET Blog" }; using (var context = new BloggingContext()) { context.Blogs.Attach(existingBlog); // Do some more work... context.SaveChanges(); }修改
1.2.创建数据库
[Serializable] [DataContract] [Table("WxStoreTB")] public class WxStoreTB : ModelBase { public WxStoreTB() { StoreBackground = "/Content/Images/adminbanner.jpg"; } [DataMember] public string DistributorSlogan { get; set; } [DataMember] public string StoreLogo { get; set; } [DataMember] public string StoreBackground { get; set; } [DataMember] public string IndexBackgroundMusic { get; set; } [DataMember] public string EnglishName { get; set; } [DataMember] public string QRCodeImage { get; set; } [DataMember] /// <summary> /// 店铺ID /// </summary> public int StoreTBID { get; set; } }定义域模型
public class DbContextBase : DbContext, IDataRepository, IDisposable { public DbContextBase(string connectionString) { this.Database.Connection.ConnectionString = connectionString; this.Configuration.LazyLoadingEnabled = false; this.Configuration.ProxyCreationEnabled = false; } public DbContextBase(string connectionString, IAuditable auditLogger) : this(connectionString) { this.AuditLogger = auditLogger; } public IAuditable AuditLogger { get; set; } public T Update<T>(T entity, bool isSave = true) where T : ModelBase { var set = this.Set<T>(); set.Attach(entity); this.Entry<T>(entity).State = EntityState.Modified; if (isSave) { this.SaveChanges(); } return entity; }封装DbContext
1.3对象关系映射:
1.3.1Data Annotations:缺点是污染域模型
[DataContract] [Table("PageTB")] public class PageTB : ModelBase { /// <summary> ///店铺ID /// </summary> [DataMember] [ForeignKey("StoreTB")] public int StoreTBID { get; set; } /// <summary> ///页面类别 0:首页 1:其他 /// </summary> [DataMember] public int PageType { get; set; } /// <summary> ///页面名称 /// </summary> [DataMember] public string GuidNumber { get; set; } /// <summary> ///页面路径(站点根目录为起始的相对路径) /// </summary> [DataMember] public string PagePath { get; set; } /// <summary> ///标题 /// </summary> [DataMember] public string Title { get; set; } /// <summary> /// 是否已上架 /// </summary> [DataMember] public bool IsOnShelf { get; set; } /// <summary> /// 版本号 空则放在商户目录下 /// </summary> [DataMember] public string Version { get; set; } }注解
1.3.2Fluent API:
modelBuilder.Entity<BlogUser>().HasKey(user => user.UserId); modelBuilder.Entity<BlogUser>().Property(user => user.BlogName).IsRequired();View Code
相关推荐
yanyongtao 2020-11-02
dangai00 2020-07-18
MrFuWen 2020-06-28
boredbird 2020-06-26
fengling 2020-06-16
MIKUScallion 2020-06-11
80447704 2020-06-09
XuDanT 2020-06-07
MrFuWen 2020-06-06
aNian 2020-06-03
dongxurr 2020-06-01
冒烟儿 2020-06-01
Roka 2020-05-25
zhangdy0 2020-05-25
ErixHao 2020-05-20
ErixHao 2020-05-16