如何填充GUID属性

时间:2013-06-17 06:23:53

标签: c# c#-4.0 guid anonymous-types

我正在尝试使用Linq输入GUID。我的插入代码:

  var registration = new Registration
            {
                FirstName = registrationInfo.FirstName,
                LastName = registrationInfo.LastName,
                AddressId = _dbcontext.Addresses.Max(x => x.AddressId),
                Password = registrationInfo.Password,
                EmailAddress = registrationInfo.EmailAddress,
                IsActive = true,
                IsDeleted = false,
                Archived=false,
                DateCreated = DateTime.Now
                GUID = ?
            };

任何人都可以帮忙更换“?”一些有用的代码。

3 个答案:

答案 0 :(得分:3)

使用内置工厂方法创建一个新方法:

GUID = Guid.NewGuid()

顺便说一句,AddressId的设定者看起来很奇怪。如果在其间创建了另一个地址,则事情将会失败。请改用导航属性。

答案 1 :(得分:0)

取决于您是否需要现有的,空的或新的Guid选项之一:

Guid.NewGuid();
Guid.Empty;
Guid.Parse("{81a130d2-502f-4cf1-a376-63edeb000e9f}"); // replace with the GUID you need

另请参阅MSDN for GUID struct:http://msdn.microsoft.com/en-us/library/cey1zx63.aspx

答案 2 :(得分:0)

试试上面的

 var registration = new Registration
        {
            FirstName = registrationInfo.FirstName,
            LastName = registrationInfo.LastName,
            AddressId = _dbcontext.Addresses.Max(x => x.AddressId),
            Password = registrationInfo.Password,
            EmailAddress = registrationInfo.EmailAddress,
            IsActive = true,
            IsDeleted = false,
            Archived=false,
            DateCreated = DateTime.Now,
            GUID = Guid.NewGuid().ToString()
        };