您当前的位置: 首页 >  .net

寒冰屋

暂无认证

  • 1浏览

    0关注

    2286博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

使用EntityFramework Core和Enums作为字符串的ASP.NET Core Razor页面——第三部分

寒冰屋 发布时间:2018-12-28 19:55:20 ,浏览量:1

目录

介绍

使用代码

添加项目和项目状态处理

  • 下载源文件 - 989.1 KB
介绍

这是一篇由多部分组成的文章的第三部分,演示了通过EntityFramework Core 2.1(EF)将C#enum值映射到数据库表中的string值。它解决了enum与应用程序实体的一对多和多对多关系中的值映射问题。它在ASP.NET Core Razor Page应用程序的上下文中执行此操作。

EF是对象关系映射器(ORM)。在诸如此示例的应用程序中,有两个“世界”。一个是在C#中作为对象模型存在的对象世界。另一个是存在于关系数据库中的关系世界,如Microsoft SQL Server。这两个世界并不一致。ORM的功能,如EntityFramework,就是这两个世界之间的桥梁,并促进它们之间的数据传输。

第一部分。设置实体框架数据上下文和初始Customer的Razor页面

第二部分。完成了Customers的CRUD功能

在第三部分。我们将创建Project和ProjectState实体并在ProjectState和Project之间实现一个一对多的关系,如下:

  • 添加Project,ProjectState和ProjectStateDescription实体。
  • 添加EF迁移以在数据库中创建和配置Projects和ProjectStateDescriptions表。
  • 演示enum对象模型实体中的string值与Projects和ProjectStateDescriptions数据库表中的值之间的转换。
  • 搭建,实现和测试包含ProjectState功能的ProjectCRUD页面,如CustomerProjects.cshtml,CustomerProjectCreate.cshtml,CustomerProjectDetails.cshtml和CustomerProjectDelete.cshtml Razor页面。
使用代码

添加初始项目处理。

接下来,我们启用Customer项目处理。该应用程序使用Customer作为“网关”实体; 一切都是通过Customer。Customer和Projects之间存在一对多的关系。因此,我们需要修改Customer类。

修改后的Customer.cs:

using System.Collections.Generic;

namespace QuantumWeb.Model
{
    /// 
    /// Customer Class
    /// 
    public class Customer
    {
        #region Constructors

        /// 
        /// Parameter-less Constructor
        /// 
        /// 
        /// Required for scaffolding the UI
        /// 
        public Customer()
        {
        } // end public Customer()

        #endregion // Constructors

        /// 
        /// Customer Identifier, primary key
        /// 
        public int CustomerId { get; set; }
        /// 
        /// Customer Name
        /// 
        public string CustomerName { get; set; }
        /// 
        /// Primary Customer Contact
        /// 
        public string CustomerContact { get; set; }
        /// 
        /// Customer Contact Phone Number
        /// 
        public string CustomerPhone { get; set; }
        /// 
        /// Customer Contact Email Address
        /// 
        public string CustomerEmail { get; set; }

        #region Navigation Properties

        /// 
        /// List of Projects
        /// 
        public List Projects { get; set; }

        #endregion // Navigation Properties

    } // end public class Customer

} // end namespace QuantumWeb.Model

我们添加了一个项目列表。在这里,我们将某些属性标识为导航属性。这些属性引用其他类/实体,以便我们可以在处理中导航到它们。Customer在Projects列表中表示零或多个Projects。初始Project类定义如下。

初始Project.cs:

namespace QuantumWeb.Model
{
    /// 
    /// Project Class
    /// 
    public class Project
    {
        /// 
        /// Project Identifier, primary key
        /// 
        public int ProjectId { get; set; }
        /// 
        /// Project Name
        /// 
        public string ProjectName { get; set; }

        #region Navigation Properties

        /// 
        /// Customer Identifier
        /// 
        public int CustomerId { get; set; }

        /// 
        /// Customer
        /// 
        /// 
        /// Every Project has a Customer
        /// 
        public Customer Customer { get; set; }

