EF创建一个新的用户配置每次我引用一个时间呢? [英] EF creates a new UserProfile every time I reference one?

查看:169
本文介绍了EF创建一个新的用户配置每次我引用一个时间呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是,下面的code不仅创造了一个ProtectedPassword(因为它应该),但也创造了被插入到它的主人,即使是业主alread存在的事实。

 用户配置用户= PublicUtility.GetAccount(User.Identity.Name); //得到现有用户配置
ProtectedPassword PW =新ProtectedPassword(model.Name,用户model.Password);
ProtectedPassword.Create(PW);

创建一个新的ProtectedPassword后,因此我结束了一个新的用户配置(具有相同的价值观,只是一个新的ID在previous一个)被引用。

我一直在搞乱这个问题了几个小时,将AP preciate如果有人可以帮助我!

顺便说一句,我用ASP.NET MVC4和EF code首先

首先,实体:
ProtectedPassword:

  [表(ProtectedPassword)]
    公共类ProtectedPassword:ProtectedProperty
    {        [需要]
        [MINLENGTH个(3)]
        [MAXLENGTH(20)]
        公共字符串密码{搞定;组; }        私人ProtectedPassword()
        {
        }        公共ProtectedPassword(字符串名称,用户配置所有者,字符串密码)
        {
            名称=名称;
            所有者=业主;
            密码=密码;
            子ID = PublicUtility.GenerateRandomString(8,0);
            TYPE = ProtectedPropertyType.Password;
        }        公共静态布尔创建(ProtectedPassword PW)
        {
            尝试
            {
                使用(MediaProfitsDb DB =新MediaProfitsDb())
                {
                    db.ProtectedPasswords.Add(PW);
                    db.SaveChanges();
                    返回true;
                }
            }
            抓住
            {
                返回false;
            }
        }
}

这ProtectedProperty继承:

 公共类ProtectedProperty
    {
        [键]
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)
        公众诠释物业ID {搞定;组; }        [需要]
        公共字符串SUBID {搞定;组; }        公众诠释下载{搞定;组; }        [需要]
        公众用户配置所有者获得{;组; }        [需要]
        公共字符串名称{;组; }        [需要]
        公共ProtectedPropertyType类型{搞定;组; }    }

和最终用户配置:

  [表(用户配置)]
    公共类用户配置
    {
        [键]
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)
        公众诠释用户ID {搞定;组; }
        [需要]
        公共字符串用户名{获得;组; }
        [需要]
        公共字符串AFFILIATEID {搞定;组; }
        公众用户配置的Referer {搞定;组; }
        [需要]
        公众诠释平衡{搞定;私人集; }
        [需要]
        [电子邮件地址]
        公共字符串PaypalEmail {搞定;组; }
        公众诠释AllTimeEarnings {搞定;组; }
}


解决方案

我认为这个问题是对密码的用户配置对象未附您使用插入的DbContext。这使得EF认为这是一个新的对象。

尝试:

 使用(MediaProfitsDb DB =新MediaProfitsDb())
{
    db.UserProfiles.Attach(pw.UserProfile);
    db.ProtectedPasswords.Add(PW);
    db.SaveChanges();
    返回true;
}

My issue is the fact that the following code creates not only a ProtectedPassword (as it should) but also creates the owner being inserted into it even though that owner alread exists.

UserProfile user = PublicUtility.GetAccount(User.Identity.Name); //gets an existing UserProfile
ProtectedPassword pw = new ProtectedPassword(model.Name, user, model.Password);
ProtectedPassword.Create(pw);

Thus after creating a new ProtectedPassword I end up with a new UserProfile (with the same values as the previous one except a new ID) being referenced.

I've been messing with this issue for a couple of hours now and would appreciate if someone could help me!

Btw, I use ASP.NET MVC4 and EF Code First.

Firstly, the entities: ProtectedPassword:

    [Table("ProtectedPassword")]
    public class ProtectedPassword : ProtectedProperty
    {

        [Required]
        [MinLength(3)]
        [MaxLength(20)]
        public string Password { get; set; }

        private ProtectedPassword()
        {
        }

        public ProtectedPassword(string name, UserProfile owner, string password)
        {
            Name = name;
            Owner = owner;
            Password = password;
            SubId = PublicUtility.GenerateRandomString(8, 0);
            Type = ProtectedPropertyType.Password;
        }

        public static bool Create(ProtectedPassword pw)
        {
            try
            {
                using (MediaProfitsDb db = new MediaProfitsDb())
                {
                    db.ProtectedPasswords.Add(pw);
                    db.SaveChanges();
                    return true;
                }
            }
            catch
            {
                return false;
            }
        }
}

Inherits from ProtectedProperty:

public class ProtectedProperty
    {
        [Key]
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
        public int PropertyId { get; set; }

        [Required]
        public string SubId { get; set; }

        public int Downloads { get; set; }

        [Required]
        public UserProfile Owner { get; set; }

        [Required]
        public string Name { get; set; }

        [Required]
        public ProtectedPropertyType Type { get; set; }

    }

And finally UserProfile:

    [Table("UserProfile")]
    public class UserProfile
    {
        [Key]
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
        public int UserId { get; set; }
        [Required]
        public string UserName { get; set; }
        [Required]
        public string AffiliateId { get; set; }
        public UserProfile Referer { get; set; }
        [Required]
        public int Balance { get; private set; }
        [Required]
        [EmailAddress]
        public string PaypalEmail { get; set; }
        public int AllTimeEarnings { get; set; }
}

解决方案

I think the problem is that the UserProfile object on the Password is not attached the the DbContext that you're using to Insert. This makes EF think that it's a new object.

Try:

using (MediaProfitsDb db = new MediaProfitsDb())
{
    db.UserProfiles.Attach(pw.UserProfile);
    db.ProtectedPasswords.Add(pw);
    db.SaveChanges();
    return true;
}

这篇关于EF创建一个新的用户配置每次我引用一个时间呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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