One to One 一对一的关系

有时候一个表太多的数据,或者其它的原因我们希望把一个表拆分成两个。这个就是1对1的关系了。

public class Student 
{
    public int Id { get; set; }

    public string Name { get; set; }

    public virtual StudentExtraInfo StudentExtraInfo { get; set; }

}

public class StudentExtraInfo
{
    [Key] //因为不是Id为主键,所以我们得加上 [Key]
    public int StudentId { get; set; }

    public string Address { get; set; }

    public string City { get; set; }

    public Student Student { get; set; }
}

public class MalemaDbContext : DbContext
{
    public MalemaDbContext(DbContextOptions<MalemaDbContext> options)
        : base(options)
    {
        this.Database.EnsureCreated();
        
    }

    public DbSet<Student> Students { get; set; }

    public DbSet<StudentExtraInfo> StudentExtraInfos { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Student>().Property(x => x.Name).HasMaxLength(50);
        base.OnModelCreating(modelBuilder);
    }
}

在数据库中生成的表如下。

可以用下面的方式来添加数据。

public void InitData()
{
    using (var dbContext = this.dbContextFactory.CreateDbContext())
    {
        List<Student> students = GetStudents();
        if (dbContext.Students.Count() < 1)
        {
            dbContext.AddRange(students);
            dbContext.SaveChanges();
            dbContext.AttachRange(GetStudentExtra());
            dbContext.SaveChanges();
            //下面这种方式也行
            dbContext.Add(new Student()
            {
                Name = "Together",
                StudentExtraInfo = new StudentExtraInfo() { City = "together" }
            });

            dbContext.SaveChanges();
        }
    }
}

private static List<StudentExtraInfo> GetStudentExtra()
{
    var student1 = new StudentExtraInfo() { StudentId = 1, City = "xiamen", };
    var student2 = new StudentExtraInfo() { StudentId = 2, City = "xiamen", };

    return new List<StudentExtraInfo>() { student1, student2 };
}

private static List<Student> GetStudents()
{
    var student1 = new Student() { BirthDate = DateTime.UtcNow.AddYears(-12), Grade = 2, Name = "张三", TotalScore = 200 };
    var student2 = new Student() { BirthDate = DateTime.UtcNow.AddYears(-13), Grade = 2, Name = "李四", TotalScore = 198 };


    return new List<Student>() { student1, student2 };
}

上面的代码跑完会看到三条数据都被添加成功了

使用Fluent api进行配置

通常情况下,如果按照规范来写的话,我们是不需要手动进行关系配置的。

但是有需要的话我们也可以手动进行关系配置。

  protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Student>().Property(x => x.Name).HasMaxLength(50);

            modelBuilder.Entity<Student>()
               .HasOne<StudentExtraInfo>(s => s.StudentExtraInfo)
               .WithOne(ad => ad.Student)
               //外键的可以是其它的属性名不符合 entity+Id的格式
               .HasForeignKey<StudentExtraInfo>(ad => ad.StudentId);

            base.OnModelCreating(modelBuilder);
        }

上面的完整代码可以在分支relationship/one_to_one看到

git clone https://gitee.com/malema/Examples
git checkout relationship/one_to_one

OneToOneExample.cs

最近更新的
...