如何填充GUID属性
本文关键字:GUID 属性 填充 何填充 | 更新日期: 2023-09-27 18:18:44
我正在尝试使用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 = ?
};
谁能帮我把"?"换成一些有用的代码
使用内置的工厂方法创建一个新工厂:
GUID = Guid.NewGuid()
顺便说一下,AddressId
的setter看起来很奇怪。如果在两者之间创建了另一个地址,则会失败。使用导航属性。
根据您需要的是现有的、空的还是新的Guid,以下选择之一:
Guid.NewGuid();
Guid.Empty;
Guid.Parse("{81a130d2-502f-4cf1-a376-63edeb000e9f}"); // replace with the GUID you need
参见MSDN的GUID结构:http://msdn.microsoft.com/en-us/library/cey1zx63.aspx
试试上面的
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()
};