ABP框架官网例子实践(1)(ASP.NET core+Multi Page Web Application)

参考:https://aspnetboilerplate.com/Pages/Articles/Introduction-With-AspNet-Core-And-Entity-Framework-Core-Part-1/index.html和https://aspnetboilerplate.com/Pages/Articles/Introduction-With-AspNet-Core-And-Entity-Framework-Core-Part-2/index.html

第一步:在core层创建实体

1、创建一个文件夹Tasks,在文件夹中创建一个Task实体(class)

using Abp.Domain.Entities;
using Abp.Domain.Entities.Auditing;
using Abp.Timing;
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace MPACore.Core.Tasks
{
    [Table("AppTasks")]
    public class Task : Entity, IHasCreationTime
    {
        public const int MaxTitleLength = 256;
        public const int MaxDescriptionLength = 64 * 1024; //64KB

        [Required]
        [StringLength(MaxTitleLength)]
        public string Title { get; set; }

        [StringLength(MaxDescriptionLength)]
        public string Description { get; set; }

        public DateTime CreationTime { get; set; }

        public TaskState State { get; set; }

        public Task()
        {
            CreationTime = Clock.Now;
            State = TaskState.Open;
        }

        public Task(string title, string description = null)
            : this()
        {
            Title = title;
            Description = description;
        }
    }

    public enum TaskState : byte
    {
        Open = 0,
        Completed = 1
    }
}

2、创建Person实体(class)

using Abp.Domain.Entities.Auditing;
using System;
using System.ComponentModel.DataAnnotations;


namespace MPACore.Core.Tasks
{
    /// 
    /// 将Id(主键)类型设置为Guid进行演示。我还派生自AuditedEntity(具有CreationTime,CreaterUserId,
    /// LastModificationTime和LastModifierUserId属性),而不是基础Entity类。
    /// 
    public class Person:AuditedEntity
    {
        public const int MaxNameLength = 32;
        [Required]
        [StringLength(MaxNameLength)]
        public string Name { get; set; }

        public Person()
        {

        }

        public Person(string name)
        {
            Name = name;
        }
    }
}

3、将人员与任务进行关联,在Task.cs中添加代码

[Table("AppTasks")]
public class Task : Entity, IHasCreationTime
{
    //...

    [ForeignKey(nameof(AssignedPersonId))]
    public Person AssignedPerson { get; set; }
    public Guid? AssignedPersonId { get; set; }

    public Task(string title, string description = null, Guid? assignedPersonId = null)
        : this()
    {
        Title = title;
        Description = description;
        AssignedPersonId = assignedPersonId;
    }
}

 

ABP框架官网例子实践(1)(ASP.NET core+Multi Page Web Application)_第1张图片

目录 

ABP框架官网例子实践(1)(ASP.NET core+Multi Page Web Application)_第2张图片

到这里core层的内容就填写完成。

第二步:在 EntityFrameworkCore 层,将实体信息映射到数据库中

ABP框架官网例子实践(1)(ASP.NET core+Multi Page Web Application)_第3张图片

1、在MPACoreDbContext.cs中添加任务

ABP框架官网例子实践(1)(ASP.NET core+Multi Page Web Application)_第4张图片

2、创建数据迁移文件

 将MPACore.EntityFrameworkCore作为启动项目,在程序包管理器控制台中输入Add-Migration initialfirst 回车 (initialfirst 的命名是任意的,什么名字都可以,但是每次迁移时名字应该不同)更新需要迁移的信息,迁移结束会自动打开迁移文件。下图是文件所在位置

ABP框架官网例子实践(1)(ASP.NET core+Multi Page Web Application)_第5张图片

3、创建数据库

更新完成之后,继续输入update-database回车,这样就将可以把数据表更新到数据中了。打开数据库就能看到数据表已经创建好了。下图是数据库中的信息

ABP框架官网例子实践(1)(ASP.NET core+Multi Page Web Application)_第6张图片

数据库连接字符串在appsettings.json中,如果要更换数据库就在这里进行配置

ABP框架官网例子实践(1)(ASP.NET core+Multi Page Web Application)_第7张图片

 

ABP框架官网例子实践(2)(ASP.NET core+Multi Page Web Application)

 

 

你可能感兴趣的:(ABP框架官网例子实践(1)(ASP.NET core+Multi Page Web Application))