其他分享
首页 > 其他分享> > ABP框架之建立多表关系

ABP框架之建立多表关系

作者:互联网

2. 领域层建立实体

2.1 建立 Student 实体

实体

using Abp.Domain.Entities;
using Abp.Domain.Entities.Auditing;
using MyTest.StudentCourseRelationships;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyTest.Students
{
    public class Student : Entity<long>, IFullAudited
    {
        /// <summary>
        /// 学号
        /// </summary>
        public string Number { get; set; }

        /// <summary>
        /// 姓名
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 性别
        /// </summary>
        public string Sex { get; set; }

        /// <summary>
        /// 出生日期
        /// </summary>
        public DateTime? Birthday { get; set; }

        /// <summary>
        /// 身份证号
        /// </summary>
        public string IdCardNumber { get; set; }

        /// <summary>
        /// 学生与课程的关系
        /// </summary>
        public ICollection<StudentCourseRelationship> SC { get; set; }

        public long? CreatorUserId { get; set; }
        public DateTime CreationTime { get; set; }
        public long? LastModifierUserId { get; set; }
        public DateTime? LastModificationTime { get; set; }
        public long? DeleterUserId { get; set; }
        public DateTime? DeletionTime { get; set; }
        public bool IsDeleted { get; set; }
    }
}

设置字段大小常量

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyTest.Students
{
    public class StudentConsts
    {
        public const int MaxNameLength = 16;

        public const int MaxSexLength = 4;

        public const int MaxNumberLength = 32;

        public const int MaxIdCardNumberLength = 32;

    }
}

2.2 建立 Course 实体

实体

using Abp.Domain.Entities;
using Abp.Domain.Entities.Auditing;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MyTest.StudentCourseRelationships;

namespace MyTest.Courses
{
    public class Course : Entity<long>, IFullAudited
    {

        /// <summary>
        /// 课程编号
        /// </summary>
        public string Number { get; set; }

        /// <summary>
        /// 课程名称
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 课程学分
        /// </summary>
        public int Credit { get; set; }

        /// <summary>
        /// 备注
        /// </summary>
        public string remark { get; set; }

        /// <summary>
        /// 课程与学生的关系
        /// </summary>
        public ICollection<StudentCourseRelationship> SC { get; set; }


        public long? CreatorUserId { get; set; }
        public DateTime CreationTime { get; set; }
        public long? LastModifierUserId { get; set; }
        public DateTime? LastModificationTime { get; set; }
        public long? DeleterUserId { get; set; }
        public DateTime? DeletionTime { get; set; }
        public bool IsDeleted { get; set; }
    }
}

设置字段大小常量

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyTest.Courses
{
    public class CourseConsts
    {
        public const int MaxNumberLength = 16;

        public const int MaxNameLength = 32;

        public const int MaxRemarkLength = 200;

        public const int MaxCreditLength = 8;
    }
}

2.3 建立学生与课程关系表 StudentCourseRelationship

using Abp.Domain.Entities;
using Abp.Domain.Entities.Auditing;
using MyTest.Courses;
using MyTest.Students;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyTest.StudentCourseRelationships
{
    public class StudentCourseRelationship : Entity<long>, ICreationAudited, IDeletionAudited, ISoftDelete
    {
        /// <summary>
        /// 学生学号
        /// </summary>
        public long StudentId { get; set; }

        /// <summary>
        /// 课程号
        /// </summary>
        public long CourseId { get; set; }

        /// <summary>
        /// 课程成绩
        /// </summary>
        public int? Grade { get; set; }


        public Student Student { get; set; }

        public Course Course { get; set; }


        public bool IsDeleted { get; set; }
        public long? CreatorUserId { get; set; }
        public DateTime CreationTime { get; set; }
        long? IDeletionAudited.DeleterUserId { get; set; }
        DateTime? IHasDeletionTime.DeletionTime { get; set; }
    }
}

3. 基础设施层更新数据库

using Microsoft.EntityFrameworkCore;
using Abp.Zero.EntityFrameworkCore;
using MyTest.Authorization.Roles;
using MyTest.Authorization.Users;
using MyTest.MultiTenancy;
using MyTest.Students;
using MyTest.Courses;
using MyTest.StudentCourseRelationships;

namespace MyTest.EntityFrameworkCore
{
    public class MyTestDbContext : AbpZeroDbContext<Tenant, Role, User, MyTestDbContext>
    {
        /* Define a DbSet for each entity of the application */
        
        public DbSet<Student> Students { get; set; }

        public DbSet<Course> Courses { get; set; }

        public DbSet<StudentCourseRelationship> SC { get; set; }


        public MyTestDbContext(DbContextOptions<MyTestDbContext> options)
            : base(options)
        {
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            modelBuilder.Entity<Student>(b =>
            {
                b.ToTable("Students");

                b.Property(x => x.Name).IsRequired().HasMaxLength(StudentConsts.MaxNameLength);
                b.Property(x => x.Sex).IsRequired().HasMaxLength(StudentConsts.MaxSexLength);
                b.Property(x => x.Number).IsRequired().HasMaxLength(StudentConsts.MaxNumberLength);
                b.Property(x => x.IdCardNumber).HasMaxLength(StudentConsts.MaxIdCardNumberLength);

                b.HasIndex(x => x.Number);
                b.HasIndex(x => x.Name);
                b.HasIndex(x => x.IdCardNumber);
            });

            modelBuilder.Entity<Course>(b =>
            {
                b.ToTable("Courses");

                b.Property(x => x.Name).IsRequired().HasMaxLength(CourseConsts.MaxNameLength);
                b.Property(x => x.Number).IsRequired().HasMaxLength(CourseConsts.MaxNumberLength);
                b.Property(x => x.remark).HasMaxLength(CourseConsts.MaxRemarkLength);
                b.Property(x => x.Credit).IsRequired().HasMaxLength(CourseConsts.MaxCreditLength);

       });
	 modelBuilder.Entity<StudentCourseRelationship>(b => 
	{
	   b.ToTable("SC");

	   /// <summary>
	  /// 设置字段 `StudentId` 和 `CourseId` 为外键
	  /// </summary>
	   b.HasOne(x => x.Student).WithMany(x => x.SC).ForeignKey(x => x.StudentId);
 	   b.HasOne(x => x.Course).WithMany(x => x.SC).ForeignKey(x => x.CourseId);
	})

}

在程序包管理控制台依次执行下面命令

add-migration 'add_student_course_sc_table'
update-database

到此一个多对多的关系就建立好了,可以在数据库查看外键的设立是否正确。

标签:set,多表,框架,get,MyTest,System,ABP,using,public
来源: https://www.cnblogs.com/jerry-1015/p/16539732.html