本文介绍了如何设置NEWID()为GUID的实体框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我创建asp mvc4 sample.In这在我的DataContext样表创建ID列GUID。
I am creating asp mvc4 sample.In this i created Id column as GUID in Sample table of datacontext.
public class Sample { [Required] public Guid ID { get; set; } [Required] public string FirstName { get; set; } }这是实体表
CreateTable( "dbo.Samples", c => new { ID = c.Guid(nullable: false), FirstName = c.String(nullable: false) }) .PrimaryKey(t => t.ID);ID传递00000000-0000-0000-0000-000000000000。
Id pass 00000000-0000-0000-0000-000000000000.
如何设置 NEWID()到 GUID 并在那里我有设置。
How to set newid() to GUID and where i have to set.
推荐答案我只想用长为您的ID类型建议。它只是工作与拥有超过GUID一定的性能提升。但是,如果你想使用一个GUID,您应该使用顺序GUID 并设置它在构造函数中。我也将做ID的私人二传手:
I would recommend just using long for your ID type. It "just works" with and has some performance gains over GUID. But if you want to use a GUID, you should use a Sequential GUID and set it in the constructor. I would also make ID a private setter:
public class Sample { public Sample() { ID = GuidComb.Generate(); } [Required] public Guid ID { get; private set; } [Required] public string FirstName { get; set; } }连续GUID
public static class GuidComb { public static Guid Generate() { var buffer = Guid.NewGuid().ToByteArray(); var time = new DateTime(0x76c, 1, 1); var now = DateTime.Now; var span = new TimeSpan(now.Ticks - time.Ticks); var timeOfDay = now.TimeOfDay; var bytes = BitConverter.GetBytes(span.Days); var array = BitConverter.GetBytes( (long)(timeOfDay.TotalMilliseconds / 3.333333)); Array.Reverse(bytes); Array.Reverse(array); Array.Copy(bytes, bytes.Length - 2, buffer, buffer.Length - 6, 2); Array.Copy(array, array.Length - 4, buffer, buffer.Length - 4, 4); return new Guid(buffer); } }更多推荐
如何设置NEWID()为GUID的实体框架
发布评论