2017-01-09 64 views
0

我有一個問題,我是不是能夠通過尋找其他的解決方案來解決這些問題。問題是我從用戶那裏得到一個應用程序。應用程序可以處於不同的狀態,我將應用程序本身保存在APPLICATION表中,並將應用程序的狀態保存在APPLICATIONSTATE表中。當用戶提交應用程序時,默認的應用程序狀態值應寫入應用程序狀態表。在此期間,應用程序的狀態將被更改,並且每個更改都應寫入APPLICATIONSTATE表中。酒店「的applicationID」是對象的關鍵信息的一部分,不能被修改

我使用實體框架數據庫第一的方針,(甲骨文EF),這裏是我的數據庫ER圖:

database er diagram

下面是由實體框架生成的部分類:

public partial class APPLICATION 
{ 
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")] 
    public APPLICATION() 
    { 
     this.APPLICATIONSTATE = new HashSet<APPLICATIONSTATE>(); 
    } 

    public int APPLICATIONID { get; set; }  

    public System.DateTime APPLICATIONDATE { get; set; } 

    public short PROVINCEID { get; set; } 
    public Nullable<short> ILCEID { get; set; } 
    public decimal AREA { get; set; } 
    public bool ISBASEMENTOK { get; set; } 
    public string APPLICATIONEXPLAINATION { get; set; } 

    public virtual PERSONEL LOJ_PERSONEL { get; set; } 
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] 
    public virtual ICollection<APPLICATIONSTATE> LOJ_APPLICATIONSTATE{ get; set; } 

} 

public partial class APPLICATIONSTATE 
{ 
    public long APPLICATIONSTATEID { get; set; } 
    public int APPLICATIONENUM { get; set; } 
    public System.DateTime CURRENTTIME { get; set; } 

    public virtual APPLICATION LOJ_APPLICATION { get; set; } 
    public virtual USER LOJ_USER { get; set; } 
} 

我正在使用Repository模式,下面是我嘗試添加新應用程序和應用程序狀態的代碼部分:

int applicationId = int.minvalue; 

    USER myuser = null; 

    APPLICATION myApplication = this.gettingFromSomewhere(); 

    using (HousingEntities model = new HousingEntities()) 
    { 
     applicantuser = model.LOJ_PERSONEL.FirstOrDefault(p => p.PERSONELID == YeniBasvuru.PersonelId); 
     myApplication.LOJ_USER = applicantuser; 
     model.APPLICATION.Add(myApplication); 
     //saving the application 
     model.SaveChanges(); 

     //getting the primary key of newly created application object from database. 
     applicationId = this.getMostCurrentIdOfApplication(); 
     myApplication.APPLICATIONID = applicationId; 

     //getting the user that submits application 
     applicant = model.USER.FirstOrDefault(p => p.LOJ_USER.USERID == myApplication.LOJ_PERSONEL.USERID); 

     //if clause-1 
     if(applicant != null) 
     { 
      //saving the state of the application. 
      appState = new APPLICATIONSTATE(); 

      //3 is the default state for application. When we need to change it to 4, newly row will be added. 
      appState.APPLICATIONSTATEID = 3; 
      appState.LOJ_APPLICATION = myApplication; 
      appState.LOJ_USER = applicant; 

      //I am getting an error here. 
      model.APPLICATIONSTATE.Add(appState); 
      model.SaveChanges(); 
     } 
    } 

    result.TransactionResult = true; 
    result.rowId = applicationId; 

} 
//other catches removed for clarity.  
catch (Exception ex) 
{ 
    islemSonuc = new FunctionResult (ex, this._olasihataciddiyeti); 
    this.writeError(ex); 
} 

我在上面指出該屬性的行收到錯誤「的applicationID」是對象的關鍵信息的一部分,不能進行修改。 error.How我可以克服這個錯誤?提前致謝。

我怎樣才能克服這種錯誤?提前致謝。

+3

問題是'myApplication.APPLICATIONID = applicationId;'行。無論是'myApplication.APPLICATIONID'應該由數據庫自動生成,你不​​需要該呼叫,或者你之前應該這樣做**'model.APPLICATION.Add(所有MyApplication);' –

+0

伊萬,這不是重點。 applicationId是從數據庫中獲取的,當我刪除if子句時,代碼運行時沒有錯誤。 – tahasozgen

+0

哪條if子句? SaveChanges裏面的那個? –

回答

0

伊萬Stoev先生的解決方案是正確的。在上面的代碼中,

appState.LOJ_APPLICATION = myApplication;

是錯誤點。我們只通過設置實體的主鍵來進行分配,我們不去數據庫。因此,EF不能僅與主鍵信息進行匹配,它還需要在後臺添加附加信息。所以,如果我們改變如下代碼:

所有MyApplication = model.APPLICATION(P => p.APPLICATIONID ==的applicationID)

現在,所有MyApplication包含從數據庫中最新鮮的資訊。當我們分配它時,EF能夠匹配實體。

相關問題