aspnet核心实体框架7自我引用“作业”。 1对多表 [英] aspnet core entity framework 7 self referencing "job" 1 to many table

查看:96
本文介绍了aspnet核心实体框架7自我引用“作业”。 1对多表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含工作的工作表。事实是,乔布斯并非总是一劳永逸的..您可以进行很多次访问的工作。我打算将其表示为另一项工作,但通过自我引用linkId链接回原始工作。



我很难使用流畅的API来表示此工作。它是一对多的关系。一个工作可能有很多访问,因此许多linkId都指向原始工作。链接ID将返回到原始作业ID。它也是可选的,因为大多数工作可能会一口气完成。.



我一直在寻找,但是很难将其他示例转换为该示例,因为它们很多一对一并进一步赋予

我的工作表是:

rel = nofollow noreferrer>示例一对多的示例似乎是使用EF6做到的。 >

 使用系统; 

命名空间JobsLedger.Model.Entities
{
public class Job:IEntityBase
{
public int Id {get;组; }
公共字符串Model {get;组; }
公用字串Serial {get;组; }
公共字符串ProblemDetails {get;组; }
公共字符串SolutionDetails {get;组; }
public DateTime JobDate {get;组; }
public int BrandId {get;组; }
public int JobTypeId {get;组; }
public int StatusId {get;组; }
public int ClientId {get;组; }
public int UserId {get;组; }

public int? LinkId {get;组; } //如果有多个职位标注,例如返回适合的零件。
public Job MultipleJobVisits {get;组; }
}
}

我很确定我错了:

  //作业
modelBuilder.Entity< Job>()。Property(j => j.Model)。 HasMaxLength(100);
modelBuilder.Entity< Job>()。Property(j => j.Serial).IsRequired();
modelBuilder.Entity< Job>()。Property(j => j.ProblemDetails).HasMaxLength(100);
modelBuilder.Entity< Job>()。Property(j => j.SolutionDetails).HasMaxLength(500);
modelBuilder.Entity< Job>()。Property(j => j.JobDate);
modelBuilder.Entity< Job>()。Property(j => j.Notes).HasMaxLength(1000);
modelBuilder.Entity< Job>()。Property(j => j.BrandId);
modelBuilder.Entity< Job>()。Property(j => j.JobTypeId);
modelBuilder.Entity< Job>()。Property(j => j.StatusId);
modelBuilder.Entity< Job>()。Property(j => j.ClientId);
modelBuilder.Entity< Job>()。Property(j => j.UserId);
modelBuilder.Entity< Job>()。HasOne(x => x.Id)
.WithMany(x => x.LinkId)
.ForeignKey(x => x .Id)
.Required(false);

我如何在EF7和fluent API中表示一对多自引用,哪个是可选的? / p>

编辑:虽然这不提供语法错误,但我不确定我是否确定

  modelBuilder.Entity< Job>()。HasMany(j => j.LinkedJobs).WithOne()。IsRequired(false); 

在此方面的任何帮助都值得赞赏...我对如何获得知识的了解很少配置一对多的自引用关系...

解决方案

工作类别:

  public class Job 
{
public int Id {get;组; }

public int? JobId {get;组; }

公共工作ParentJob {get;组; }

公共ICollection< Job> ChildJobs {get;组; }
}

Fluent API:

  modelBuilder.Entity< Job>()
.HasMany(oj => oj.ChildJobs)
.WithOne(j => j.ParentJob)
.HasForeignKey(j => j.JobId);


I have a "Job" table that contains jobs. The fact is Jobs are not always done in one go.. you can have a job that has many visits. I intended to represent that as another job but linked back to the original job via self referencing linkId.

I am having trouble representing this using the fluent API. Its a one to many relationship.. one job might have many visits and thus a number of linkId's point back to the orginal job. The link Id would back to the orginal job Id. Its also optional since most jobs might be completed in one go..

I have looked for this but its difficult to turn the other examples to this example as alot of them are one to one and further the ones that give examples of one to many seem to do so using EF6 which is different.

My job table is:

    using System;

namespace JobsLedger.Model.Entities
{
    public class Job : IEntityBase
    {
        public int Id { get; set; }
        public string Model { get; set; }
        public string Serial { get; set; }
        public string ProblemDetails { get; set; }
        public string SolutionDetails { get; set; }
        public DateTime JobDate { get; set; }
        public int BrandId { get; set; }
        public int JobTypeId { get; set; }
        public int StatusId { get; set; }
        public int ClientId { get; set; }
        public int UserId { get; set; }

        public int? LinkId { get; set; }  //If there are more than one job callout eg back to fit parts.
        public Job MultipleJobVisits { get; set; }
    }
}

I am pretty sure I have this wrong:

        // Job
        modelBuilder.Entity<Job>().Property(j => j.Model).HasMaxLength(100);
        modelBuilder.Entity<Job>().Property(j => j.Serial).IsRequired();
        modelBuilder.Entity<Job>().Property(j => j.ProblemDetails).HasMaxLength(100);
        modelBuilder.Entity<Job>().Property(j => j.SolutionDetails).HasMaxLength(500);
        modelBuilder.Entity<Job>().Property(j => j.JobDate);
        modelBuilder.Entity<Job>().Property(j => j.Notes).HasMaxLength(1000);
        modelBuilder.Entity<Job>().Property(j => j.BrandId);
        modelBuilder.Entity<Job>().Property(j => j.JobTypeId);
        modelBuilder.Entity<Job>().Property(j => j.StatusId);
        modelBuilder.Entity<Job>().Property(j => j.ClientId);
        modelBuilder.Entity<Job>().Property(j => j.UserId);
        modelBuilder.Entity<Job>().HasOne(x => x.Id)
                                  .WithMany(x => x.LinkId)
                                  .ForeignKey(x => x.Id)
                                  .Required(false);

How do I represent a one to many self reference in EF7 and fluent API and which is optional?

EDIT: whilst this doesnt provide a syntax error I have to say I am not sure if its OK

modelBuilder.Entity<Job>().HasMany(j => j.LinkedJobs).WithOne().IsRequired(false);

Any help on this is appreciated... I have found precious little in the way of knowledge on how to configure a one to many self referencing relationship...

解决方案

Job class:

public class Job
{
    public int Id { get; set; }

    public int? JobId { get; set; }

    public Job ParentJob { get; set; }

    public ICollection<Job> ChildJobs { get; set; }
}

Fluent API:

modelBuilder.Entity<Job>()
                .HasMany(oj => oj.ChildJobs)
                .WithOne(j => j.ParentJob)
                .HasForeignKey(j => j.JobId);

这篇关于aspnet核心实体框架7自我引用“作业”。 1对多表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