簡體   English   中英

Entity Framework Core 2.0中每種類型的表

[英]Table per Type in Entity Framework Core 2.0

這些是我的模型:

public class Company
{
   public int CompanyId { get; set; }
   public string Name { get; set; }
   public string Address { get; set; }
   public string Email { get; set; }
   public string Url { get; set; }
   //...
}

public class HeadOffice : Company
{
   public int HeadOfficeId { get; set; }
   public virtual List<BranchOffice> BranchOffice { get; set; } = new List<BranchOffice>();
}

public class BranchOffice : Company
{
   public int BranchOfficeId { get; set; }
   public virtual HeadOffice HeadOffice { get; set; }
}

我想要以下數據庫結構:

餐桌公司

  • 公司編號(PK)
  • 名稱
  • 地址
  • 電子郵件
  • 網址

表總公司

  • HeadOfficeId(PK)
  • 公司編號(FK)

表分公司

  • BranchOfficeId(PK)
  • 總部編號(FK)
  • 公司編號(FK)

我怎樣才能做到這一點?

創建遷移時,EF僅創建一個表,其中包含所有列! 我不要這種方法!

您必須將模型更改為如下所示,請注意,您不能使用這種方法使用繼承:

public class Company
{
   public int CompanyId { get; set; }
   //...
}

public class Company
{
   public int CompanyId { get; set; }
   public string Name { get; set; }
   //...
}

public class HeadOffice
{
   [ForeignKey(nameof(Company))]
   public int CompanyId { get; set; }
   public Company Company { get; set; }
   // Add Properties here
}

public class BranchOffice
{
   [ForeignKey(nameof(Company))]
   public int CompanyId { get; set; }
   public Company Company { get; set; }
   // Add Properties here
}

您的DbContext

public class YourContext : DbContext
{
  public DbSet<Company> Companys { get; set; }
  public DbSet<HeadOffice> HeadOffices { get; set; }
  public DbSet<BranchOffice> BranchOffices { get; set; }

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

然后,您可以使用EF Core Migrations 該命令如下所示:

dotnet ef migrations add Initial_TPT_Migration -p ./../../ModelProject.csproj -s ./../../ModelProject.csproj -c YourContext -o ./TptModel/CodeFirst/Migrations

它generats一類Initial_TPT_Migration包含的方法來生成數據庫。

用法

要進行查詢,您需要將“公司屬性”映射到字段名稱。 如果將此與“ 存儲庫模式”鏈接 )結合使用,它實際上可能與EF Core當前使用的默認方法一樣方便。

YourContext ctx = ...

// Fetch all BranchOffices
var branchOffices = ctx.BranchOffices
          .Select(c => new BranchOffice()
                  {
                    CompanyId = c.CompanyId,
                    Name = c.Company.Name,
                  })
          .ToList();

您可以在此處找到有關此方法的更多信息。

您可以在這里找到答案https://docs.microsoft.com/zh-cn/aspnet/core/data/ef-mvc/inheritance?view=aspnetcore-2.1

如果您需要針對一個表的許多繼承的類,也請查看本主題。https://docs.microsoft.com/zh-cn/ef/core/modeling/relational/inheritance

復制代碼到這里,以防萬一微軟曾經弄亂URL和文檔

  1. 每個表的每個繼承類型
public class SchoolContext : DbContext
{
    public SchoolContext(DbContextOptions<SchoolContext> options) : base(options)
    {
    }

    public DbSet<Student> Students { get; set; }
    public DbSet<Instructor> Instructors { get; set; }
    public DbSet<Person> People { get; set; }

    protected override void OnModelCreating(ModelBuilder b)
    {
        b.Entity<Student>().ToTable("Student");
        b.Entity<Instructor>().ToTable("Instructor");
        b.Entity<Person>().ToTable("Person");
    }
}

public abstract class Person
{
    public int ID { get; set; }

    public string LastName { get; set; }
    public string FirstMidName { get; set; }
}

public class Instructor : Person
{
    public DateTime HireDate { get; set; }
}

public class Student : Person
{
    public DateTime EnrollmentDate { get; set; }
}
  1. 一個表中有許多繼承的類型
public class MyContext : DbContext
{
    public DbSet<Blog> Blogs { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Blog>()
            .HasDiscriminator<string>("blog_type")
            .HasValue<Blog>("blog_base")
            .HasValue<RssBlog>("blog_rss");
    }
}

public class Blog
{
    public int BlogId { get; set; }
    public string Url { get; set; }
}

public class RssBlog : Blog
{
    public string RssUrl { get; set; }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM