1. dotnet ef相关命令

删除数据库(适合只有一个数据库的情形)

dotnet ef database drop --force

更新数据库(适合只有一个数据库的情形)

dotnet ef database update

如果多个数据库,则需更改:

首先,查看有哪些数据库context:

dotnet ef dbcontext list
> dotnet ef dbcontext list
info: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[]
User profile is available. Using 'C:\Users\zte_pre\AppData\Local\ASP.NET\D
.......
dName]
FROM [AspNetRoles] AS [r]
WHERE [r].[NormalizedName] = @__normalizedName_0
SportsStore.Models.IdentityDataContext
SportsStore.Models.DataContext

在对具体的数据库进行操作:

dotnet ef database drop --force --context  IdentityDataContext

dotnet ef database update  --context  IdentityDataContext

1.2 数据迁移

第一次是初始化:

dotnet ef migrations add Initial

随后开发的过程中增加表时,迁移是增加相应的表名:

dotnet ef migrations add ChangeDeleteBehavior

从多个数据库的话,要选择数据库上迁移建立表:

dotnet ef migrations list --context DataContext

dotnet ef migrations add Orders --context DataContext

如果发现迁移的表不正确,可以把Migrations目录下的相关的cs文件删除,如Orders发现数据库里设计的不对,重新migrate时,可以删除如下两个文件,重新运行迁移命令即可:

......\Migrations\20181015071529_Orders.cs

......\Migrations\20181015071529_Orders.Designer.cs

2 .appsetting

{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Microsoft.EntityFrameworkCore": "Information",
"Microsoft.AspNetCore.NodeServices": "Information",
"Default": "Warning"
},
"Data": {
"Products": {
"ConnectionString": "Server=(localdb)\\MSSQLLocalDB;Database=SportsStoreAngular;Trusted_Connection=True;MultipleActiveResultSets=true"
}
}
}
}

dotnet sql-cache create "Data Source=(localdb)\MSSQLLocalDB;Database=SportsStoreAngular;Trusted_Connection=True;MultipleActiveResultSets=true" "dbo" "SessionData"

Trusted_Connection=True;这句不加,就出现了”管道的另一端上无任何进程“的错误,估计跟用户校验相关。

另外注意:

在有用户密码的数据库中,使用如下命令:

dotnet sql-cache create 'Data Source=localhost,1433;Database=SimpleGlossary;User Id=sa;password="dusf123SQL!";MultipleActiveResultSets=true' 'dbo' 'SessionData'

注意上面语句的单引号里面是双引号,主要是解决密码里有个!特殊字符,命令行不识别的问题

2.angular-cli创建MVC项目

dotnet new mvc --language C# --auth None --framework netcoreapp2.

dotnet add package Microsoft.AspNetCore.SpaServices

dotnet add package Microsoft.EntityFrameworkCore --version 1.1.
dotnet add package Microsoft.EntityFrameworkCore.Design --version 1.1.
dotnet add package Microsoft.EntityFrameworkCore.SqlServer --version 1.1.

3. npm的镜像替换成淘宝

1.得到原本的镜像地址

npm get registry

> https://registry.npmjs.org/

设成淘宝的

npm config set registry http://registry.npm.taobao.org/

yarn config set registry http://registry.npm.taobao.org/

2.换成原来的

npm config set registry https://registry.npmjs.org/

4.关于循环引用的问题

.
public Product GetProduct(long id) {
Product result = context.Products
.Include(p => p.Supplier)
.Include(p => p.Ratings)
.First(p => p.ProductId == id);
if (result != null) {
if (result.Supplier != null) {
result.Supplier.Products = null;
}
if (result.Ratings != null) {
foreach (Rating r in result.Ratings) {
r.Product = null;
}
}
}
return result;
} .
public Product GetProduct(long id) {
Product result = context.Products
.Include(p => p.Supplier).ThenInclude(s => s.Products)
.Include(p => p.Ratings)
.First(p => p.ProductId == id);
if (result != null) {
if (result.Supplier != null) {
result.Supplier.Products = result.Supplier.Products.Select(p =>
new Product {
ProductId = p.ProductId,
Name = p.Name,
Category = p.Category,
Description = p.Description,
Price = p.Price,
});
}
if (result.Ratings != null) {
foreach (Rating r in result.Ratings) {
r.Product = null;
}
}
}
return result;
}
}

