考虑我有以下课程:
class User
{
[Key]
public Guid Id {get; set;}
public string UserName {get; set;}
}
class MyUsersContext: DbContext
{
DbSet<User> Users {get; set;}
}
class MyRepository
{
MyUsersContext _db; //INJECTED
User AddUser(string userName)
{
var newBorn = new User() { UserName = userName };
_db.Users.Add(newBorn);
return newBorn;
}
}
class MyMemProvider: MembershipProvider
{
MyRepository repo; //INJECTED
public override MembershipUser CreateUser(string username...
{
var ret = repo.AddUser(username);
return new MembershipUser(System.Web.Security.Membership.Provider.Name, ret.UserName...
}
}
这是我的ASP.NET MVC3站点的自定义成员资格提供程序的精简版本。在这种情况下,User.Id在什么时候充满了实际的Guid?
答案 0 :(得分:0)
您必须在Id属性上应用 DatabaseGeneratedOption 。
class User
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid Id {get; set;}
public string UserName {get; set;}
}