我有一個問題,我是不是能夠通過尋找其他的解決方案來解決這些問題。問題是我從用戶那裏得到一個應用程序。應用程序可以處於不同的狀態,我將應用程序本身保存在APPLICATION表中,並將應用程序的狀態保存在APPLICATIONSTATE表中。當用戶提交應用程序時,默認的應用程序狀態值應寫入應用程序狀態表。在此期間,應用程序的狀態將被更改,並且每個更改都應寫入APPLICATIONSTATE表中。酒店「的applicationID」是對象的關鍵信息的一部分,不能被修改
我使用實體框架數據庫第一的方針,(甲骨文EF),這裏是我的數據庫ER圖:
下面是由實體框架生成的部分類:
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我可以克服這個錯誤?提前致謝。
我怎樣才能克服這種錯誤?提前致謝。
問題是'myApplication.APPLICATIONID = applicationId;'行。無論是'myApplication.APPLICATIONID'應該由數據庫自動生成,你不需要該呼叫,或者你之前應該這樣做**'model.APPLICATION.Add(所有MyApplication);' –
伊萬,這不是重點。 applicationId是從數據庫中獲取的,當我刪除if子句時,代碼運行時沒有錯誤。 – tahasozgen
哪條if子句? SaveChanges裏面的那個? –