我正在使用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,是啊,这是我的错误。它已被编辑,现在显示正确的代码。 –