        /// 
        /// Project Status Code
        /// 
        public ProjectState ProjectStateCode { get; set; }

        /// 
        /// ProjectStateDescription Reference
        /// 
        public ProjectStateDescription ProjectStateDescription { get; set; }

        #endregion // Navigation Properties

    } // end public class Project

} // end namespace QuantumApp.Model

除了定义初始化Project类之外,我们还将在Model文件夹中定义ProjectState enum。

ProjectState.cs:

namespace QuantumWeb.Model
{   
    /// 
    /// Project State Enumeration
    /// 
    public enum ProjectState
    {
        Prospect,
        UnderReview,
        StartScheduled,
        InProgress,
        Completed
    } // end public enum ProjectState

} // end namespace QuantumWeb.Model

这个enum指定了Project工作流的状态。

  • Prospect。这涉及一个有前景的Project。这个Project可能是通过推荐或其他营销工作提出的。尚未进行任何研究,且规格尚不清楚。
  • UnderReview。在这种状态下,Project制定了要求,初始预算和进度表。没有承诺Quantum或者Customer。
  • StartScheduled。已经指定了工作开始的日期,并且正在准备开始工作。
  • InProgress。实际工作已经开始但尚未完成。
  • Completed。项目工作完成。

如前所述,我们对此应用程序有两个目标。

  1. 我们应该为Project将在UI中显示的每个状态定义简短描述,以帮助用户理解每个状态的含义。
  2. 每个enum值都作为string类型存储在数据库中。

为了满足ProjectState enum的这些要求,我们定义了ProjectStateDescription类。

ProjectStateDescription.cs:

using System.Collections.Generic;

namespace QuantumWeb.Model
{
    /// 
    /// Project State Description Class
    /// 
    public class ProjectStateDescription
    {
        /// 
        /// ProjectState Code
        /// 
        public ProjectState ProjectStateCode { get; set; }

        /// 
        /// State Description
        /// 
        public string StateDescription { get; set; }

        #region Navigation Properties

        /// 
        /// Projects Collection
        /// 
        public List Projects { get; set; }

        #endregion // Navigation Properties

    } // end public class ProjectStateDescription

} // end namespace QuantumWeb.Model

ProjectState对Projects的一对多的关系,通过导航属性启用。每个Project都有一个ProjectStateDesciption。每个ProjectStateDescripton都有一个Projects集合。

接下来,我们需要为Project和ProjectStateDescription定义EF配置类,并在QuantumDbContext类中包含所有内容。所有此活动都发生在Data文件夹中。

初始ProjectConfiguration.cs:

using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using QuantumWeb.Model;

namespace QuantumWeb.Data
{
    public class ProjectConfiguration : IEntityTypeConfiguration
    {
        public void Configure(EntityTypeBuilder builder)
        {
            builder.ToTable("Projects");
            builder.HasKey(p => p.ProjectId);
            builder.Property(p => p.ProjectId)
            .HasColumnType("int");
            builder.Property(p => p.ProjectName)
            .IsRequired()
            .HasColumnType("nvarchar(80)")
            .HasMaxLength(80);
            builder.Property(p => p.CustomerId)
            .HasColumnType("int")
            .IsRequired();
            builder.HasOne(p => p.Customer)
            .WithMany(c => c.Projects)
            .HasForeignKey(p => p.CustomerId)
            .IsRequired();
            builder.Property(p => p.ProjectStateCode)
            .HasColumnType("nvarchar(15)")
            .HasDefaultValue(ProjectState.Prospect)
            .HasConversion(
                p => p.ToString(),
                p => (ProjectState)Enum.Parse(typeof(ProjectState), p));
            builder.HasOne(p => p.ProjectStateDescription)
            .WithMany(pd => pd.Projects)
            .HasForeignKey(p => p.ProjectStateCode);
        } // end public void Configure(EntityTypeBuilder builder)

    } // end public class ProjectConfiguration : IEntityTypeConfiguration

} // end namespace QuantumWeb.Data

看看下面提取的行:

