本文转自:https://www.pronopcommerce.com/using-entity-framework-ef-code-first-migrations-in-nopcommerce-for-fast-customizations

Using Entity Framework (EF) Code-First Migrations in nopCommerce for Fast Customizations

By Woon Cherk Lam | Wednesday, December 18, 2013            
 

I've seen a lot of nopCommerce forums users asking how they can use Entity Framework (EF) Code-First Migrations to customize nopCommerce, add new fields and entites to the core. I actually use a lot of EF Migrations myself when doing nopCommerce customization projects, and I must say it helps a lot in the development.

Today, I'll share with you how you can do that in nopCommerce project! I'll be using nopCommerce 3.20 as an example, but you can easily apply the concept to other vesions!

Setting Up EF Migrations in nopCommerce Solution

The first thing you want to do is to enable migrations in your nopCommerce solution. So fire up nopCommerce in Visual Studio, look at Nop.Web project, and open Web.config. You need to add a connection string to your development database. Note that adding the connection string in Web.config doesn't affect how nopCommerce works, since nopCommerce doesn't look for connection string in Web.config.

Then, open NopObjectContext.cs in Nop.Data. Add a new constructor that points to the name of the connection string that you've just added in previous step. NOTE: replace Pro-nopCommerce with the name of your connection string.

The next step is to actually enable migrations in the project. If you have not already done so, bring up Package Manager Console. In the "Default Project" drop-down list, select Nop.Data as the project. Please also make sure Nop.Web is selected as the StartUp Project. Now, enter the command "enable-migrations" in Package Manager Console, and hit Enter! Visual Studio will generate a file named "Configurations.cs"; you can safely ignore it, but you need to keep it.

The last step in EF Migrations setup is done by entering the command "add-migration InitialMigration -IgnoreChanges" in Package Manager Console. "InitialMigration" (highligted yellow) is the name you want to give the the current migration, and the "IgnoreChanges" handle is to tell EF Migrations that you want to leave out the current database as-is: that means you want EF to ignore all the existing tables so that no script is generated for the existing tables.

As a result, you'll see a new .cs file generated by Visual Studio that correspond to the migration - InitialMigration - you've just added. If you look at the file, it essentially is a blank file, due to the fact that we used the -IgnoreChanges handle in previous step.

To actually save this migration over to the database, run the command "update-database" in Package Manager Console. Your database is now ready for actual EF Migration tasks!

Adding a Migration and Updating the Database

Now, suppose we want to link up Blog and Products, where a Product can have multiple BlogPost that talk about the Product itself. We'll need a one-to-many relationship between Product and BlogPost.

In essence, what we need is a new ProductId field in BlogPost, and a ICollection<BlogPost> property in Product. The following screenshot sums up the update we should add to both Product.cs and BlogPost.cs (in Nop.Core project).

Now you have the properties ready on your domain model, you also need to setup the configuration and tell Entity Framework how you want to format the parameter, for example the relationship, and whether a field is optional/required. Open BlogPostMap.cs in Nop.Data, and enter the following lines, where we tell EF to that Product (or ProductId) is an optional property (meaning it can have NULL as value), and a Product can have many BlogPost, and to use ProductId as the foreign key between BlogPost and Product.

We now have enough information to instruct Entity Framework to generate the migration, so again, bring up Package Manager Console, and enter the command "add-migration AddProductToBlogPost".

This command causes Visual Studio to generate a .cs file that looks like the following:

To actually generate the SQL script and update the database, run again the command "update-database" in Package Manager Console. Now, to verify that the database is updated correctly, open up BlogPost table in the database, and check that the new field, foreign key and etc are indeed added.

Conclusion

Entity Framework Code-first Migration can be a very handy tool if you customize nopCommerce a lot. Like this you don't need to touch the database when you want to add new fields, tables and etc to the database. Everything can be done from the code!

Learn up the technique, and have fun coding!

