Entity framework核心一对一
Entity framework core One to One
The databse
(Project,Contest,LocalJob)->每个都有1个WorkDetails,1个WorkDetails有其中之一
你好,这是我的数据库,我想问你以下问题:
我应该在 WorkDetails 中为项目、竞赛或 LocalJobs 添加外键列吗?
如果我必须如何实现导航属性?
在代码优先中,您可以为此使用自有类型:
using Microsoft.EntityFrameworkCore;
using System.Linq;
using System;
using System.ComponentModel.DataAnnotations.Schema;
using System.Collections.Generic;
namespace efCoreTest
{
class Project
{
public int Id { get; set; }
public WorkDetail WorkDetail { get; set; }
}
class Contest
{
public int Id { get; set; }
public WorkDetail WorkDetail { get; set; }
}
class LocalJob
{
public int Id { get; set; }
public WorkDetail WorkDetail { get; set; }
}
class WorkDetail
{
public int Id { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
}
class Db : DbContext
{
public DbSet<Project> Projects { get; set; }
public DbSet<Contest> Contests { get; set; }
public DbSet<LocalJob> LocalJobs { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("Server=.;Database=efCoreTest;Integrated Security=true");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Project>().OwnsOne(p => p.WorkDetail);
modelBuilder.Entity<Contest>().OwnsOne(p => p.WorkDetail);
modelBuilder.Entity<LocalJob>().OwnsOne(p => p.WorkDetail);
base.OnModelCreating(modelBuilder);
}
}
class Program
{
static void Main(string[] args)
{
using (var db = new Db())
{
db.Database.EnsureDeleted();
db.Database.EnsureCreated();
}
}
}
}
The databse
(Project,Contest,LocalJob)->每个都有1个WorkDetails,1个WorkDetails有其中之一
你好,这是我的数据库,我想问你以下问题:
我应该在 WorkDetails 中为项目、竞赛或 LocalJobs 添加外键列吗?
如果我必须如何实现导航属性?
在代码优先中,您可以为此使用自有类型:
using Microsoft.EntityFrameworkCore;
using System.Linq;
using System;
using System.ComponentModel.DataAnnotations.Schema;
using System.Collections.Generic;
namespace efCoreTest
{
class Project
{
public int Id { get; set; }
public WorkDetail WorkDetail { get; set; }
}
class Contest
{
public int Id { get; set; }
public WorkDetail WorkDetail { get; set; }
}
class LocalJob
{
public int Id { get; set; }
public WorkDetail WorkDetail { get; set; }
}
class WorkDetail
{
public int Id { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
}
class Db : DbContext
{
public DbSet<Project> Projects { get; set; }
public DbSet<Contest> Contests { get; set; }
public DbSet<LocalJob> LocalJobs { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("Server=.;Database=efCoreTest;Integrated Security=true");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Project>().OwnsOne(p => p.WorkDetail);
modelBuilder.Entity<Contest>().OwnsOne(p => p.WorkDetail);
modelBuilder.Entity<LocalJob>().OwnsOne(p => p.WorkDetail);
base.OnModelCreating(modelBuilder);
}
}
class Program
{
static void Main(string[] args)
{
using (var db = new Db())
{
db.Database.EnsureDeleted();
db.Database.EnsureCreated();
}
}
}
}