0

我首先使用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) 

我不知道什麼GrantApplicationIsGrantApplication_Id以及它們爲什麼是列名稱。從何而來?

+1

不應該這個'GrantApplicationIs'是'GrantApplicationId'嗎? –

+0

我不知道什麼是GrantApplicationIs,它來自哪裏。我甚至沒有在我的GrantApplication表中有這個名字的列。 –

+0

@Brendan不在你的桌子上。但是在你的'AuditEntry'類中你有一個'GrantApplicationIs'屬性。 – Eranga

回答

0

對於我來說,它看起來,這是問題的根源:

public int GrantApplicationIs { get; set; } 

這是一個錯字?你不是GrantApplicationId?反正效果是:

  • GrantApplicationIs不確認爲GrantApplication導航屬性外鍵屬性。相反,它是一個普通的標量屬性。如果在數據庫中沒有像這樣命名的列,則會得到第一個例外:列名稱'GrantApplicationIs'無效。

  • EF使用FK數據庫列的默認名稱,即GrantApplication_Id。如果在數據庫中沒有像這樣命名的列,則會得到第二個例外:無效的列名稱'GrantApplication_Id'

解決方案:

  • 要麼按照約定(GrantApplicationId
  • 或申請數據註釋或流暢API來告訴EF這GrantApplicationIs確實是您的FK屬性名稱的FK屬性(創建DB中適當的列)。
+0

請參閱我的更新。 –

+0

@Brendan:是的,我希望你的桌子看起來像這樣。問題是您的'AuditEntry'類中的屬性名稱'GrantApplicationIs'與數據庫中的列名稱'GrantApplicationId'不匹配。你說你正在使用Code-First,所以你已經手寫了你的類,對吧?你不能簡單地將屬性名稱更改爲'GrantApplicationId'嗎?這將是解決這兩個例外的最簡單方法。 – Slauma

+0

對不起所有的麻煩。這是我的一個錯字。 GrantApplicationIs應該是GrantApplicationId。 –