Using EF-Code-First I want to set up a DB table that is related (1-1) to the inbuilt Simple Membership provider that comes OOTB with ASP.Net 4.0+
The UserProfile class in AccountModels.cs is a simple 2 field class:
[Table("UserProfile")]
public class UserProfile
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public string UserName { get; set; }
}
My class with other user info I want to keep separate has this format:
[Table("UserInfo")]
public class UserInfo
{
[Key]
public int ID { get; set; }
<user info fields here...>
public virtual UserProfile UserProfile { get; set; }
}
The Context is:
public class UsersContext : DbContext
{
public UsersContext()
: base("DefaultConnection")
{
}
public DbSet<UserProfile> UserProfiles { get; set; }
public DbSet<UserInfo> UsersInfo { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<UserInfo>()
.HasRequired(u => u.UserProfile)
.WithRequiredPrincipal();
}
}
Now, without changing anything else, when I run the app, the models build fine, but I’m getting a error when WebSecurity.CreateUserAndAccount(model.UserName, model.Password); runs (Acct Controller Line 82). The INSERT statement conflicted with the FOREIGN KEY constraint "UserInfo_UserProfile".
What is the (assumed) preferred method of storing user information that I want to be able to reference directly from the DB later (ie. not in blobs as per the old Profile provider)
How do I set this up using EF-CodeFirst as opposed to running everything through a repository layer? Should I just go the repository route?
I had trouble getting azureSQL to include more columns in the UserProfile table when I tested that method, so would like to keep the userInfo stuff out of UserProfile.
Thanks
Means your UserProfile class in turn requires UserInfo. Since SimpleMembership will insert into UserProfile and not UserInfo, the exception is thrown.
I don’t think you need this off the top of my head since your relationship is optional – UserProfile will exist before UserInfo, as simplemembership will create it prior to you creating a UserInfo.
http://social.msdn.microsoft.com/Forums/en/adonetefx/thread/257f33d2-9870-46ce-9b0b-147935162a0c