0
我使用的GUID爲我的數據庫中的實體主鍵,使用模型綁定的asp.net 4.5 Web表單功能,當IM使用實體框架5將在數據庫中的記錄自動IM做類似生成的GUID主鍵EF5
public void onInsert([Control("ControlID")] int? countryID){
if(countryID.hasValue){
var DbEntityToInsert = new DbEntity(); //where DbEntity is the class generated by the EF
TryUpdateModel(DbEntityToInsert);
DbEntityToInsert.GuidPK = Guid.NewGuid();
if(Page.ModelState.IsValid){
using(var db = new DatabaseContext()){
db.Add(DbEntityToInsert);
db.Save();
}//using ends
}//modelstate.isvalid if ends
}//countryid.hasvalue ends
}//main method ends
現在我想問問有沒有一種方法,我可以告訴EF生成GUID的PK同時插入一個新的記錄,因此我沒有寫線
DbEntityToInsert.GuidPK = Guid.NewGuid();