添加新增实体的示例数据
在Libraries\Nop.Services\Installation\CodeFirstInstallationService.cs文件中添加服务成员变量:
private readonlyIRepository<Link>_linkRepository;
然后给CodeFirstInstallationService构造函数添加对应的参数IRepository<LinkModel>linkRepository,并给_linkRepository赋值。
接着添加函数:
protected virtualvoid InstallLinks()
{
List<Link> links =newList<Link>()
{
new Link {Name="Zaxx",Value="最爱肖霄——zaxx" }
};
_linkRepository.Insert(links);
}
最后在InstallData函数中添加对InstallLinks()函数的调用。
另外还要在Presentation\Nop.Web\App_Data\Install\create_sample_data.sql文件中添加下面语句:
SETIDENTITY_INSERT [dbo].[Link] ON
INSERT[dbo].[Link] ([Id], [Name], [Value]) VALUES (1, N'Zaxx', N'最爱肖霄——zaxx')
SETIDENTITY_INSERT [dbo].[Link] OFF
GO
DependencyRegistrar的Register中会根据Web.config文件中UseFastInstallationService(使用快速安装服务)的配置值来判断是用SqlFileInstallationService的InstallData执行create_sample_data.sql文件向数据库中插入演示数据,还是使用CodeFirstInstallationService的InstallData向数据库中插入演示数据。Web.config文件中UseFastInstallationService为默认是false,将CodeFirstInstallationService的InstallData向数据库中插入演示数据(注意:若插入的中文数据是乱码,则用记事本打开CodeFirstInstallationService.cs,把它的编码改为UTF-8)。
注意:迁移时未向数据库中插入演示数据,因为在之前的代码中,未在Configuration.cs的Seed方法中给出演示数据。若有需要,可以自行给出。在数据库中已有演示数据时,按之前所述的迁移步骤对数据库进行更新时,已有演示数据是不会丢失的。
个人推广:文章代写与软件开发