Reputation: 5896
New to .NET core here. Trying to create a model structure to accommodate the following:
Right now, I have two models. JobsServer.Models.Job and JobsServer.Models.SubJobCategory.SpecificJob. The look like this:
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;}
}
}
I then have a job context that looks like this:
using Microsoft.EntityFrameworkCore;
using 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;}
}
}
This gives me the error:
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.
I'm probably going about this the wrong way. I'm coming from a ruby on rails and primarily elixir background, where I can accomplish what I'm trying to do here.
Essentially, if a user submits a post for creating a SpecificJob, I plan to set type to 'specific_job' and then validate the data json to match a certain format. There will be multiple types of jobs, of course, but the above is a simplified version to test the architecture.
Any assistance or ideas appreciated.
Upvotes: 0
Views: 2327
Reputation: 35018
The EF pattern you are likely looking for is Table-per-Hierarchy, where you use inheritance to differentiate types of jobs, represented in a single job table with a discriminator. ("Type")
You should find all you need here: https://weblogs.asp.net/manavi/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-1-table-per-hierarchy-tph
Upvotes: 1