zoukankan      html  css  js  c++  java
  • NopCommerce 数据库初始化

    NopCommerce数据库初始化比较复杂,我简化了,只初始化创建一张表,不多说,直接上代码:

    //数据实体

    /// <summary>
        /// Represents an affiliate
        /// </summary>
        public partial class Affiliate
        {
            /// <summary>
            /// Gets or sets the address identifier
            /// </summary>
            public int AddressId { get; set; }
    
            /// <summary>
            /// Gets or sets the admin comment
            /// </summary>
            public string AdminComment { get; set; }
    
            /// <summary>
            /// Gets or sets the friendly name for generated affiliate URL (by default affiliate ID is used)
            /// </summary>
            public string FriendlyUrlName { get; set; }
    
            /// <summary>
            /// Gets or sets a value indicating whether the entity has been deleted
            /// </summary>
            public bool Deleted { get; set; }
    
            /// <summary>
            /// Gets or sets a value indicating whether the entity is active
            /// </summary>
            public bool Active { get; set; }
    
        }
    
    
        public partial class AffiliateMap : NopEntityTypeConfiguration<Affiliate>
        {
            public AffiliateMap()
            {
                this.ToTable("Affiliate");//表名
                this.HasKey(a => a.AddressId);//设置主键
            }
        }
     public abstract class NopEntityTypeConfiguration<T> : EntityTypeConfiguration<T> where T : class
        {
            protected NopEntityTypeConfiguration()
            {
                PostInitialize();
            }
    
            /// <summary>
            /// Developers can override this method in custom partial classes
            /// in order to add some custom initialization code to constructors
            /// </summary>
            protected virtual void PostInitialize()
            {
    
            }
        }

    //创建上下文 通过反射获取要加载的类

    /// <summary>
        /// Object context
        /// </summary>
        public class NopObjectContext : DbContext
        {
    
            public DbSet<Affiliate> Affiliates { get; set; }
    
            /// <summary>
            ///
            /// </summary>
            /// <param name="modelBuilder"></param>
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                //dynamically load all configuration
                //System.Type configType = typeof(LanguageMap);   //any of your configuration classes here
                //var typesToRegister = Assembly.GetAssembly(configType).GetTypes()
    
                var typesToRegister = Assembly.GetExecutingAssembly().GetTypes()
                .Where(type => !String.IsNullOrEmpty(type.Namespace))
                 .Where(type => type.BaseType != null && type.BaseType.IsGenericType &&
                     type.BaseType.GetGenericTypeDefinition() == typeof(NopEntityTypeConfiguration<>));
                foreach (var type in typesToRegister)
                {
                    dynamic configurationInstance = Activator.CreateInstance(type);
                    modelBuilder.Configurations.Add(configurationInstance);//获取此 DbModelBuilder 的 ConfigurationRegistrar。 注册器允许使用此生成器来注册派生的实体和复杂类型配置。
                }
                //...or do it manually below. For example,
                //modelBuilder.Configurations.Add(new LanguageMap());
    
    
    
                base.OnModelCreating(modelBuilder);
            }
        }

    //测试

      Database.SetInitializer(new DropCreateDatabaseAlways<NopObjectContext>());//创建新的数据库
                using (NopObjectContext ctx = new NopObjectContext())
                {
                    ctx.Database.Initialize(force: true);
                }

    这里都结束了,当然Nop用的要复杂的多,这里都简单介绍哈

  • 相关阅读:
    一个java的进销存管理系统源码阅读
    嵌入式起步
    java数据库操作有关的杂记
    VS 调试
    软件测试的原则
    黑盒测试与白盒测试
    Xmind快速入门(基本操作够用了)
    opt目录
    日志测试
    bug的前世今生
  • 原文地址:https://www.cnblogs.com/xchit/p/4686967.html
Copyright © 2011-2022 走看看