使用EF-Code-First,我想设置一个与ASP.Net 4.0+附带的内置简单成员资格提供程序(1-1)相关的DB表
AccountModels.cs中的UserProfile类是一个简单的两字段类:
[Table("UserProfile")]
public class UserProfile
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public string UserName { get; set; }
}我想要分开保存的包含其他用户信息的类具有以下格式:
[Table("UserInfo")]
public class UserInfo
{
[Key]
public int ID { get; set; }
<user info fields here...>
public virtual UserProfile UserProfile { get; set; }
}上下文是:
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();
}
}现在,在不更改任何其他东西的情况下,当我运行应用程序时,模型构建得很好,但当WebSecurity.CreateUserAndAccount(model.UserName, model.Password);运行时,我得到了一个错误(帐户控制器第82行)。The INSERT statement conflicted with the FOREIGN KEY constraint "UserInfo_UserProfile".
什么是(假设的)存储用户信息的首选方法,我希望以后能够直接从DB引用(即,根据旧的配置文件提供程序,不在blobs中)
我如何使用EF-CodeFirst进行设置,而不是通过存储库层运行所有内容?我应该直接走存储库的路线吗?
在测试该方法时,我很难让azureSQL在UserProfile表中包含更多列,所以我希望将userInfo内容排除在UserProfile之外。
谢谢
发布于 2012-11-15 14:18:55
modelBuilder.Entity<UserInfo>()
.HasRequired(u => u.UserProfile)
.WithRequiredPrincipal();意味着您的UserProfile类反过来需要UserInfo。由于SimpleMembership将插入到UserProfile中,而不是UserInfo中,因此抛出了异常。
我不认为你需要这个,因为你的关系是可选的- UserProfile将在UserInfo之前存在,因为简单的成员关系会在你创建UserInfo之前创建它。
http://social.msdn.microsoft.com/Forums/en/adonetefx/thread/257f33d2-9870-46ce-9b0b-147935162a0c
https://stackoverflow.com/questions/12425708
复制相似问题