上面11和22的代码区别是,22多了个ThenInclude(s => s.Products). Inlclude和ThenInclude都是表示让EF装载关联数据的。Include只关联和id相等的产品信息,即11中的Supllier的Products有多个product的话,只会关联出

productId和查询id一直的产品,并不会把所有的与该supplier关联的产品装载出来。与此对应的结果如下所示:

.
{
"productId":,"name":"Kayak","category":"Watersports",
"description":"A boat for one person","price":275.00,
"supplier":{
"supplierId":,"name":"Splash Dudes","city":"San Jose",
"state":"CA","products":null},
"ratings":[{"ratingId":,"stars":,"product":null},
{"ratingId":,"stars":,"product":null}]
} .
{
"productId":,"name":"Kayak","category":"Watersports",
"description":"A boat for one person","price":275.00,
"supplier":{"supplierId":,"name":"Splash Dudes","city":"San Jose",
"state":"CA",
"products":[
{ "productId":,"name":"Kayak","category":"Watersports",
"description":"A boat for one person", "price":275.00,
"supplier":null,"ratings":null},
{ "productId":,"name":"Lifejacket","category":"Watersports",
"description":"Protective and fashionable","price":48.95,
"supplier":null,"ratings":null}]},
"ratings":[{"ratingId":,"stars":,"product":null},
{"ratingId":,"stars":,"product":null}]
}

5.关于数据库删除相关数据的行为问题

protected override void OnModelCreating(ModelBuilder modelBuilder) {
modelBuilder.Entity<Product>().HasMany<Rating>(p => p.Ratings)
.WithOne(r => r.Product).OnDelete(DeleteBehavior.Cascade);
modelBuilder.Entity<Product>().HasOne<Supplier>(p => p.Supplier)
.WithMany(s => s.Products).OnDelete(DeleteBehavior.SetNull);
}

如上所示,OnModelCreating函数会重载对数据库操作行为的默认定义。里面的第一句可以将解释为,针对Product实体,它有多个Rating的关联实体,每个Rating实体关联一个Product实体。如果删除Product实体,Rating的实体必须同时删掉。

第二句解释为:针对Product实体,它有一个Supplier的关联实体,每个Supplier实体关联多个Product实体。如果删除Product实体,Supplier的实体设置为null。

6.待解决的问题

  • 其他url不能直接跳转到主页
  • checkout的几个步骤,刷新页面都会跳到购物车
  • Admin的几个页面直接输入url,会切换到用户登录页面; 每次即使已经输入用户密码仍是这样;为什么不能保存用户密码,而要每次输入呢

7. json

. cs里JsonConvert.SerializeObject(products);将对象转化成json的字符串

  public static object DeserializeObject(string value);

  public static string SerializeObject(object value);
 

js里:JSON.stringify(this.newProduct); 将对象转换成json的字符串

如:

JSON.stringify({});                        // '{}'
JSON.stringify(true); // 'true'
JSON.stringify("foo"); // '"foo"'
JSON.stringify([1, "false", false]); // '[1,"false",false]'
JSON.stringify({ x: 5 }); // '{"x":5}' 相反的过程函数是 JSON.parse("string")
JSON.parse('{}'); // {}
JSON.parse('true'); // true
JSON.parse('"foo"'); // "foo"
JSON.parse('[1, 5, "false"]'); // [1, 5, "false"]
JSON.parse('null'); // null
JSON.parse('1'); //

