实体框架异常在运行时:在实体参与的关系? [英] Entity Framework Exception at run time: Entities in participate in the relationship?

查看:249
本文介绍了实体框架异常在运行时:在实体参与的关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用code第一种方式,当我跑我的项目。在新用户注册,实体框架抛出此异常:

While using code first approach when I run my project. During new user registration, the entity framework is throwing this exception:

其他信息:实体在'ApplicationDbContext.IdentityUsers'参与'ApplicationUser_UserDetails的关系。有关ApplicationUser_UserDetails_Target 0被发现了。 1'ApplicationUser_UserDetails_Target'的预期。

抛出异常:

var manager = new UserManager();
var user = new ApplicationUser() { UserName = UserName.Text };
//This line At the time of creating user
IdentityResult result = manager.Create(user, Password.Text);

我创建的模式code:

I created the schema code:

public class ApplicationUser : IdentityUser
    {
        public Nullable<DateTime> DateOfCreation { get; set; }
        public Nullable<bool> IsActive { get; set; }
        public Nullable<DateTime> LastLogin { get; set; }
        public UserDetails UserDetails { get; set; }
        public virtual ICollection<TaskSheetManagement> TaskSheet { get; set; }
        public virtual ICollection<Projects> Projects { get; set; }
    }

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext()
            : base("DefaultConnection")
        {
        }

        protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<IdentityUser>().HasKey(pk => pk.Id).ToTable("Users");
            modelBuilder.Entity<ApplicationUser>().HasKey(pk=>pk.Id).ToTable("Users");
            modelBuilder.Entity<UserDetails>().HasKey(pk => pk.UserDetailId).ToTable("UserDetails");
            modelBuilder.Entity<ApplicationUser>().HasRequired(p => p.UserDetails).WithRequiredPrincipal(c => c.ApplicationUser);
            modelBuilder.Entity<ApplicationUser>()
                .Property(t => t.UserName).IsRequired()
                .HasMaxLength(14)
                .HasColumnAnnotation("Index",
                new IndexAnnotation(new IndexAttribute("U_Username", 1) { IsUnique = true }));
            modelBuilder.Entity<UserDetails>()
                .Property(t => t.Email).IsRequired()
                .HasMaxLength(25)
                .HasColumnAnnotation("Index",
                new IndexAnnotation(new IndexAttribute("U_Email", 2) { IsUnique = true }));
            base.OnModelCreating(modelBuilder);
        }
    }

和的UserDetails是:

And UserDetails is:

public class UserDetails
{
    public int UserDetailId { get; set; }
    public string Name { get; set; }
    public string Email { get; set; }
    public string CountryName { get; set; }
    public string Gender { get; set; }
    public string Nic { get; set; }
    public string ResidentAddress { get; set; }
    public string CellNO { get; set; }
    public string PhoneNo { get; set; }
    public string Religion { get; set; }
    public ApplicationUser ApplicationUser { get; set; }
}

我无法找出这个例外是有关。我搜索谷歌和其他Implemeted一个解决方案,但解决不了。请帮助。

I couldn't find out what this exception is related to. I searched google and implemeted other solutions but couldn't resolve. Help please.

推荐答案

您已经创建了一个1:1的关联,但1:1的关联并不在实体框架以这种方式工作。

You've created a 1:1 association, but 1:1 associations do not work this way in Entity Framework.

1:1的社团必须共享相同的主键。你定义的方法就是两个1:M协会,他们互相冲突

1:1 Associations must share the same primary key. The way you've defined this is two 1:M associations, and they conflict with each other.

如果你想有一个1:1,那么你必须改变你的主键进行的UserDetails是ID,然后你必须给他们喜欢的东西一起映射:

If you want a 1:1, then you have to change your Primary Key for UserDetails to be Id, and then you have to map them together with something like:

 modelBuilder.Entity<UserDetails>().HasRequired(x => x.ApplicationUser)
            .WithRequiredDependent(x => x.UserDetails);

这篇关于实体框架异常在运行时:在实体参与的关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