.NET Entity Framework 核心一对多关系
.NET Entity Framework Core one-many relationship
当我尝试使用 EF 创建 table 时,我看到 User table 中 FK 的名称与我的预期不同,并且 'allow null'。我打算在 'User' table 下创建 FK 'orgId'。我参考了website这个。对于示例,它为 'Student' table 下面的 'Grade' 创建了一个外键。你能告诉我我在这里错过了什么吗? (*我使用了 EntityFrameworkCore@3.1.0)
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace Domain
{
public class User
{
[Key]
public int userId { get; set; }
[Required]
public string userEmail { get; set; }
public string previousPW { get; set; }
public string userHashedPassword { get; set; }
public DateTime userResetPWDate { get; set; }
public string userResetCode { get; set; }
public string userFullName { get; set; }
public string userOrgName { get; set; }
public string userJobTitle { get; set; }
public string userFName { get; set; }
public string userLName { get; set; }
public string userPhone { get; set; }
public string userFax { get; set; }
public string userLevel { get; set; }
public DateTime userCreatedDate { get; set; }
public DateTime userAccessedDate { get; set; }
public string userComment { get; set; }
public int orgId { get; set; }
public virtual Organization Organization { get; set; }
public virtual ICollection<PermissionPkgView> PermissionPkgView { get; set; }
}
}
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace Domain
{
public class Organization
{
[Key]
public int orgId { get; set; }
public string orgName { get; set; }
public virtual ICollection<User> User { get; set; }
}
}
作为一些评论,我尽量照着做,和参考一样。
namespace Domain
{
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public Grade Grade { get; set; }
}
}
----
using System.Collections.Generic;
namespace Domain
{
public class Grade
{
public int GradeID { get; set; }
public string GradeName { get; set; }
public string Section { get; set; }
public ICollection<Student> Student { get; set; }
}
}
然而,结果与样本不同。它创建了 'FK_Student_Grade_GradeID' 并允许 null。
最后我直接用'Fluent API',就正常显示FK了。
using Microsoft.EntityFrameworkCore;
using Domain;
namespace Persistence
{
public class DataContext : DbContext
{
public DataContext(DbContextOptions options) : base(options)
{
}
public DbSet<tbl_PW> tbl_PW { get; set; }
public DbSet<User> User { get; set; }
public DbSet<Organization> Organization { get; set; }
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<User>()
.HasOne<Organization>(u => u.Organization)
.WithMany(o => o.User)
.HasForeignKey(u => u.orgId);
}
}
}
当我尝试使用 EF 创建 table 时,我看到 User table 中 FK 的名称与我的预期不同,并且 'allow null'。我打算在 'User' table 下创建 FK 'orgId'。我参考了website这个。对于示例,它为 'Student' table 下面的 'Grade' 创建了一个外键。你能告诉我我在这里错过了什么吗? (*我使用了 EntityFrameworkCore@3.1.0)
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace Domain
{
public class User
{
[Key]
public int userId { get; set; }
[Required]
public string userEmail { get; set; }
public string previousPW { get; set; }
public string userHashedPassword { get; set; }
public DateTime userResetPWDate { get; set; }
public string userResetCode { get; set; }
public string userFullName { get; set; }
public string userOrgName { get; set; }
public string userJobTitle { get; set; }
public string userFName { get; set; }
public string userLName { get; set; }
public string userPhone { get; set; }
public string userFax { get; set; }
public string userLevel { get; set; }
public DateTime userCreatedDate { get; set; }
public DateTime userAccessedDate { get; set; }
public string userComment { get; set; }
public int orgId { get; set; }
public virtual Organization Organization { get; set; }
public virtual ICollection<PermissionPkgView> PermissionPkgView { get; set; }
}
}
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace Domain
{
public class Organization
{
[Key]
public int orgId { get; set; }
public string orgName { get; set; }
public virtual ICollection<User> User { get; set; }
}
}
作为一些评论,我尽量照着做,和参考一样。
namespace Domain
{
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public Grade Grade { get; set; }
}
}
----
using System.Collections.Generic;
namespace Domain
{
public class Grade
{
public int GradeID { get; set; }
public string GradeName { get; set; }
public string Section { get; set; }
public ICollection<Student> Student { get; set; }
}
}
然而,结果与样本不同。它创建了 'FK_Student_Grade_GradeID' 并允许 null。
最后我直接用'Fluent API',就正常显示FK了。
using Microsoft.EntityFrameworkCore;
using Domain;
namespace Persistence
{
public class DataContext : DbContext
{
public DataContext(DbContextOptions options) : base(options)
{
}
public DbSet<tbl_PW> tbl_PW { get; set; }
public DbSet<User> User { get; set; }
public DbSet<Organization> Organization { get; set; }
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<User>()
.HasOne<Organization>(u => u.Organization)
.WithMany(o => o.User)
.HasForeignKey(u => u.orgId);
}
}
}