标签:set 多表 框架 get MyTest System ABP using public
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
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。