我首先使用Ef 4.1代碼。EF 4.1代碼首先在插入父對象和子對象時遇到問題
我收到follwing錯誤和不知道我做錯了:保存不爲他們的關係暴露的外鍵的屬性的實體發生
錯誤。 EntityEntries屬性將返回null,因爲單個實體不能被識別爲異常的來源。通過在您的實體類型中公開外鍵屬性,可以更輕鬆地處理保存時的異常。有關詳細信息,請參閱InnerException。
內部異常:
無效的列名稱GrantApplicationIs「。 列名稱'GrantApplication_Id'無效。
我有一個授予申請,我試圖添加一個審計條目,當我將它保存到數據庫。
這裏是我的背景:
public class HbfContext : DbContext
{
public DbSet<Bank> Banks { get; set; }
public DbSet<AccountType> AccountTypes { get; set; }
public DbSet<GrantApplication> GrantApplications { get; set; }
public DbSet<AuditEntry> AuditEntries { get; set; }
}
資助申請類:
public class GrantApplication
{
public int Id { get; set; }
public string EmployeeNumber { get; set; }
public string Title { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public virtual ICollection<AuditEntry> AuditEntries { get; set; }
}
AuditEntry類:
public class AuditEntry
{
public int Id { get; set; }
public int OldValue { get; set; }
public int NewValue { get; set; }
public DateTime AuditDate { get; set; }
public string EmployeeNumber { get; set; }
public int GrantApplicationIs { get; set; }
public GrantApplication GrantApplication { get; set; }
}
這是我如何添加一個新的審計進入一個新的贈款應用:
public void Insert(GrantApplication grantApplication)
{
DateTime currentDateTime = DateTime.Now;
string submitterEmployeeNumber = "123456";
grantApplication.SignatureDate = currentDateTime;
grantApplication.SubmitterEmployeeNumber = submitterEmployeeNumber;
// Add audit entry
grantApplication.AuditEntries = new List<AuditEntry>();
grantApplication.AuditEntries.Add(new AuditEntry
{
NewValue = grantApplication.GrantApplicationStateId,
AuditDate = currentDateTime,
EmployeeNumber = submitterEmployeeNumber
});
// Insert the new grant application
grantApplicationRepository.Insert(grantApplication);
}
UPDATE:
我的表結構是這樣的:
GrantApplications
表:
Id int
EmployeeNumber varchar(6)
Title varchar(10)
FirstName varchar(50)
LastName varchar(50)
AuditEntries
表:
Id int
GrantApplicationId int
OldValue int
NewValue int
AuditDate datetime
EmployeeNumber varchar(6)
我不知道什麼GrantApplicationIs
和GrantApplication_Id
以及它們爲什麼是列名稱。從何而來?
不應該這個'GrantApplicationIs'是'GrantApplicationId'嗎? –
我不知道什麼是GrantApplicationIs,它來自哪裏。我甚至沒有在我的GrantApplication表中有這個名字的列。 –
@Brendan不在你的桌子上。但是在你的'AuditEntry'類中你有一個'GrantApplicationIs'屬性。 – Eranga