我有實體程序有兩個外鍵到我的主題表(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,現在一切正常。抱歉浪費了大家的時間。謝謝!
您確定錯誤在數據訪問層嗎?你的控制器呢?請指定更多關於指導數據插入的渠道的信息。 – Emad
ProgramsMain和ProgramsSecondary是什麼樣的? – Bit
我想出了我的答案。我換了.HasRequired to .HasOptional,現在一切正常。抱歉浪費了大家的時間。謝謝! – Ben