我想在MVC中的Personal表和User表之间设置一对一或一对零的关系。 这是我个人的班级代码:
public class Personal
{
#region Configuration
internal class Configuration : System.Data.Entity.ModelConfiguration.EntityTypeConfiguration<Personal>
{
public Configuration()
{
HasRequired(current => current.ApplicationUser)
.WithOptional(user => user.PersonalU)
.WillCascadeOnDelete(true);
}
}
#endregion Configuration
public string UserName { get; set; }
[Key,ForeignKey("ApplicationUserID")]
public string ApplicationUserID { get; set; }
public virtual ApplicationUser ApplicationUser { get; set; }
}
这是ApplicationUser代码:
public class ApplicationUser : IdentityUser
{
public virtual Personal PersonalU { get; set; }
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
return userIdentity;
}
}
但是当我使用Personal表添加控制器时,错误“不支持每种类型的多个对象集。对象集'ApplicationUsers'和'Users'都可以包含'WebApplication1.Models.ApplicationUser'类型的实例。”完了。
答案 0 :(得分:0)
而不是
[Key,ForeignKey("ApplicationUserID")]
public string ApplicationUserID { get; set; }
public virtual ApplicationUser ApplicationUser { get; set; }
设置此
public string ApplicationUserID { get; set; }
[ForeignKey("ApplicationUserID")]
public virtual ApplicationUser ApplicationUser { get; set; }
答案 1 :(得分:0)
我猜你是想明确地将ApplicationUser
放在DbContext
中,但你的DbContext
可能会继承IdentityDbContext
已经有一个数据库集IdentityUser
。
不要这样做:
public class MyAwesomeContext : IdentityDbContext<ApplicationUser>
{
public DbSet<ApplicationUser> ApplicationUsers { get; set; } // this shouldn't exist
public DbSet<Personal> Personals { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new Configuration());
}
}
我建议将Configuration
重命名为PersonalConfiguration
或其他内容,否则有人会认为它来自迁移的配置。
由于IdentityDbContext<>
已经为DbSet
中指定的TUser
类型声明了IdentityDbContext
,因此您只需忽略ApplicationUser
数据库集,如下所示:
public class MyAwesomeContext : IdentityDbContext<ApplicationUser>
{
public DbSet<Personal> Personals { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder); // don't forget this...
modelBuilder.Configurations.Add(new Configuration());
}
}
确保将ApplicationUser
置于IdentityDbContext<>
通用类型内。
此外,您的Personal
课程必须定义一个键,您可以在EntityTypeConfiguration
中执行此操作:
internal class Configuration : System.Data.Entity.ModelConfiguration.EntityTypeConfiguration<Personal>
{
public Configuration()
{
HasKey(p => p.ApplicationUserID);
HasRequired(current => current.ApplicationUser)
.WithOptional(user => user.PersonalU)
.WillCascadeOnDelete(true);
}
}
...并从您的班级中移除[Key,ForeignKey("ApplicationUserID")]
,因为您已经在使用流利的api 。
答案 2 :(得分:0)
尝试使用EF 6.0实现这一目标 - 检查是否有用
public class Personal
{
public string UserName { get; set; }
public string ApplicationUserID { get; set; }
[ForeignKey("ApplicationUserID")]
public virtual ApplicationUser ApplicationUser { get; set; }
}
public class ApplicationUser : IdentityUser
{
[Key]
public string ApplicationUserID { get; set; }
public virtual Personal PersonalU { get; set; }
}
protected override void OnModelCreating(DbModelBuilder builder)
{
builder.Entity<Personal>()
.HasKey(m => m.ApplicationUserID);
builder.Entity<ApplicationUser>()
.HasRequired(current => current.PersonalU)
.WithOptional(user => user.ApplicationUser)
.WillCascadeOnDelete(true);
}