builder.HasOne(p => p.Customer)
.WithMany(c => c.Projects)
.HasForeignKey(p => p.CustomerId)
.IsRequired();

对这些行的解释是,“每个Project都有一个带有许多Projects的Customer。每个Project都映射到Projects数据库中的表,通过外键CustomerId,并且这是必需的。因此,Customer- Project关系是一对多。

在一对多ProjectStateDescription- Project关系被配置为:

builder.HasOne(p => p.ProjectStateDescription)
.WithMany(pd => pd.Projects)
.HasForeignKey(p => p.ProjectStateCode);

接下来,我们将了解处理enum的值到数据库string列配置的方式。

builder.Property(p => p.ProjectStateCode)
.HasColumnType("nvarchar(15)")
.HasDefaultValue(ProjectState.Prospect)
.HasConversion(
    p => p.ToString(),
    p => (ProjectState)Enum.Parse(typeof(ProjectState), p));

这些行首先在Projects名为的表中配置一个列ProjectStateCode,类型为nvarchar(15),其默认值来自ProjectState.Prospect。接下来,定义ProjectState值和string值之间的转换。当将值从ProjectState enum移动到Projects表时,将使用该ToString()函数转换值。换另一种方式时,表中string的值将被解析为一个enum值。始终使用相同的方案在数据库列中的enum值和string值之间进行转换。

ProjectStateDescriptionConfiguration类如下所示。

ProjectStateDescriptionConfiguration.cs:

using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using QuantumWeb.Model;

namespace QuantumWeb.Data
{
    /// 
    /// ProjectState Description Configuration Class
    /// 
    public class ProjectStateDescriptionConfiguration : 
                   IEntityTypeConfiguration
    {

        public void Configure(EntityTypeBuilder builder)
        {
            builder.ToTable("ProjectStateDescriptions");
            builder.HasKey(p => p.ProjectStateCode);
            builder.Property(p => p.ProjectStateCode)
            .HasColumnType("nvarchar(15)")
            .HasConversion(
                p => p.ToString(),
                p => (ProjectState)Enum.Parse(typeof(ProjectState), p));
            builder.Property(p => p.StateDescription)
            .IsRequired()
            .HasColumnType("nvarchar(80)")
            .HasMaxLength(80);
        } // end public void Configure(EntityTypeBuilder builder)

    } // end public class ProjectStateDescriptionConfiguration : 
      // IEntityTypeConfiguration

} // end namespace QuantumWeb.Data

现在,我们更新QuantumDbContext类。

然后更新QuantumDbContext.cs:

using Microsoft.EntityFrameworkCore;
using QuantumWeb.Model;

namespace QuantumWeb.Data
{
    public class QuantumDbContext : DbContext
    {
        public QuantumDbContext (DbContextOptions options)
            : base(options)
        {
        } // end public QuantumDbContext (DbContextOptions options)

        #region DbSets

        /// 
        /// Customer DbSet
        /// 
        public DbSet Customers { get; set; }

        /// 
        /// Project DbSet
        /// 
        public DbSet Projects { get; set; }

        /// 
        /// ProjectStateDescription DbSet
        /// 
        public DbSet ProjectStateDescriptions { get; set; }

        #endregion // DbSets

        /// 
        /// Data Model Creation Method
        /// 
        /// ModelBuilder instance
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.ApplyConfiguration(new CustomerConfiguration());
            modelBuilder.ApplyConfiguration(new ProjectConfiguration());
            modelBuilder.ApplyConfiguration(new ProjectStateDescriptionConfiguration());
        } // end  protected override void OnModelCreating(ModelBuilder modelBuilder)

    } // end public class QuantumDbContext : DbContext

} // end namespace QuantumWeb.Data

现在为Project和ProjectState实体添加EF迁移。(译者注:同样需要调整appsettings.json中数据库连接字符串,建议删除Migrations文件夹后在执行下面的命令,同时提醒,在执行下面命令后需要命令update-database更新到数据库)

Add-Migration Added-Project-ProjectState

