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用的要复杂的多,这里都简单介绍哈
NopCommerce 数据库初始化的更多相关文章
- nopCommerce 数据库初试化及数据操作
系统启动时执行任务:IStartupTask,启动时执行的任务主要是数据库的初始化和加载. IStartupTask调用IEfDataProvider进行数据库的初始化. IEfDataProvide ...
- Entity Framework 数据库初始化的三种方法
在数据库初始化产生时进行控制,有三个方法可以控制数据库初始化时的行为.分别为CreateDatabaseIfNotExists.DropCreateDatabaseIfModelChanges.Dro ...
- 4.DB Initialization(数据库初始化)[EF Code-First系列]
前面的例子中,我们已经看到了Code-First自动为我们创建数据库的例子. 这里我们将要学习的是,当初始化的时候,Code-First是怎么决定数据库的名字和服务的呢??? 下面的图,解释了这一切! ...
- Entity Framework 数据库初始化四种策略
策略一:数据库不存在时重新创建数据库 Database.SetInitializer<testContext>(new CreateDatabaseIfNotExists<testC ...
- DbContext 那些事 —— 数据库初始化
数据库初始化 上图,这个图解释了,数据库初始化的流程,是基于我们在上下文类中的构造函数中传递的参数. 在上面的图中,context类中的base构造器中,可以填入下面的参数: 无参数(No Param ...
- Entity Framework数据库初始化四种策略
策略一:数据库不存在时重新创建数据库 程序代码 Database.SetInitializer<testContext>(new CreateDatabaseIfNotExists< ...
- EF数据库初始化策略及种子数据的添加
EF数据库初始化策略及种子数据的添加 CreateDatabaseIfNotExists 判断当前数据库连接字符串对应的数据库是否存在,若不存在则根据代码定义的model进行创建 DropCreate ...
- SQL Server 数据库初始化准备脚本
通常我们在项目部署前都会写一份数据库初始化脚本.由于数据库外键的限制,我们需要按照数据引用顺序添加初始记录,这个整理过程相当麻烦. 因此写了以下脚本,原理是先去掉所有外键,然后执行一次清空,然后添加数 ...
- EF CodeFirst 数据库初始化策略
最近用EF做了几个小东西,了解简单使用后有了深入研究的兴趣,所以想系统的研究一下EF CodeFist的几个要点.下面简单列一下目录 1.1 目录 数据库初始化策略和数据迁移Migration的简单介 ...
随机推荐
- ndk文件操作问题及小结
最近在做文件传输,发现在android下用f系列的C库函数去读取文件文件大小会受到2G大小的约束,查阅了很久,最后只能去看google的libc源码,发现了以下几个问题: 1.bionic的libc是 ...
- 《锋利的Jquery第二版》读书笔记 第一章
按照书本介绍顺序整理jquery库相关的语法.要点. window.onload与$(document).ready()功能类似,前者需要所有资源加载完毕,且不能同时编写多个:后者加载完DOM结构即执 ...
- adb常用命令介绍
adb connect 命令格式:adb connect <host>[:<port>] 作用:connect to a device via TCP/IP,Port 5555 ...
- Lucene学习笔记:一,全文检索的基本原理
一.总论 根据http://lucene.apache.org/java/docs/index.html定义: Lucene是一个高效的,基于Java的全文检索库. 所以在了解Lucene之前要费一番 ...
- java计时代码
public class Test{ public static void main(String[] args) { long startMili=System.currentTimeMillis( ...
- homework -06 围棋
playPrev功能的实现 public void playPrev(ref GoMove gm) { Point p = gm.Point; m_colorToPlay = gm.Color; cl ...
- Spring入门(6)-使用注解装配
Spring入门(6)-使用注解装配 本文介绍如何使用注解装配. 0. 目录 使用Autowired 可选的自动装配 使用Qualifier选择 1. 使用Autowired package com. ...
- codeforces 630C Lucky Numbers
C. Lucky Numbers time limit per test 0.5 seconds memory limit per test 64 megabytes input standard i ...
- 查杀oracle锁表
()锁表查询的代码有以下的形式: select count(*) from v$locked_object; select * from v$locked_object; ()查看哪个表被锁 sele ...
- ASP.NET网站中设置404自定义错误页面
在用ASP.NET WebForm开发一个网站时,需要自定义404错误页面. 做法是这样的 在网站根目录下建立了一个404.html的错误页面,然后在Global.asax文件中,加入如下代码: &l ...