.NET核心的新功能。尝试创建一个模型结构来容纳以下内容:
现在,我有两种型号。 JobsServer.Models.Job和JobsServer.Models.SubJobCategory.SpecificJob。看起来像这样:
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace JobsServer.Models
{
[Table("jobs")]
public class Job
{
[Column("id"), Key, Required, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id {get; set;}
[Column("type"), Required]
public string Type {get; set;}
[Column("domain"), Required]
public string Domain {get; set;}
[Column("status"), Required]
public string Status {get; set;}
[Column("data", TypeName = "jsonb")]
public string Data {get; set;}
[Column("rescheduled_from_id")]
public int RescheduledFromId {get; set;}
[ForeignKey("RescheduledFromId")]
public Job RescheduledFrom {get; set;}
[Column("parent_job_id")]
public int ParentJobId {get; set;}
[ForeignKey("ParentJobId")]
public Job ParentJob {get; set;}
}
}
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using JobsServer.Models;
namespace JobsServer.Models.SubJobCategory
{
[Table("jobs")]
public class SpecificJob
{
[Column("id"), Key, Required, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id {get; set;}
[Column("type"), Required]
public string Type {get; set;}
[Column("domain"), Required]
public string Domain {get; set;}
[Column("status"), Required]
public string Status {get; set;}
[Column("data", TypeName = "jsonb")]
public string Data {get; set;}
[Column("rescheduled_from_id")]
public int RescheduledFromId {get; set;}
[ForeignKey("RescheduledFromId")]
public Job RescheduledFrom {get; set;}
[Column("parent_job_id")]
public int ParentJobId {get; set;}
[ForeignKey("ParentJobId")]
public Job ParentJob {get; set;}
}
}
然后我的工作环境如下:
使用Microsoft.EntityFrameworkCore;
使用JobsServer.Models;
namespace JobsServer.Contexts
{
public class JobContext : DbContext
{
public JobContext(DbContextOptions<JobContext> options) : base(options)
{}
public DbSet<Job> Jobs {get; set;}
public DbSet<JobsServer.Models.SubJobCategory.SpecificJob> SpecificJob {get; set;}
}
}
这给了我错误:
Cannot use table 'jobs' for entity type 'Job' since it is being used for entity type 'SpecificJob' and there is no relationship between their primary keys.
我可能会以错误的方式进行操作。我来自红宝石,主要是长生不老药,在这里我可以完成我想做的事情。
基本上,如果用户提交了一个用于创建SpecificJob的帖子,我计划将type设置为“ specific_job”,然后验证数据json以匹配某种格式。当然,会有多种类型的作业,但是上面是测试架构的简化版本。
感谢任何帮助或想法。
答案 0 :(得分:1)
您可能要寻找的EF模式是每个层次的表,您可以在其中使用继承来区分作业的类型,在单个作业表中使用区分符表示。 (“类型”)
您应该在这里找到所需的全部: https://weblogs.asp.net/manavi/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-1-table-per-hierarchy-tph