当前位置:网站首页 / .NET CORE / 正文

ay的C#8.0和net5高级编程笔记14-C#和.NET的实际应用

时间:2021年05月06日 | 作者 : aaronyang | 分类 : .NET CORE | 浏览: 816次 | 评论 0

Blazor允许使用C#而不是Javascrip构建服务器端或客户端组件以及用户界面。

所有Web平台都有大量可用的CMS,比如PHP的wordpress和Python的Django。

.NET Framework(我自己简称NF)有Episerver和Sitecore,但是不能用在NC上。支持NC的包括Piranha CMS,Squidex 和 Orchard Core


NC2.1 有SignalR,Razor类库,GDPR支持,IDentity UI库和搭建脚手架scaffolding,集成测试,ApiController和ActionResult<T>,Web Api,IHttpClientFactory

NC2.2 Kestrel中的HTTP2,进程内托管模式,健康检查API,开放的API分析器,端点路由。

NC3.0 Blazor服务器端和客户端,Razor类库中的静态资产,用于MVC服务注册的新选项


更多WebSocket信息:查看

SignalR通过对多种底层通信技术进行抽象,简化了向应用程序添加实时Web功能的过程。

SignalR会在WebSocket可用使用WebSocket,不可用的时候,使用其他的,例如AJAX长轮询。SignalR是用于服务器-客户端过程调用(RPC)的API。RPC从服务器端的.NET Core代码中调用客户端的JavaScript函数。SignalR有用于定义管道的集线器,并使用两个内置的集线器协议自动处理消息调度:JSON和基于MessagePack的二进制协议。


在服务器端SignalR支持ASP.NET CORE能运行的所有地方运行,SignalR的客户端:支持IE11以上,和最新的其他浏览器,例如谷歌。

.NET客户端,包括Android和IOS移动应用的Xamarin,Java8后续版本。

更多信息:https://docs.microsoft.com/zh-cn/aspnet/core/signalr/introduction?view=aspnetcore-5.0


WebAssembly: 2017年,WebAssembly Consensus已经完成,所有主流浏览器都支持WebAssembly

WebAssembly(Wasm)是一种用于虚拟机的二进制指令格式,这种格式提供了一种在Web上以接近本机的速度运行使用多种语言编写的代码的方法。Wasm是用于编译C#等高级语言的可移植目标。

更多信息:https://webassembly.org


Blazor是编程或应用模型,并且带有两个托管模型。

服务器端Blazor在服务器端运行,使用SignalR与客户端通信,已经在NC3中了。

客户端Blazor使用WebAssembly在客户端运行

这意味着Web开发人员只需要编写一次Blazor组件,然后就可以在服务器端或者客户端运行他们。

https://github.com/AdrienTorris/awesome-blazor


UWP只能win10的设备运行,NC的WPF和Winform程序只能在Windows运行


www.ayjs.net==================


为Northwind实体模型创建类库

新建chapter14文件夹,vs2019新建PracticalApps解决方案

后面的章节知识会用到

image.png

新建NorthwindEntitesLib net5类库

然后nuget安装Microsoft.EntityFrameworkCore.Sqlite


添加8个类 Category.cs Customer.cs Employee.cs Order.cs OrderDetail.cs Product.cs Shipper.cs(发货人)和Supplier.cs(供应商)

using System;
using System.Collections;
using System.Collections.Generic;

namespace NorthwindEntitesLib
{
    //添加8个类 www.ayjs.net
    public class Category
    {
        public int CategoryID { get; set; }
        public string CategoryName { get; set; }
        public string Description { get; set; }
        public ICollection<Product> Products { get; set; }

    }
    public class Product
    {
        public int ProductID { get; set; }
        public string ProductName { get; set; }
        public int? SupplierID { get; set; }

        public int? CategoryID { get; set; }

        public string QuantityPerUnit { get; set; }

        public decimal? UnitPrice { get; set; } = 0;
        public short? UnitsInStock { get; set; } = 0;
        public short? UnitsOnOrder { get; set; } = 0;
        public short? ReorderLevel { get; set; } = 0;
        public bool Discontinued { get; set; } = false;



        public Category Category { get; set; }
        public Supplier Supplier { get; set; }

    }
    public class Supplier
    {
        public int SupplierID { get; set; }
        public string CompanyName { get; set; }
        public string ContactName { get; set; }
        public string ContactTitle { get; set; }
        public string Address { get; set; }
        public string City { get; set; }
        public string Region { get; set; }
        public string PostalCode { get; set; }
        public string Country { get; set; }

        public string Phone { get; set; }
        public string Fax { get; set; }
        public string HomePage { get; set; }

        public ICollection<Product> Products { get; set; }
    }


    public class Customer
    {
        public string CustomerID { get; set; }
        public string CompanyName { get; set; }
        public string ContactName { get; set; }
        public string ContactTitle { get; set; }
        public string Address { get; set; }
        public string City { get; set; }
        public string Region { get; set; }
        public string PostalCode { get; set; }
        public string Country { get; set; }

        public string Phone { get; set; }
        public string Fax { get; set; }


        public ICollection<Order> Orders { get; set; }
    }
    public class Order
    {
        public int OrderID { get; set; }
        public string CustomerID { get; set; }
        public int  EmployeeID { get; set; }

        public DateTime? OrderDate { get; set; }
        public DateTime? RequiredDate { get; set; }
        public DateTime? ShippedDate { get; set; }
        public int ShipVia { get; set; }
        public decimal? Freight { get; set; }

        public Customer Customer { get; set; }
        public Employee Employee { get; set; }
        public Shipper Shipper { get; set; }

        public ICollection<OrderDetail> OrderDetails { get; set; }
    }
    public class OrderDetail
    {
        public int OrderID { get; set; }
        public int ProductID { get; set; }
        public decimal UnitPrice { get; set; } = 0;
        public short Quantity { get; set; } = 1;
        public double Discount { get; set; } = 0;
        public Order Order { get; set; }
        public Product Product { get; set; }
    }
    public class Employee {
        public int EmployeeID { get; set; }
        public string LastName { get; set; }

        public string FirstName { get; set; }
        public string Title { get; set; }
        public string TitleOfCourtesy { get; set; }
        public DateTime? BirthDate { get; set; }
        public DateTime? HireDate { get; set; }

        public string Address { get; set; }
        public string City { get; set; }
        public string Region { get; set; }
        public string PostalCode { get; set; }
        public string Country { get; set; }

        public string HomePhone { get; set; }

        public string Extension { get; set; }

        public string Notes { get; set; }

        public ICollection<Order> Orders { get; set; }
    }

    public class Shipper
    {
        public int ShipperID { get; set; }
        public string ShipperName { get; set; }
        public string Phone { get; set; }

        public ICollection<Order> Orders { get; set; }
    }

    }

基本上和数据库的设计一样

数据库的real类型对应double,money对应decimal


为Northwind数据库上下文创建类库

新建个NC类库,名叫NorthwindContextLib

修改NorthwindContextLib.csproj

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net5.0</TargetFramework>
  </PropertyGroup>
	<ItemGroup>
		<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="5.0.5" />
	</ItemGroup>
</Project>

修改Class1.cs名为Northwind,建议和数据库名一致

image.png

添加对NorthwindEntitesLib的引用

八个表八个 DbSet,属性名和表名一致

using Microsoft.EntityFrameworkCore;
using NorthwindEntitesLib;
using System;

namespace NorthwindContextLib
{
    /// <summary>
    /// www.ayjs.net
    /// </summary>
    public class Northwind : DbContext
    {
        public DbSet<Category> Categories { get; set; }
        public DbSet<Customer> Customers { get; set; }
        public DbSet<Employee> Employees { get; set; }
        public DbSet<Order> Orders { get; set; }
        public DbSet<OrderDetail> OrderDetails { get; set; }
        public DbSet<Product> Products { get; set; }
        public DbSet<Shipper> Shippers { get; set; }
        public DbSet<Supplier> Suppliers { get; set; }

        public Northwind(DbContextOptions<Northwind> options) : base(options)
        {

        }
        protected override void OnModelCreating(ModelBuilder modelbuilder)
        {
            base.OnModelCreating(modelbuilder);

            modelbuilder.Entity<Category>().Property(x => x.CategoryName).IsRequired().HasMaxLength(15);
            modelbuilder.Entity<Category>().HasMany(x => x.Products).WithOne(p => p.Category);

            modelbuilder.Entity<Customer>().Property(x => x.CustomerID).IsRequired().HasMaxLength(5);
            modelbuilder.Entity<Customer>().Property(x => x.CompanyName).IsRequired().HasMaxLength(40);
            modelbuilder.Entity<Customer>().Property(x => x.ContactName).HasMaxLength(30);
            modelbuilder.Entity<Customer>().Property(x => x.Country).HasMaxLength(15);
            modelbuilder.Entity<Customer>().HasMany(x => x.Orders).WithOne(p => p.Customer);

            modelbuilder.Entity<Employee>().Property(x => x.LastName).IsRequired().HasMaxLength(20);
            modelbuilder.Entity<Employee>().Property(x => x.FirstName).IsRequired().HasMaxLength(10);
            modelbuilder.Entity<Employee>().Property(x => x.Country).HasMaxLength(15);
            modelbuilder.Entity<Employee>().HasMany(x => x.Orders).WithOne(p => p.Employee);

            modelbuilder.Entity<Product>().Property(x => x.ProductName).IsRequired().HasMaxLength(40);

            modelbuilder.Entity<Product>().HasOne(x => x.Category).WithMany(p => p.Products);
            modelbuilder.Entity<Order>().HasOne(x => x.Shipper).WithMany(p => p.Orders).HasForeignKey(o=>o.ShipVia);

            modelbuilder.Entity<OrderDetail>().ToTable("Order Details");

            modelbuilder.Entity<OrderDetail>().HasKey(od => new { od.OrderID, od.ProductID });

            modelbuilder.Entity<Supplier>().Property(x => x.CompanyName).IsRequired().HasMaxLength(40);
            modelbuilder.Entity<Supplier>().HasMany(x => x.Products).WithOne(p => p.Supplier);
        }


    }
}

这一章主要是给后面几章的知识做一个介绍,还有 建立 Northwind数据库模型,后面的示例好操作。


www.ayjs.net==================











推荐您阅读更多有关于“C#8.0core3,”的文章

猜你喜欢

额 本文暂时没人评论 来添加一个吧

发表评论

必填

选填

选填

必填

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

  查看权限

抖音:wpfui 工作wpf

目前在合肥企迈科技公司上班,加我QQ私聊

2023年11月网站停运,将搬到CSDN上

AYUI8全源码 Github地址:前往获取

杨洋(AaronYang简称AY,安徽六安人)AY唯一QQ:875556003和AY交流

高中学历,2010年开始web开发,2015年1月17日开始学习WPF

声明:AYUI7个人与商用免费,源码可购买。部分DEMO不免费

查看捐赠

AYUI7.X MVC教程 更新如下:

第一课 第二课 程序加密教程

标签列表