生成 ~\Migrations\20181021203503_Added-Project-ProjectState.cs:

using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;

namespace QuantumWeb.Migrations
{
    public partial class AddedProjectProjectState : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "ProjectStateDescriptions",
                columns: table => new
                {
                    ProjectStateCode = 
                    table.Column(type: "nvarchar(15)", nullable: false),
                    StateDescription = 
                    table.Column(type: "nvarchar(80)", maxLength: 80, nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_ProjectStateDescriptions", x => x.ProjectStateCode);
                });

            migrationBuilder.CreateTable(
                name: "Projects",
                columns: table => new
                {
                    ProjectId = table.Column(type: "int", nullable: false)
                        .Annotation("SqlServer:ValueGenerationStrategy", 
                        SqlServerValueGenerationStrategy.IdentityColumn),
                    ProjectName = table.Column(type: "nvarchar(80)", 
                                  maxLength: 80, nullable: false),
                    CustomerId = table.Column(type: "int", nullable: false),
                    ProjectStateCode = table.Column
                    (type: "nvarchar(15)", nullable: false, defaultValue: "Prospect")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Projects", x => x.ProjectId);
                    table.ForeignKey(
                        name: "FK_Projects_Customers_CustomerId",
                        column: x => x.CustomerId,
                        principalTable: "Customers",
                        principalColumn: "CustomerId",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_Projects_ProjectStateDescriptions_ProjectStateCode",
                        column: x => x.ProjectStateCode,
                        principalTable: "ProjectStateDescriptions",
                        principalColumn: "ProjectStateCode",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateIndex(
                name: "IX_Projects_CustomerId",
                table: "Projects",
                column: "CustomerId");

            migrationBuilder.CreateIndex(
                name: "IX_Projects_ProjectStateCode",
                table: "Projects",
                column: "ProjectStateCode");
        }

        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(
                name: "Projects");

            migrationBuilder.DropTable(
                name: "ProjectStateDescriptions");
        }
    }
}

在Update- Database命令之后,SQL Server Management Studio(SSMS)中的数据库关系图如下所示。

使用Customer- Project- ProjectState表的QuantumDbContext数据库关系图:

https://img-blog.csdnimg.cn/20181228195522488

修改Project和ProjectState的Razor 页。

我们需要为项目的应用程序添加一些自定义客户Razor 页面。首先,我们需要为CustomerProjects添加一个指向Customer/Index页面的链接。

添加CustomerProjects 链接指向Pages\Customers\Index.cshtml:

@page
@model QuantumWeb.Pages.Customers.IndexModel

@{
    ViewData["Title"] = "Index";
}

Index

Create New

@Html.DisplayNameFor(model => model.Customer[0].CustomerName) @Html.DisplayNameFor(model => model.Customer[0].CustomerContact) @Html.DisplayNameFor(model => model.Customer[0].CustomerPhone) @Html.DisplayNameFor(model => model.Customer[0].CustomerEmail) @foreach (var item in Model.Customer) { @Html.DisplayFor(modelItem => item.CustomerName) @Html.DisplayFor(modelItem => item.CustomerContact) @Html.DisplayFor(modelItem => item.CustomerPhone) @Html.DisplayFor(modelItem => item.CustomerEmail) Edit | Details | Projects | Delete }

我们将如下构建几个自定义Customers Razor页面。

为客户设计的定制构建Razor页面:

https://img-blog.csdnimg.cn/20181228195522543

搭建Customers/CustomerProjects Razor 页面:

https://img-blog.csdnimg.cn/20181228195522598

单击“ 添加 ”将为CustomerProjects Index页面生成shell文件。

生成~Pages\Customers\CustomerProjects.cshtml

@page
@model QuantumWeb.Pages.Customers.CustomerProjectsModel
@{
    ViewData["Title"] = "CustomerProjects";
}

CustomerProjects

生成~Pages\Customers\CustomerProjects.cshtml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace QuantumWeb.Pages.Customers
{
    public class CustomerProjectsModel : PageModel
    {
        public void OnGet()
        {

        }
    }
}

