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用的要复杂的多,这里都简单介绍哈

NopCommerce 数据库初始化的更多相关文章

  1. nopCommerce 数据库初试化及数据操作

    系统启动时执行任务:IStartupTask,启动时执行的任务主要是数据库的初始化和加载. IStartupTask调用IEfDataProvider进行数据库的初始化. IEfDataProvide ...

  2. Entity Framework 数据库初始化的三种方法

    在数据库初始化产生时进行控制,有三个方法可以控制数据库初始化时的行为.分别为CreateDatabaseIfNotExists.DropCreateDatabaseIfModelChanges.Dro ...

  3. 4.DB Initialization(数据库初始化)[EF Code-First系列]

    前面的例子中,我们已经看到了Code-First自动为我们创建数据库的例子. 这里我们将要学习的是,当初始化的时候,Code-First是怎么决定数据库的名字和服务的呢??? 下面的图,解释了这一切! ...

  4. Entity Framework 数据库初始化四种策略

    策略一:数据库不存在时重新创建数据库 Database.SetInitializer<testContext>(new CreateDatabaseIfNotExists<testC ...

  5. DbContext 那些事 —— 数据库初始化

    数据库初始化 上图,这个图解释了,数据库初始化的流程,是基于我们在上下文类中的构造函数中传递的参数. 在上面的图中,context类中的base构造器中,可以填入下面的参数: 无参数(No Param ...

  6. Entity Framework数据库初始化四种策略

    策略一:数据库不存在时重新创建数据库 程序代码 Database.SetInitializer<testContext>(new CreateDatabaseIfNotExists< ...

  7. EF数据库初始化策略及种子数据的添加

    EF数据库初始化策略及种子数据的添加 CreateDatabaseIfNotExists 判断当前数据库连接字符串对应的数据库是否存在,若不存在则根据代码定义的model进行创建 DropCreate ...

  8. SQL Server 数据库初始化准备脚本

    通常我们在项目部署前都会写一份数据库初始化脚本.由于数据库外键的限制,我们需要按照数据引用顺序添加初始记录,这个整理过程相当麻烦. 因此写了以下脚本,原理是先去掉所有外键,然后执行一次清空,然后添加数 ...

  9. EF CodeFirst 数据库初始化策略

    最近用EF做了几个小东西,了解简单使用后有了深入研究的兴趣,所以想系统的研究一下EF CodeFist的几个要点.下面简单列一下目录 1.1 目录 数据库初始化策略和数据迁移Migration的简单介 ...

随机推荐

  1. 《Python 学习手册4th》 第六章 动态类型简介

    ''' 时间: 9月5日 - 9月30日 要求: 1. 书本内容总结归纳,整理在博客园笔记上传 2. 完成所有课后习题 注:“#” 后加的是备注内容 (每天看42页内容,可以保证月底看完此书)“重点笔 ...

  2. 【软件多国语言】一个demo

    之前上学的时候做过一个东西,需要中英文软件界面,并且需要随时可以切换,当时是师妹来做的,用的最直接也是最笨的办法, what? if(中文) { button1.Text = "花姑娘&qu ...

  3. 【跟我一起学Python吧】python with statement 进阶理解

    由于之前有一个项目老是要打开文件,然后用pickle.load(file),再处理...最后要关闭文件,所以觉得有点繁琐,代码也不简洁.所以向python with statement寻求解决方法.以 ...

  4. Redis3.0 Install

    Installation Download, extract and compile Redis with: $ wget http://download.redis.io/releases/redi ...

  5. HDU4861:Couple doubi(费马小定理)

    题意: 给出k个球和质数p,对每个球以公式val(i)=1^i+2^i+...+(p-1)^i (mod p)计算出它的价值,然后两个人轮流拿,最后拿到的球的总价值大的获胜,问我们先手是否获胜. 我们 ...

  6. 生成chm文档工具- Sandcastle -摘自网络

    Sandcastle是微软官方的文档生成工具,NDoc开发停止后,这个貌似也是唯一的一个这方面的工具.它从dll文件及其xml注释文件能够 生成完整的帮助文档,支持多种生成格式(Helpe1x:chm ...

  7. 第二百五十八天 how can I 坚持

    装虚拟机了.CentOs,32位,这电脑装4台能不能带起来啊. 早上,流鼻血了,不知道咋回事.太干了... 明天得早起会,得利索着点,不能托托拉拉的. 还有,今天又忘带钥匙了.悲剧. 睡觉.hadoo ...

  8. HDU 5783 Divide the Sequence (贪心)

    Divide the Sequence 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5783 Description Alice has a seq ...

  9. Spring Autowiring by AutoDetect

    In Spring, "Autowiring by AutoDetect", means chooses "autowire by constructor" i ...

  10. 嵌入式LINUX入门到实践(二)

    这篇中将围绕韦东山LINUX第二部分教程源码,对IIC协议进行程序实现上的分析. /* I2C registers */#define IICCON      (*(volatile unsigned ...