什么是在Entity Framework中手动生成主键的最佳方式4.1代码优先?什么是在Entity Framework中手动生成主键的最佳方式4.1代码优先
我正在编程ASP.NET MVC 3,我使用存储库模式。
'Code First Class
Public Class Foo
<Key()>
<DatabaseGenerated(DatabaseGeneratedOption.None)>
Public Property iId As Integer
Public Property sBar As String
End Class
'Context Class
Public Class FooBarContext : Inherits DbContext
Public Property Foos As DbSet(Of Foo)
End Class
'Get the current Id
'Part of code in repository that stores Entity Foo.
Dim iCurrId as Integer = (From io In context.Foo
Select io.iId()).Max
Dim iNewId as Integer = iCurrId + 1
Foo.iId = iNewId
我consern是(但不太可能),这两个(或更多)的用户将试图挽救实体美孚在同一时间:
我目前使用下面的代码生成一个顺序键并且因此将获得相同的ID并且插入将失败。
这是一个好方法,还是有更好的?
请不要说我无法(也不会)使用数据库生成的标识字段!
为什么你不必使用身份? – 2011-05-07 22:46:23