我们将在每种情况下修改这些shell文件以满足我们的需求。CustomerProjects Index 页面的修改文件。

修改了~Pages\Customers\CustomerProjects.cshtml

@page "{id:int?}"
@model QuantumWeb.Pages.Customers.CustomerProjectsModel
@{
    ViewData["Title"] = "Customer Projects";
}

Customer Projects

Customer @Html.DisplayNameFor(model => model.Customer.CustomerId) @Html.DisplayFor(model => model.Customer.CustomerId) @Html.DisplayNameFor(model => model.Customer.CustomerName) @Html.DisplayFor(model => model.Customer.CustomerName) @Html.DisplayNameFor(model => model.Customer.Projects) Project ID Project Name Project State @foreach (var item in Model.Customer.Projects) { @Html.DisplayFor(modelItem => item.ProjectId) @Html.DisplayFor(modelItem => item.ProjectName) @Html.DisplayFor(modelItem => item.ProjectStateCode) Edit | Delete }
Create New Project | Back to List

“ {id:int?}”表示需要整数参数,id需要或者请求页面将返回HTTP 401(未找到页面)错误。在这种情况下,这是目标Customer的标识符(CustomerId)。另外,请注意引用该CustomerProjectCreate页面的链接。

Create New Project |

这将把我们带到CustomerProjectCreate尚未创建的页面,为引用Customer创建一个新的Project。

修改了~Pages\Customers\CustomerProjects.cshtml.cs

using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore;
using QuantumWeb.Data;
using QuantumWeb.Model;

namespace QuantumWeb.Pages.Customers
{
    public class CustomerProjectsModel : PageModel
    {
        private readonly QuantumDbContext _context;

        public CustomerProjectsModel(QuantumDbContext context)
        {
            _context = context;
        } // end public CustomerProjectsModel(QuantumDbContext context)

        public Customer Customer { get; set; }

        public async Task OnGet(int? id)
        {
            if (id == null)
            {
                return NotFound();
            } // endif (id == null)

            Customer = await _context.Customers
                .Include(c => c.Projects)
                    .FirstOrDefaultAsync(c => c.CustomerId == id);

            if (Customer == null)
            {
                return NotFound();
            } // endif (Customer == null)

            return Page();
        } // end public async Task OnGet(int? id)

    } // end public class CustomerProjectsModel : PageModel

} // end namespace QuantumWeb.Pages.Customers

请注意,OnGet处理程序具有可为空的整数参数,id应该是如上所述的CustomerId。

QuantumWeb 应用客户页面: https//localhost: 44306/Customers 具有项目链接。

https://img-blog.csdnimg.cn/20181228195522645

Customer Projects页面:https//localhost: 44306/Customers/CustomerProjects/1(无项目)

https://img-blog.csdnimg.cn/20181227224121669

“ 创建新项目 ”链接将激活自定义CustomerProjectCreate Razor页面。我们现在搭建这个页面。

搭建Customers/CustomerProjectCreate Razor页面:

https://img-blog.csdnimg.cn/2018122722531162

Initial~Pages\Customers\CustomerProjectCreate.cshtml.cs

using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using QuantumWeb.Data;
using QuantumWeb.Model;

namespace QuantumWeb.Pages.Customers
{
    public class CustomerProjectCreateModel : PageModel
    {
        private readonly QuantumDbContext _context;

        public CustomerProjectCreateModel(QuantumDbContext context)
        {
            _context = context;
        } // end public CustomerProjectCreateModel(QuantumContext context)

        [BindProperty]
        public Customer Customer { get; set; }

        public async Task OnGet(int? id)
        {
            if (id == null)
            {
                return NotFound();
            } // endif (id == null)

            Customer = await _context.Customers
                .Include(c => c.Projects)
                    .FirstOrDefaultAsync(c => c.CustomerId == id);

            if (Customer == null)
            {
                return NotFound();
            } // endif (Customer == null)

            ViewData["ProjectStateCode"] = new SelectList(_context.ProjectStateDescriptions,
                "ProjectStateCode", "StateDescription", ProjectState.Prospect);

            return Page();

        } // end public async Task OnGet(int? id)

