Two models pointing to the same table .NET Core - entity-framework

New to .NET core here. Trying to create a model structure to accommodate the following:
A single table, jobs, with a jsonb column in postgres
Multiple types of jobs, each of which will have a certain 'type' column string
Each job type will have it's own validation on the json column
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.

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

Related

ForeignKey Attribute in database first application

Following the ForeignKey docs, and multiple examples online I was under the influence that if I give my property (foreign key) this attribute, it would get replaced in a Html.Display call by the first textual property of the parent table.
This doesn't happen and all I get is the same foreign key field.
Does this work in db first applications, and if so, how do I make it work (using ForeignKey)?
Thanks.
EDIT: Or is this Scaffolding exclusive behaviour?
UPDATE: Example code:
// Entity model in Case.cs
public partial class Case
{
public int ID {get; set;}
public string Description {get; set;}
public int Classification_ID {get; set;}
public virtual Classification Classification {get; set;}
}
// Entity model in Classification.cs
// It's a lookup table
public partial class Classification
{
public int ID {get; set;}
public string Label {get; set;}
}
// File with partials
[MetadataType(typeof(CaseMetadata))]
public partial class {}
public class CaseMetadata
{
[ForeignKey("Classification")]
public int Classification_ID {get; set;}
}

Automapper with reciprocal navigation properties

need a bit of help here.
I have a couple of classes that I am trying to map using Automapper. I am using EF core.
The basic domain is like this:
Public class A
{
public string Id {get; set;}
public string Name {get; set;}
public virtual Icollection<AB> AB {get; set;}
}
Public class B
{
public string Id {get; set;}
public string Name {get; set;}
public virtual ICollection<AB> AB {get; set;}
}
Public class AB
{
public string A_Id {get; set;}
public string B_Id {get; set;}
public virtual A A {get; set;}
}
My DTOs are like this:
Public class A_DTO
{
public string Id {get; set;}
public string Name {get; set;}
public ICollection<B> Bs {get; set;}
}
Public class B_DTO
{
public string Id {get; set;}
public string Name {get; set;}
public ICollection<A> As {get; set;}
}
Now where I get stuck is:
How to set up the mapping so that Automapper automatically retrieves the list of children (e.g the relevant 'Bs' for the current 'A')
How to configure my DTOs so that, for example, the retrieved 'Bs' for an 'A' do not expose the 'A's navigation property to prevent infinite recursion.
Thank you!
Partial answer here. I was researching and stumbled upon https://www.exceptionnotfound.net/entity-framework-and-wcf-loading-related-entities-with-automapper-and-reflection/
So I changed my DTOs by removing the navigation properties when the DTO is not the principal.
Public class A_DTO
{
public string Id {get; set;}
public string Name {get; set;}
}
Public class A_Nav_DTO: A_DTO
{
public ICollection<B> Bs {get; set;}
}
and in my mappings I did
CreateMap<A, A_DTO>();
CreateMap<A, A_Nav_DTO>()
.ForMember(dto => dto.B, map =>
map.MapFrom(model =>
model.AB.Select(ab => ab.B).ToList()));
Now this works, but obviously now I have to map three classes instead of two. Any suggestions on how to improve this solution?
I know it's an old question but hopefully this helps someone.
You could call Automapper's ResolveUsing method like so :
cfg.CreateMap<A, A_DTO>()
.ForMember(x => x.Bs,
opt => opt.ResolveUsing(src => RemoveInclusions(src)));
And in RemoveInclusions method you can manually set B's navigation of A's to null like :
private B RemoveInclusions(A src)
{
src.Bs.A = null;
return src.Bs;
}

Bidirectional links to the same class in Entity Framework (code first)

