【问题标题】:ASP.Net MVC 4 SimpleMembership UserID as Foreign KeyASP.Net MVC 4 SimpleMembership UserID 作为外键
【发布时间】:2013-10-22 19:19:43
【问题描述】:

亲爱的 SO 成员,请帮助我摆脱我认为应该很容易的任务,但我已经被困了 2 天。我有几个表需要对当前登录用户的 UserId 进行 FK。这是一个例子

用户资料

    [Table("UserProfile")]
    public class UserProfile
    {
        [Key]
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
        public int UserId { get; set; }
        public string UserName { get; set; }
        public string Email { get; set; }
        public string ManagerName { get; set; }
        public string PhoneNumber { get; set; }
   }

客户实体

 public class Customer
    {
        [Key]
        public int CustomerId { get; set; }
        public string Names { get; set; }

        [ForeignKey("UserProfile")]
        public int UserId { get; set; }

        public virtual UserProfile UserProfile { get; set; }
    }

数据库上下文

public class UsersContext : DbContext
    {
        public UsersContext()
            : base("DefaultConnection")
        {
        }

        public DbSet<UserProfile> UserProfiles { get; set; }
        public DbSet<Customer> Customers { get; set; }

    }

客户控制器

[HttpPost]
        [InitializeSimpleMembership]
        [ValidateAntiForgeryToken]
        public ActionResult Create(Customer model)
        {
            if (ModelState.IsValid)
            {
                model.UserProfile = db.UserProfiles.FirstOrDefault(u => u.UserId == WebSecurity.CurrentUserId);
                db.Customers.Add(model);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.UserId = new SelectList(db.UserProfiles, "UserId", "UserName", model.UserId);
            return View(model);
        }

也试过了

 [HttpPost]
    [InitializeSimpleMembership]
    [ValidateAntiForgeryToken]
    public ActionResult Create(Customer model)
    {
        if (ModelState.IsValid)
        {
            model.UserId = WebSecurity.GetUserId(User.Identity.Name);
            db.Customers.Add(model);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        ViewBag.UserId = new SelectList(db.UserProfiles, "UserId", "UserName", model.UserId);
        return View(model);
    }

在每种情况下我都会收到错误消息

"The INSERT statement conflicted with the FOREIGN KEY constraint "FK_dbo.Customers_dbo.UserProfile_UserId". The conflict occurred in database "ValueCardProject", table "dbo.UserProfile", column 'UserId'.
The statement has been terminated."

我想要做的就是能够创建带有 FK 到已登录用户的 UserID 的表。为了充分披露的利益,客户不应该直接创建为用户,在我的用例中,当前登录的用户是零售商,所以我想要完成的是创建一个带有 FK 到 RetailerId 的客户记录(即UserProfile 中的 UserId)。

感谢您的宝贵时间。

【问题讨论】:

  • 因为您正在尝试添加同一个用户。 GetUserId 将返回已存在用户的信息,然后您使用该 Id 创建具有相同 Id 的新用户
  • 用户当前是否已登录系统? WebSecurity.CurrentUserId 返回什么值?它是否与 UserProfile 表中的条目匹配?

标签: asp.net-mvc-4 entity-framework-5 simplemembership


【解决方案1】:

我认为您在 UserId 上的外键属性放错了位置。

你应该这样做:

public int UserId { get; set; }

[ForeignKey("UserId")]
public virtual UserProfile UserProfile { get; set; }

【讨论】:

    猜你喜欢
    • 2015-01-26
    • 2013-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-04
    • 2012-10-08
    • 1970-01-01
    相关资源
    最近更新 更多