        [BindProperty]
        public Project Project { get; set; }

        public async Task OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return Page();
            } // endif (!ModelState.IsValid)

            Project.CustomerId = Customer.CustomerId;

            _context.Projects.Add(Project);
            await _context.SaveChangesAsync();

            return RedirectToPage("./CustomerProjects", new { id = Customer.CustomerId });
        } // end public async Task OnPostAsync()

    } // end public class CustomerProjectCreateModel : PageModel

} // end namespace QuantumWeb.Pages.Customers

请注意此代码中的这些行。 

[BindProperty]
public Customer Customer { get; set; }

该[BindProperty]将Customer实例绑定到UI的元素,以便在浏览器和Web服务器之间保留它们的值。另请注意,此属性也适用于Project实例。

Customer = await _context.Customers
    .Include(c => c.Projects)
        .FirstOrDefaultAsync(c => c.CustomerId == id);

此语句对数据库执行查询,以检索Customer其主键值CustomerId与输入参数id值及其关联Project记录匹配的记录。如果有,该.Include函数的功能是查询中包含相关记录。

ViewData["ProjectStateCode"] = new SelectList(_context.ProjectStateDescriptions,
   "ProjectStateCode", "StateDescription", ProjectState.Prospect);

ViewData是一个无类型的键值字典,用于在CustomerProjectCreateModel类(在.cshtml.cs文件中)和.cshtml文件中的HTML 之间传递值。这类似于MVC中将数据从Controller 传递到View,在使用ViewData中,数据仅在HTTP请求中持久存在。其成员由ProjectStateDescriptions数据库表中的查询填充。在这种情况下,_context.ProjectStateDescriptions是IEnumerable从查询返回的。ProjectStateCode是表中的主键,表示ViewData字典中的键。StateDescription成为ViewData字典中的关联值。ViewData将用来填充在CustomerProjectCreate.cshtml(见下文)中的元素。ProjectState.Prospect是为从ProjectState enum中默认选择的值。您可以阅读更多ViewData信息在以下链接上 https://www.tektutorialshub.com/viewbag-viewdata-asp-net-core/。(译者注:需要在ProjectStateDescriptions数据库表中手动加入数据,具体可见下图展示的数据加,ProjectStateCode字段的值就是对应枚举中的值--定义在代码中的名称。如此值为Completed,StateDescription字段值为project is complete,以此类推)

初始化~Pages\ Customers\CustomerProjectCreate.cshtml:

@page
@model QuantumWeb.Pages.Customers.CustomerProjectCreateModel
@{
    ViewData["Title"] = "Create Customer Project";
}

Create Customer Project


    
        @Html.DisplayNameFor(model => model.Customer.CustomerId)
    
    
        @Html.DisplayFor(model => model.Customer.CustomerId)
    
    
        @Html.DisplayNameFor(model => model.Customer.CustomerName)
    
    
        @Html.DisplayFor(model => model.Customer.CustomerName)
    

Back to Customer Projects
@section Scripts { @{await Html.RenderPartialAsync("_ValidationScriptsPartial");} }

关键要素如下:

这个隐藏捕获目标CustomerId以便在发布时可用它来创建Project。


此元素将显示为UI中的下拉列表,其中包含CustomerProjectCreate.OnGet()方法中ViewData填充的值。

初始化 ~Pages\Customers\CustomerProjectCreate.cshtml:

https://img-blog.csdnimg.cn/20181228195522684

这将显示最初显示的Customers/CustomerProjectCreate页面。

CustomerProjectCreate 包含数据的页面:

https://img-blog.csdnimg.cn/20181228195522740

点击“ Create”后,我们会看到:

客户Projects页面添加Project:

https://img-blog.csdnimg.cn/20181228195522787

接下来的两个图显示了为两个Customers添加其他Projects之后的情况。

客户项目页面包含Mirarex Oil&Gas的2个项目:

https://img-blog.csdnimg.cn/20181227224121714

客户项目页面包含Polyolefin Processing, Inc.的3个项目

https://img-blog.csdnimg.cn/20181228195522829

我们现在可以添加另一个页面来编辑Customer项目,CustomerProjectEdit页面。

搭建 Customers/CustomerProjectEdit Razor Page

https://img-blog.csdnimg.cn/20181228195522869

初始化~Pages\Customers\CustomerProjectEdit.cshtml.cs

using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using QuantumWeb.Data;
using QuantumWeb.Model;

namespace QuantumApp.Pages.Customers
{
    public class CustomerProjectEditModel : PageModel
    {
        private readonly QuantumDbContext _context;

        public CustomerProjectEditModel(QuantumDbContext context)
        {
            _context = context;
        } // end public CustomerProjectEditModel(QuantumDbContext context)

        [BindProperty]
        public Customer Customer { get; set; }
        [BindProperty]
        public Project Project { get; set; }

        public async Task OnGet(int? id)
        {
            if (id == null)
            {
                return NotFound();
            } // endif (id == null)

            Project = await _context.Projects
                .Include(p => p.Customer)
                    .FirstOrDefaultAsync(p => p.ProjectId == id);

            if (Project == null)
            {
                return NotFound();
            } // endif (Project == null)

            Customer = Project.Customer;

            ViewData["ProjectStateCode"] = new SelectList(_context.ProjectStateDescriptions,
                "ProjectStateCode", "StateDescription", ProjectState.Prospect);

            return Page();
        } // end public async Task OnGet(int? id)

        public async Task OnPostAsync(int? id)
        {
            if (!ModelState.IsValid)
            {
                return Page();
            } // endif (!ModelState.IsValid)

            var projectToUpdate = await _context.Projects.FindAsync(id);

            if (projectToUpdate == null)
            {
                return NotFound();
            } // endif (projectToUpdate == null)

            projectToUpdate.CustomerId = Customer.CustomerId;

            if (await TryUpdateModelAsync(
                projectToUpdate,
                "project",
                p => p.ProjectName, p => p.ProjectStateCode))
            {
                await _context.SaveChangesAsync();
                return RedirectToPage("./CustomerProjects", new { id = Customer.CustomerId });
            }

            return Page();
        } // end public async Task OnPostAsync(int? id)

    } // end public class CustomerProjectEditModel : PageModel

} // end namespace QuantumApp.Pages.Customers

此代码与CustomerProjectCreate页面在.Include和ViewData方面具有相同的构件。

初始化~Pages\Customers\CustomerProjectEdit.cshtml

@page "{id:int?}"
@model QuantumWeb.Pages.Customers.CustomerProjectEditModel
@{
    ViewData["Title"] = "Edit Customer Project";
}

Edit Customer Project


    
        @Html.DisplayNameFor(model => model.Customer.CustomerId)
    
    
        @Html.DisplayFor(model => model.Customer.CustomerId)
    
    
        @Html.DisplayNameFor(model => model.Customer.CustomerName)
    
    
        @Html.DisplayFor(model => model.Customer.CustomerName)
    

Back to Customer Projects
@section Scripts { @{await Html.RenderPartialAsync("_ValidationScriptsPartial");} }

关于CustomerId的隐藏和,此页面具有与CustomerProjectCreate页面相同的元素。

Customer Projects 页面包含Mirarex Oil&Gas的2个项目——用于编辑:

https://img-blog.csdnimg.cn/20181227224121750

Mirarex Oil&Gas, Zolar Pipeline的客户项目编辑页面:

https://img-blog.csdnimg.cn/20181228195522919

客户项目页面包含Mirarex Oil & Gas 的2个项目——项目编辑:

https://img-blog.csdnimg.cn/20181227224121795

通过CustomerProjectDelete页面,此项目的最后一个功能是删除。

搭建 Customers/CustomerProjectDelete Razor页面:

https://img-blog.csdnimg.cn/20181227225311112

