1

を作成してログインした後、ExtendedProfileを作成することができました。類似の投稿はこちらUserProfileUserProfileExtendedProfileUserProfileUserIdExtendedProfileUserId

私のエンティティの設計:

[Table("UserProfile")]
public class UserProfile
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int UserId { get; set; }

    public string UserName { get; set; }

    public string Email { get; set; }
}

拡張プロファイル

public class ExtendedProfile
{        
    [Required]
    [Key]
    [ForeignKey("UserProfile"), DatabaseGenerated(DatabaseGeneratedOption.None)]
    [HiddenInput(DisplayValue = false)]
    public int UserId { get; set; }

    [Display(Name = "First name")]
    public string FirstName { get; set; }

    [Display(Name = "Last name")]
    public string LastName { get; set; }        

    public virtual UserContact UserContact { get; set; }

    public virtual UserProfile UserProfile { get; set; }
}

ユーザー連絡先

public class UserContact
{
    [Key]
    [Required]
    [ForeignKey("ExtendedProfile")]
    [HiddenInput(DisplayValue = false)]
    public int UserId { get; set; }

    [DataType(DataType.PhoneNumber)]
    [Display(Name = "Phone:")]
    public string Phone { get; set; }

    public virtual ExtendedProfile ExtendedProfile { get; set; }
}

アクションを作成

[HttpGet]
    public ActionResult Create()
    {            
        return View();
    }

    [HttpPost]
    public ActionResult Create(CreateExtendedUserViewModel model)
    {
        if (ModelState.IsValid)
        {
            try
            {
                var userId = WebSecurity.GetUserId(System.Web.HttpContext.Current.User.Identity.Name);
                var userProfile = _dataSource.UserProfiles.FirstOrDefault(u => u.UserId == userId);

                if (userProfile != null)
                {
                    var extendedProfile= new ExtendedProfile
                                        {
                                            UserProfile = userProfile,
                                            FirstName = model.FirstName,
                                            LastName = model.LastName,
                                            UserContact = new UserContact
                                                                {
                                                                   Phone = mode.Phone
                                                                }
                                        };
                    _dataSource.SaveUser(extendedProfile);
                    return RedirectToAction("About", "Home");
                }
            }
            catch (Exception)
            {
                ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists, see your system administrator.");
                throw;
            }
        }
        return View(model);
    }

編集

ユーザーの保存()

public void SaveUser(ExtendedUser user)
    {
        using (var context = new EFContextDb())
        {
            if (user.UserId== 0)
            {
                context.ExtendedUsers.Add(user);
            }
            else if (user.UserId> 0)
            {
                 var currentUser = context.ExtendedUsers
                    .Include(c => c.UserContact)
                    .Single(t => t.UserId== player.UserId);

                context.Entry(currentUser).CurrentValues.SetValues(user);
                currentUser.UserContact = user.UserContact;
            }
            context.SaveChanges();
        }
    }

なぜ戻ってメンバーシップテーブルにデータを挿入するのか理解できません。場合を除き、メンバーシップ テーブルと追加された残りのテーブルに異なるコンテキストを使用する必要があるかもしれません。

とにかく、箱から出してコントローラーで使用してもcontext.ExtendedUsers.Add(user);context.SaveChanges();同じことが起こります。IoCなしでデモアプリを作成し、すべてがうまく機能したため、これはIoC(Ninject)と関係があると思い始めています。

4

0 に答える 0