0

我有实体程序有两个外键到我的主题表(MainContactSubjectId,SecondaryContactSubjectId)。主要和次要都是可空的多头。出于某种原因,当我尝试插入实体程序错误(内部服务器错误),并且不允许我插入,除非主要和次要存在。下面是我的实体程序和我的一些dbContext。任何人都可以看到我做错了什么?代码优先 - 非必填字段是必需的

[Table("Program")] 
    public class Program : Entity<long> 
    { 

     [Required] 
     public int TenantId { get; set; } 

     [Required] 
     public long ProgramTypeId { get; set; } 

     [Required] 
     [MaxLength(4000)] 
     public string ProgramName { get; set; } 

     public long? MainContactSubjectId { get; set; } 

     public long? SecondaryContactSubjectId { get; set; } 

     public virtual ICollection<AppTables.Case_ProgramRequirements.Case_ProgramRequirement> Case_ProgramRequirement { get; set; } 
     public virtual AppTables.ProgramTypes.ProgramType ProgramType { get; set; } 
     public virtual AppTables.Subjects.Subject MainSubject { get; set; } 
     public virtual AppTables.Subjects.Subject SecondarySubject { get; set; } 
    } 

我猜这个问题在这里,但我不知道它是什么。我最好的 猜测是.HasRequired,但我不知道如何重写它。 没有这个代码,外键没有得到正确创建 和循环参考问题。 WillCascadeOnDelete(false)停止 循环引用问题。

public virtual IDbSet<AppTables.Programs.Program> Programs { get; set; } 


protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
      modelBuilder.Entity<AppTables.Programs.Program>() 
        .HasRequired(m => m.MainSubject) 
        .WithMany(t => t.ProgramsMain) 
        .HasForeignKey(m => m.MainContactSubjectId) 
        .WillCascadeOnDelete(false); 

      modelBuilder.Entity<AppTables.Programs.Program>() 
        .HasRequired(m => m.SecondarySubject) 
        .WithMany(t => t.ProgramsSecondary) 
        .HasForeignKey(m => m.SecondaryContactSubjectId) 
        .WillCascadeOnDelete(false); 
    } 

编辑1: 我相当有信心,我的问题是数据访问层,但如果没有,我直接调用Ajax到我的应用服务层“创建”。当我踏入其中时,一切看起来都很完美。它命中返回,然后我得到内部服务器错误。没有其他细节。我尝试在try/catch中进行包装,并且捕获从未被击中。

我正在使用Asp.net Boilerplate框架。我也发布了他们的私人论坛,但没有运气。我只是假设这是一个数据访问层问题,这就是我在这里发布的原因。

public async Task<> Create(ProgramInput model) 
    { 
      Program domainModel = new Program(); 
      domainModel.TenantId = (int)AbpSession.TenantId; 
      domainModel.ProgramName = model.ProgramName; 
      domainModel.ProgramTypeId = model.ProgramTypeId; 
      domainModel.MainContactSubjectId = model.MainContactId; 
      domainModel.SecondaryContactSubjectId = model.SecondaryContactId; 
      domainModel.CreatedBy = (long)AbpSession.UserId.Value; 
      domainModel.CreatedDate = Clock.Now; 
      domainModel.IsDeleted = false; 

      await _programRepository.InsertAsync(domainModel); 

      return; 
    } 

编辑2: 这是我的主题表。它有50个列和10个外键,但我会缩短相关数据。

public class Subject : Entity<long> 
{ 
    public Subject() 
    { 
     this.ProgramsMain = new HashSet<AppTables.Programs.Program>(); 
     this.ProgramsSecondary = new HashSet<AppTables.Programs.Program>(); 
    } 

    [Required] 
    public int TenantId { get; set; } 

    [Required] 
    public long SubjectTypeId { get; set; } 

    [MaxLength(1000)] 
    public string FirstName { get; set; } 

    [MaxLength(1000)] 
    public string MiddleName { get; set; } 

    [MaxLength(1000)] 
    public string LastName { get; set; } 

    [MaxLength(100)] 
    public string Suffix { get; set; } 

    [MaxLength(3000)] 
    public string FullName { get; set; } 
//A TON MORE COLUMNS .... 




    public virtual AppTables.SubjectTypes.SubjectType SubjectType { get; set; } 
    public virtual ICollection<AppTables.Programs.Program> ProgramsMain { get; set; } 
    public virtual ICollection<AppTables.Programs.Program> ProgramsSecondary { get; set; } 

ANSWER 我想通了,我的答案。我换了.HasRequired to .HasOptional,现在一切正常。抱歉浪费了大家的时间。谢谢!

+0

您确定错误在数据访问层吗?你的控制器呢?请指定更多关于指导数据插入的渠道的信息。 – Emad

+0

ProgramsMain和ProgramsSecondary是什么样的? – Bit

+1

我想出了我的答案。我换了.HasRequired to .HasOptional,现在一切正常。抱歉浪费了大家的时间。谢谢! – Ben

回答

0

从他的问题编辑和注释的OP的回答是:

变化.HasRequired到.HasOptional按下面的代码。

public virtual IDbSet<AppTables.Programs.Program> Programs { get; set; } 

protected override void OnModelCreating(DbModelBuilder modelBuilder) 
{ 
     modelBuilder.Entity<AppTables.Programs.Program>() 
       .HasOptional(m => m.MainSubject) 
       .WithMany(t => t.ProgramsMain) 
       .HasForeignKey(m => m.MainContactSubjectId) 
       .WillCascadeOnDelete(false); 

     modelBuilder.Entity<AppTables.Programs.Program>() 
       .HasOptional(m => m.SecondarySubject) 
       .WithMany(t => t.ProgramsSecondary) 
       .HasForeignKey(m => m.SecondaryContactSubjectId) 
       .WillCascadeOnDelete(false); 
}