微小黑开发笔记

一.说明

用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

相关推荐