我正在使用EntityFramework將項目版本號保存到數據庫 。在UI頁面中,用戶鍵入版本(主要,次要,構建)整數值並單擊保存按鈕 在I保存,我想確保在數據庫中沒有創建重複版本。實體框架如何防止重複項進入db
什麼,我想是確保major.minor.build組合是唯一
ProjVersion newVersion=new ProjVersion();
newVersion.Major=10;
newVersion.Minor=1;
newVersion.Build=1;
this.repository.Add<ProjVersion>(newVersion);
//here how can I ensure that no duplicate versions are added to database
this.repository.SaveChanges();
[Serializable]
public class ProjVersion
{
[Key]
public int Version_Id { get; set; }
public int Major { get; set; }
public int Minor { get; set; }
public int Build { get; set; }
}
並在其周圍添加「使用(TransactionScope tsTransScope = new TransactionScope())」。 – Ubikuity
version_id是一個自動生成的數字。因此每次添加新記錄時,它都會生成一個新的數字作爲version_id。但這裏我正在嘗試確保major.minor.build組合是唯一的 – Millar
@Millar,是啊,這是我的錯誤。它已被編輯,現在顯示正確的代碼。 –