初始化~Pages\Customers\CustomerProjectDelete.cshtml.cs

using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore;
using QuantumWeb.Data;
using QuantumWeb.Model;

namespace QuantumWeb.Pages.Customers
{
    public class CustomerProjectDeleteModel : PageModel
    {
        private readonly QuantumDbContext _context;

        public CustomerProjectDeleteModel(QuantumDbContext context)
        {
            _context = context;
        } // end public CustomerProjectDeleteModel(QuantumContext context)

        [BindProperty]
        public Customer Customer { get; set; }
        [BindProperty]
        public Project Project { get; set; }

        public async Task OnGetAsync(int? id)
        {
            if (id == null)
            {
                return NotFound();
            } // endif (id == null)

            Project = await _context.Projects
                .Include(p => p.Customer)
                    .FirstOrDefaultAsync(p => p.ProjectId == id);

            if (Project == null)
            {
                return NotFound();
            } // endif (Project == null)

            Customer = Project.Customer;

            return Page();
        } // end public async Task OnGet(int? id)

        public async Task OnPostAsync(int? id)
        {
            if (id == null)
            {
                return NotFound();
            } // endif (id == null)

            Project = await _context.Projects
                .Include(p => p.Customer)
                .FirstOrDefaultAsync(p => p.ProjectId == id);

            if (Project != null)
            {
                _context.Projects.Remove(Project);
                await _context.SaveChangesAsync();
            } // endif (Project != null)

            return RedirectToPage("./CustomerProjects", new { id = Project.Customer.CustomerId });
        } // end public async Task OnPostAsync(int? id)

    } // end public class CustomerProjectDeleteModel : PageModel

} // end namespace QuantumWeb.Pages.Customer

初始化~Pages\Customers\CustomerProjectDelete.cshtml

@page "{id:int?}"
@model QuantumWeb.Pages.Customers.CustomerProjectDeleteModel
@{
    ViewData["Title"] = "Delete Customer Project";
}

Delete Customer Project

Are you sure you want to delete this?
@Html.DisplayNameFor(model => model.Customer.CustomerName) @Html.DisplayFor(model => model.Customer.CustomerName) @Html.DisplayNameFor(model => model.Project.ProjectId) @Html.DisplayFor(model => model.Project.ProjectId) @Html.DisplayNameFor(model => model.Project.ProjectName) @Html.DisplayFor(model => model.Project.ProjectName) @Html.DisplayNameFor(model => model.Project.ProjectStateCode) @Html.DisplayFor(model => model.Project.ProjectStateCode) Back to Customer Projects |

客户项目页面包含Mirarex Oil & Gas的3个项目:

https://img-blog.csdnimg.cn/20181227224121848

删除客户项目页面——删除Ouachita Shale:

https://img-blog.csdnimg.cn/20181227225311154

客户项目页面包含Mirarex Oil&Gas的2个项目:

https://img-blog.csdnimg.cn/20181227224121888

此时,我们可以总结下表中的测试数据:

Customers, Projects, ProjectStates

CustomerId

Customer Name

ProjectId

Project Name

ProjectStateCode

StateDescription

1

Mirarex Oil & Gas, LLC

1

Zolar Pipeline

UnderReview

Project is under review and negotiation

1

Mirarex Oil & Gas, LLC

2

Nelar Ranch Gas Fracturing

Prospect

Prospective or referred project

2

Polyolefin Processing, Inc.

3

Port Gibson Plant Expansion

Prospect

Prospective or referred project

2

Polyolefin Processing, Inc.

4

Jackson Plant Control System Upgrade

Prospect

Prospective or referred project

2

Polyolefin Processing, Inc.

5

Eutaw Plant Shutdown & Maintenance

Prospect

Prospective or referred project

下面可以进入第四部分进行学习。 

 

原文地址:https://www.codeproject.com/Articles/1264330/ASP-NET-Core-Razor-Pages-Using-EntityFramework-C-2

关注
打赏
1665926880
查看更多评论
立即登录/注册

微信扫码登录

0.0485s