博客
关于我
Nancy简单实战之NancyMusicStore(一):准备工作和搭建项目
阅读量:418 次
发布时间:2019-03-06

本文共 5126 字,大约阅读时间需要 17 分钟。

开发环境

在开始迁移和搭建项目之前,首先需要明确当前的开发环境配置。以下是我的开发环境设置:

  • 操作系统:Windows 10 10.0.14393
  • 开发工具:Visual Studio 2015 Community With Update 3
  • 数据库:PostgreSQL 9.6
  • 数据库管理工具:pgAdmin 4

迁移数据与配置PG远程访问

在迁移数据之前,需要先备份并提取MVC MusicStore中的数据。数据分为两部分:系统相关数据和会员数据。根据项目需求,我们只需要迁移系统相关的数据,会员数据可以忽略。

系统相关的数据存储在SQL Server Compact数据库中。通过Database4打开这个数据库,然后生成相关的脚本文件。这些脚本需要进行简单的修改后,通过pgAdmin执行。

此外,还需要添加一个新的用户表。以下是新建用户表的建表语句:

CREATE TABLE public.sysuser(    sysuserid character varying(100) COLLATE pg_catalog."default" NOT NULL,    sysusername character varying(100) COLLATE pg_catalog."default" NOT NULL,    sysuserpassword character varying(100) COLLATE pg_catalog."default" NOT NULL,    sysuseremail character varying(100) COLLATE pg_catalog."default",    CONSTRAINT sysuser_pkey PRIMARY KEY (sysuserid))WITH (OIDs = FALSE) TABLESPACE pg_default;ALTER TABLE public.sysuser    OWNER to dev;

数据库和数据表已经建好后,下面需要配置PG以便远程访问。否则,在Linux部署时可能会提示无法访问数据库。只需修改pg_hba.conf配置文件,将host all all 127.0.0.1/32 md5修改为host all all 0.0.0.0/0 md5

如果不希望允许所有IP远程访问,可以将0.0.0.0设定为特定的IP值。

搭建项目

新建一个空的Web项目,命名为NancyMusicStore。通过NuGet添加以下程序包:

Install-Package Nancy -Version 1.4.3Install-Package Nancy.Hosting.Aspnet -Version 1.4.1Install-Package Nancy.Viewengines.Razor -Version 1.4.3Install-Package Nancy.Authentication.Forms -Version 1.4.1Install-Package DapperInstall-Package Npgsql -Version 3.1.9

添加了这些程序包后,需要移除不再使用的DLL引用。项目中只保留必要的引用,具体清单如下:

// 过滤后的引用清单using Nancy;using Nancy.Authentication.Forms;using Nancy.Bootstrapper;using Nancy.Conventions;using Nancy.Session;using Nancy.TinyIoc;using NancyMusicStore.Common;

接下来,在ContentScripts文件夹中添加必要的静态资源文件。确保启动器支持静态资源访问,否则会出现404错误。

为此,创建自定义的启动器类CustomBootstrapper,具体实现如下:

using Nancy;using Nancy.Authentication.Forms;using Nancy.Bootstrapper;using Nancy.Conventions;using Nancy.Session;using Nancy.TinyIoc;using NancyMusicStore.Common;namespace NancyMusicStore{    public class CustomBootstrapper : DefaultNancyBootstrapper    {        protected override void ConfigureConventions(NancyConventions conventions)        {            base.ConfigureConventions(conventions);            conventions.StaticContentsConventions.Add(                StaticContentConventionBuilder.AddDirectory("Scripts")            );            conventions.StaticContentsConventions.Add(                StaticContentConventionBuilder.AddDirectory("Content")            );        }    }}

数据模型与数据库映射

Common文件夹中创建数据模型和数据库表映射。以下是DBHelper的实现:

using Dapper;using Npgsql;using System.Collections.Generic;using System.Linq;using System.Data;namespace NancyMusicStore.Common{    public class DBHelper    {        private static IDbConnection OpenConnection()        {            var conn = new NpgsqlConnection(ConfigHelper.GetConneectionStr());            conn.Open();            return conn;        }        public static int Execute(string sql, object param = null,                                 IDbTransaction transaction = null,                                 int? commandTimeout = null,                                 CommandType? commandType = null)        {            using (var conn = OpenConnection())            {                return conn.Execute(sql, param, transaction, commandTimeout, commandType);            }        }        public static object ExecuteScalar(string cmd,                                          object param = null,                                          IDbTransaction transaction = null,                                          int? commandTimeout = null,                                          CommandType? commandType = null)        {            using (var conn = OpenConnection())            {                return conn.ExecuteScalar(cmd, param, transaction, commandTimeout, commandType);            }        }        public static IList
Query
(string sql, object param = null, IDbTransaction transaction = null, bool buffered = true, int? commandTimeout = null, CommandType? commandType = null) where T : class { using (var conn = OpenConnection()) { return conn.Query(sql, param, transaction, buffered, commandTimeout, commandType) .ToList(); } } public static T QueryFirstOrDefault
(string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null) where T : class { using (var conn = OpenConnection()) { return conn.QueryFirstOrDefault
(sql, param, transaction, commandTimeout, commandType); } } }}

数据库连接配置

web.config中添加PostgreSQL的连接字符串:

总结

通过以上步骤,我们已经完成了开发环境的配置、数据迁移、项目搭建以及数据库连接设置。接下来,将开始打造NancyMusicStore的首页。希望这篇文章能为你的项目开发提供有价值的参考。

转载地址:http://erxkz.baihongyu.com/

你可能感兴趣的文章
Notepad++在线和离线安装JSON格式化插件
查看>>
notepad++最详情汇总
查看>>
notepad++正则表达式替换字符串详解
查看>>
notepad如何自动对齐_notepad++怎么自动排版
查看>>
Notes on Paul Irish's "Things I learned from the jQuery source" casts
查看>>
Notification 使用详解(很全
查看>>
NotImplementedError: Cannot copy out of meta tensor; no data! Please use torch.nn.Module.to_empty()
查看>>
NotImplementedError: Could not run torchvision::nms
查看>>
nova基于ubs机制扩展scheduler-filter
查看>>
Now trying to drop the old temporary tablespace, the session hangs.
查看>>
nowcoder—Beauty of Trees
查看>>
np.arange()和np.linspace()绘制logistic回归图像时得到不同的结果?
查看>>
np.power的使用
查看>>
NPM 2FA双重认证的设置方法
查看>>
npm build报错Cannot find module ‘webpack/lib/rules/BasicEffectRulePlugin‘解决方法
查看>>
npm build报错Cannot find module ‘webpack‘解决方法
查看>>
npm ERR! ERESOLVE could not resolve报错
查看>>
npm ERR! fatal: unable to connect to github.com:
查看>>
npm ERR! Unexpected end of JSON input while parsing near '...on":"0.10.3","direc to'
查看>>
npm ERR! Unexpected end of JSON input while parsing near ‘...“:“^1.2.0“,“vue-html-‘ npm ERR! A comp
查看>>