Essential pro angular and asp.net core 笔记的更多相关文章

  1. User Authentication with Angular and ASP.NET Core

    User authentication is a fundamental part of any meaningful application. Unfortunately, implementing ...

  2. [原]CentOS7.2最小安装环境部署Asp.NET Core笔记

    转载请注明原作者(think8848)和出处(http://think8848.cnblogs.com) 写在前面的话 不知不觉在cnblogs上注册已经10多年了,看我的园龄就直接暴露了我实际年龄, ...

  3. docker部署angular和asp.net core组成的前后端分离项目

    最近使用docker对项目进行了改进,把步骤记录一下,顺便说明一下项目的结构. 项目是前后端分离的项目,后端使用asp.net core 2.2,采用ddd+cqrs架构的分层思想,前端使用的是ang ...

  4. asp.net core 笔记

     dnvm use 1.0.0-rc1-final -r clr    切换版本

  5. Angular调用Asp.net Core JWT Authentication接口

    基本思路是调用登录接口,获取token,使用token请求其他JWT接口: getHomeDetails(): Observable<HomeDetails> { let headers ...

  6. Angular 5和ASP.NET Core入门

    我希望你们都知道Angular 5已经发布了.在本文中,我们将看到如何使用Angular5TemplateCore开始使用Angular 5和ASP.NET Core. 使用Angular5Templ ...

  7. 【原生态跨平台:ASP.NET Core 1.0(非Mono)在 Ubuntu 14.04 服务器上一对一的配置实现-篇幅1】

    鸡冻人心的2016,微软高产年. build 2016后 各种干货层出不穷. 1 Win10 集成了bash  ,实现了纳德拉的成诺,Microsoft Love Linux!!! 2 跨平台  ,收 ...

  8. 在Visual Studio 2017中使用Asp.Net Core构建Angular4应用程序

    前言 Visual Studio 2017已经发布了很久了.做为集成了Asp.Net Core 1.1的地表最强IDE工具,越来越受.NET系的开发人员追捧. 随着Google Angular4的发布 ...

  9. Asp.Net Core 轻松学-项目目录和文件作用介绍

    前言     上一章介绍了 Asp.Net Core 的前世今生,并创建了一个控制台项目编译并运行成功,本章的内容介绍 .NETCore 的各种常用命令.Asp.Net Core MVC 项目文件目录 ...

随机推荐

  1. Elasticsearch 学习总结 - 相关配置补充说明

    一.   Elasticsearch的基本概念 term索引词,在elasticsearch中索引词(term)是一个能够被索引的精确值.foo,Foo Foo几个单词是不相同的索引词.索引词(ter ...

  2. python 时间模块time,datetime

    模块(module)是 Python 中非常重要的东西,你可以把它理解为 Python 的扩展工具.换言之,Python 默认情况下提供了一些可用的东西,但是这些默认情况下提供的还远远不能满足编程实践 ...

  3. Hyperledger Fabric链码之三

    在<Hyperledger Fabric链码之一>和<Hyperledger Fabric链码之二>中我们介绍了链码的定义,并通过dev网络测试了测试了自己编写的链码程序. 本 ...

  4. Linux命令-基本变量类型及其运算

    [root@Redis01 ~]# cd /install/[root@Redis01 install]# mkdir -p test && cd test -s修改时间:[root@ ...

  5. Redis学习笔记(3)-XShell连接CentOSMini,并安装Redis

    使用XShell远程连接CentOSMini 点击download下载XShell5.0. 下载之后安装.配置XShell. 配置XShell前的准备 打开VM,启动CentOSMini.CentOS ...

  6. 【转载】阿里云ECS服务器监控资源使用情况

    在阿里云Ecs服务器运维过程中,无论是Centos系统还是Windows系统,有时候我们需要监控分析最新的服务器资源利用率等运行情况,例如最近3个小时CPU使用率情况.内存使用率.网络流入带宽.网络流 ...

  7. 关于我空间那篇名为《JavaScript axError:Unexpected token ILLEGAL 很简单的代码……》的随笔

    如图所示,我是空间的那片随笔,这里就不贴出链接了,也不希望你有这个兴趣,不知道会不会有人无意间闯入,额,自从日志用windows live writer 2009 发了那篇随笔后,我在网页上一直没能正 ...

  8. 【开源程序(C++)】获取bing图片并自动设置为电脑桌面背景

    众所周知,bing搜索网站首页每日会更新一张图片,张张漂亮(额,也有一些不合我口味的),特别适合用来做电脑壁纸. 我们想要将bing网站背景图片设置为电脑桌面背景的通常做法是: 上网,搜索bing 找 ...

  9. Netty实战十之编解码器框架

    编码和解码,或者数据从一种特定协议的格式到另一种格式的转换.这些任务将由通常称为编解码器的组件来处理.Netty提供了多种组件,简化了为了支持广泛的协议而创建自定义的编解码器的过程.例如,如果你正在构 ...

  10. 程序员晋级CTO之路的8大准则

    推荐阅读: 大数据智慧平台落地方案 Nginx + 阿里云SSL + tomcat 实现https访问代理 永远别忘了TD 再确认测试代码前,先找别人帮你检查下是否无误.在别人做之前尽量检查出bug并 ...