[转]Using Entity Framework (EF) Code-First Migrations in nopCommerce for Fast Customizations的更多相关文章

  1. 【极力分享】[C#/.NET]Entity Framework(EF) Code First 多对多关系的实体增,删,改,查操作全程详细示例【转载自https://segmentfault.com/a/1190000004152660】

      [C#/.NET]Entity Framework(EF) Code First 多对多关系的实体增,删,改,查操作全程详细示例 本文我们来学习一下在Entity Framework中使用Cont ...

  2. [C#/.NET]Entity Framework(EF) Code First 多对多关系的实体增,删,改,查操作全程详细示例

    本文我们来学习一下在Entity Framework中使用Context删除多对多关系的实体是如何来实现的.我们将以一个具体的控制台小实例来了解和学习整个实现Entity Framework 多对多关 ...

  3. Entity Framework(EF) Code First将实体中的string属性映射成text类型的几种方式

    1.通过ColumnType属性设置 [Column(TypeName="text")] public string Text { get; set; } 在进行以上属性设置时,请 ...

  4. 创建ASP.NET Core MVC应用程序(3)-基于Entity Framework Core(Code First)创建MySQL数据库表

    创建ASP.NET Core MVC应用程序(3)-基于Entity Framework Core(Code First)创建MySQL数据库表 创建数据模型类(POCO类) 在Models文件夹下添 ...

  5. Entity Framework 之 Code First

    使用NuGet助您玩转代码生成数据————Entity Framework 之 Code First [前言] 如果是Code First老鸟或者对Entity Framework不感兴趣,就不用浪费 ...

  6. 在Entity Framework 中用 Code First 创建新的数据库

    在Entity Framework 中用 Code First 创建新的数据库 (原文链接) 本文将逐步介绍怎样用Code First 创建新数据库,使用在代码中定义类和API中提供的特性(Attri ...

  7. Entity Framework Core Code First 项目实践

    Entity Framework Core Code First 实践 任何一种技术的出现都是为了解决一系列特定的问题,只有了解了技术所要解决的关键问题,才能理解它的真正用途,之后,才能在实践中用好它 ...

  8. EF Code First Migrations数据库迁移

    1.EF Code First创建数据库 新建控制台应用程序Portal,通过程序包管理器控制台添加EntityFramework. 在程序包管理器控制台中执行以下语句,安装EntityFramewo ...

  9. ASP.NET Core 开发-Entity Framework (EF) Core 1.0 Database First

    ASP.NET Core 开发-Entity Framework Core 1.0 Database First,ASP.NET Core 1.0 EF Core操作数据库. Entity Frame ...

随机推荐

  1. JavaScript寄生组合式继承分析

    JavaScript寄生组合式继承特点: 避免了在子类prototype上创建不必要多余的属性,相比直接继承基类的实例效率要高. 是JavaScript 实现继承的最有效方式. <script& ...

  2. 过去几个月出炉的30款最喜欢的 jQuery 插件

    在这篇文章中,我们收集了一些在过去的几个月里最喜欢的 jQuery 插件.为了使您更容易搜索到自己喜欢的 jQuery 插件,我们已经对插件进行了分类: 页面布局插件,图片和视频插件,滑块和画廊,排版 ...

  3. Aurelia – 模块化,简单,可测试的 JS 框架

    Aurelia 是下一代 JavaScript 客户端框架,利用简单的约定来激发你的创造力.凭借其强大的专注于开发经验, Aurelia 可以使您不仅创造惊人的应用程序,同时也享受这个过程.它经过精心 ...

  4. 10个最好的 JavaScript 动画库和开发框架

    虽然 CSS3 动画功能能够让我们以简单轻松的方式实现动画效果,但是浏览器兼容性问题让人头疼.不过不用担心,我们还有另外的武器——JavaScript,它同样可以帮助你实现各种各样的动画效果,而且借助 ...

  5. 高端大气上档次!10个精美的国外HTML5网站欣赏

    这篇文章挑选了10个高端大气上档次的 HTML5 网站分享给大家.作为下一代网页语言,HTML5 加入中众多的语义化标签,例如 video.audio.section.article.header.f ...

  6. Tabio – 轻松,高效的管理 Chrome 标签页

    Tabio 是一个 Chrome 扩展,旨在简化大量浏览器标签页的管理.它提供的搜索功能允许您快速.轻松地找到您需要的选项卡.Tabio 便于组织你的标签,简单的拖拽排序.您也可以使用输入.删除和箭头 ...

  7. jQ函数after、append、appendTo的区别

    1.after函数定义和用法:after() 方法在被选元素后插入指定的内容.参考:http://keleyi.com/a/bjac/cfyxd60g.htm 语法:$(selector).after ...

  8. html5的发展历程和由此引起的政治斗争

    2007年,乔布斯断言拒绝Flash并预言HTML5时代来临,IT行业就对HTML5产生了一股宗教热情.HTML5有着许多出众的特性,可以直接在网页上绘图.嵌入音视频.实现信息互动,可以跨越iOS.A ...

  9. sharepoint2010问卷调查(4)-实现问卷的重复答复次数(采用自定义字段类型和JS)

    sharepoint的问卷调查可以设置重复和一次答复.但是设置一次后,调查过的用户再进行答复.会提示如下图: 分析下:该提示用户体验很不好.给用户感觉是系统出问题了.因此网上有人提出用eventhan ...

  10. Linux下运行windows程序

    现在Winxp停止了支持,那我们的windows程序是否可以再linux上执行呢,如下是一些参考的信息 在您的 Linux/Mac 操作系统上运行 Windows 软件 http://www.wine ...