For reference, I've done what this accepted answer recommends to fix the problem.
Specifically I'm having trouble with my aspnet_Roles
table when I try to add new roles. See my model and my code below:
public partial class aspnet_Roles
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage",
"CA2214:DoNotCallOverridableMethodsInConstructors")]
public aspnet_Roles()
{
RoleId = Guid.NewGuid();
aspnet_Users = new HashSet<aspnet_Users>();
}
public Guid ApplicationId { get; set; }
[Key]
public Guid RoleId { get; set; }
[Required]
[StringLength(256)]
public string RoleName { get; set; }
[Required]
[StringLength(256)]
public string LoweredRoleName { get; set; }
[StringLength(256)]
public string Description { get; set; }
public virtual aspnet_Applications aspnet_Applications { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage",
"CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<aspnet_Users> aspnet_Users { get; set; }
}
The model is pretty standard... I added the NewGuid
assignment to the constructor since I'm building this code-first. I'm not sure if ASP Membership works with int
values so I'm (grudgingly) hanging onto the Guids.
I'm using the Web.Security.Roles
wrapper to add the roles via CreateRole
:
protected override void Seed(Ortund.Models.OrtundDbContext context)
{
Roles.CreateRole("global"); // Supreme ruler of all that he surveys.
Roles.CreateRole("admin"); // Has some privileges.
Roles.CreateRole("user"); // Nothing special happening here.
}
To my understanding, given the addition to the model of the RoleId = Guid.NewGuid()
assignment, I should now be able to add these roles into the database with no errors as this is apparently meant to simulate the assignment of newid()
as a default value on the database.
This isn't the case, however.
Interesting to note is that if I do update the database outside of my data model to include the newid()
or newsequentialid()
default value, the roles can be added with the above code and without any difficulty.
If I don't add the default values on the database, the following error appears in the Visual Studio debugger:
Cannot insert the value NULL into column 'RoleId', table 'OrtundStuff.dbo.aspnet_Roles'; column does not allow nulls. INSERT fails.
What have I missed? Why doesn't this NewGuid() thing work?