Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 实体框架-违反多重性约束_C#_.net_Entity Framework - Fatal编程技术网

C# 实体框架-违反多重性约束

C# 实体框架-违反多重性约束,c#,.net,entity-framework,C#,.net,Entity Framework,这个问题被问了很多次,我尝试了所有的解决方案,它们似乎都不起作用 模型包括: public class User : IdentityUser { public virtual Profile Profile { get; set; } } public class Profile { public virtual string Id { get; set; } public virtual User User { get; set; } } protected overrid

这个问题被问了很多次,我尝试了所有的解决方案,它们似乎都不起作用

模型包括:

public class User : IdentityUser
{
  public virtual Profile Profile { get; set; }
}

public class Profile
{
   public virtual string Id { get; set; }
  public virtual User User { get; set; }
}

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    Database.SetInitializer(new MigrateDatabaseToLatestVersion<ApplicationDbContext, Configuration>());
    modelBuilder.Entity<User>().HasRequired(x => x.Profile).WithRequiredPrincipal(x => x.User);
    modelBuilder.Entity<Profile>().HasRequired(x => x.User).WithRequiredDependent(x => x.Profile);
    base.OnModelCreating(modelBuilder);
}
因此,在简单的选择之后,代码失败了

编辑:

错误是

违反了多重性约束。关系“Thanker.Models.DAL.Profile\u User”的角色“Profile\u User\u Source”的重数为1或0..1

EDIT2
出于某种原因,实体的DefaultConstructor正在那里被调用。你知道为什么吗

原因是我的配置文件和用户的默认构造函数正在对实体进行更改。由于某些原因,即使在创建简单secect时也会调用默认构造函数。

是否检查了数据库中的重复配置文件?是的,配置文件和用户是唯一的……您正在具体化实体(
ToList()
),因此会调用构造函数。
 foreach (var user in context.Users.Include(x => x.Profile).ToList())
 {
     //I commented everything here while debugging  
 }
 context.saveChanges();