I would like to create bidirectional links to the same class. Id like for the relationship class to have the attributes that would explain how the two classes are related. It may be a parent-child relationship or it be a simple "reference" relationship.
Currently, if I use the setup below, Entity Framework will automatically create a 3rd foreign key in the link table for the "myChildNodes" relationship. The only way I can get Entity Framework to understand what I am trying to do on the link class is to create two collections I.E. (childOf and ParentOf).
I would like to dynamically add relationship types and not need to create a collection representing that relationship type. I would rather handle that in the repository for the node object.
Node
{
Public int id {get; set;}
Public datetime createDate {get; set;}
Public bool isModified {get; set;}
//I would like just one collection for all links from this node as the source node
Public virtual ICollection<Link> myChildNodes{get; set;}
//I don't want to use something like this that explicitly defines the relationship
//Public virtual ICollection<Node> parentOf{get; set;}
//Public virtual ICollection<Node> childOf{get; set;}
Public Node() {
}
}
Link {
Public int id {get; set;}
Public datetime createdDate {get; set;}
Public string linkType {get; set;}
[ForeignKey("SourceNode")]
Public int? SourceNodeId { get; set;}
Public Node SourceNode {get; set;}
[ForeignKey("TargetNode")]
Public int? TargetNodeId { get; set;}
Public Node TargetNode {get; set;}
Public Link() {
}
}
Has anyone had success with this design before?

auto projection in entity framework

Is there anyway to create a auto projection in entity framework? see please:
public class Person{
public int Id {get; set;}
public string FirstName {get; set;}
public string LastName {get; set;}
public string FatherName {get; set;}
public string City {get; set;}
public string AddressLine {get; set;}
public string Something {get; set;}
}
public class PersonNameModel{
public string FirstName {get; set;}
public string LastName {get; set;}
public string FatherName {get; set;}
}
public class PersonAddressModel{
public string City {get; set;}
public string AddressLine {get; set;}
}
// etc...
I mean I be able to replace normal projection like:
context.Persons.Select(t => new PersonNameModel{ FirstName = t.FirstName /* etc */ });
whit an extension method that can use reflection and create an auto projection like:
public static class MyExtensions{
public static IQueryable<T> AutoSelect<T, TProject>(this IQueryable<T> q){
// read TProject type in reflection
// create a projection as a IQueryable<T>
}
}
Is there any way? I googled it, but didn't find any resource. Can you guide me please?
Yes, it's possible to auto project entity framework entities to some Dto. See one of implementations here https://gist.github.com/1367880
You can use it as:
context.Persons.Project().To<PersonNameModel>().ToList();
In that case the db query will be generated to select only required columns (specified by PersonNameModel).
If you want just to map query results (that retrieved objects), so EmitMapper or AutoMapper should be your choice.
If I understand correctly what you want is a mapping between objects, use Automapper it would do the mapping for you
http://www.codeproject.com/Articles/61629/AutoMapper
http://automapper.org/
git hub path https://github.com/AutoMapper/AutoMapper

Entity Framework code first not working

I have a Class like this:
class ClassA
{
public long classAID {get; set;}
public string Description {get; set;}
public IEnumerable<ClassB> ClassBs {get; set;}
}
class ClassB
{
public long classBID {get; set;}
public string SomeOtherDescription {get; set;}
public IEnumerable<ClassA> {get; set;}
}
class TestContext: DBContext
{
public DbSet<ClassA> ClassAs {get; set;}
public DbSet<ClassB> ClassBs {get; set;}
}
H have the DB with same column names and table names as the classes and properties.
I have done the web.config configuration as required. When i try to use above to retrieve the data i get the error
"System.Data.Edm.EdmEntityType: : EntityType 'ClassA' has no key defined. Define the key for this EntityType."
and
"System.Data.Edm.EdmEntityType: : EntityType 'ClassB' has no key defined. Define the key for this EntityType."
I tired multiple approaches such as setting the key attribute, Foreign key attribute etc. but nothing worked. Please let me know what am i missing.
I use C# 4 and i have verified with following URLs:
http://www.asp.net/mvc/tutorials/mvc-music-store-part-4
http://weblogs.asp.net/scottgu/archive/2010/07/16/code-first-development-with-entity-framework-4.aspx
Use this:
public class ClassA
{
public long ClassAID {get; set;}
public string Description {get; set;}
public virtual ICollection<ClassB> ClassBs {get; set;}
}
public class ClassB
{
public long ClassBID {get; set;}
public string SomeOtherDescription {get; set;}
public virtual ICollection<ClassA> {get; set;}
}
public class TestContext: DBContext
{
public DbSet<ClassA> ClassAs { get; set; }
public DbSet<ClassB> ClassBs { get; set; }
}
As you can see navigation properties are marked as virtual. It will allow lazy loading = navigation property will be loaded separately first time your code access the property. It is not always the best way to load navigation properties this way because it causes additional roundtrips to the database. Because of that EF also offers eager loading where you explicitly tell EF to load your navigation property:
var query = context.ClassAs.Include(c => ClassBs);