DBContext:
public MyDb () : base("name=conn") { Database.SetInitializer<MyDb>(null); } protected override void OnModelCreating (DbModelBuilder modelBuilder) { //這句是不要將EF生成的sql表名不要被複數 就是表名後面不要多加個S modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); } public DbSet<TestTable> TestTable { get; set; }
Entity:
public class TestTable { public int Id { get; set; } public string Name { get; set; } /// <summary> /// 數據庫中對應時間戳的字段, 一定要加註解, 否則ef不進行解讀 /// </summary> [Timestamp] public byte[] timespan { get; set; } }
Table:
測試:
- 數據庫中此時的數據
- 兩份測試代碼
-
模擬併發同時修改一條數據
-
數據 9 修改成功
-
數據 10 拋出異常
-
-