如何在asp.net mvc4中添加外键数据

本文关键字:添加 数据 mvc4 asp net | 更新日期: 2023-09-27 18:03:17

Ok,所以我已经在由websecurity助手创建的相同数据库中创建了我的表。

我的班级是

public class MovieModels
{
    public int id { get; set; }
    public string name { get; set; }
    [ForeignKey("UserProfile")]
    public int UserId { get; set; }
}

每个电影都是由用户添加的。现在这里的问题是,我不想根据当前登录的用户Id自动填写UserId字段。我认为触发器将在这里使用,但因为我是绝对新的asp.net mvc我不知道如何做到这一点。

如有任何帮助,不胜感激。


好的,我将代码改为

[ForeignKey("UserProfile")] 
    private int id_of_the_user;
    public int UserId
    {
        get { return id_of_the_user; }
        set { id_of_the_user = WebSecurity.CurrentUserId; }
    }

但是现在奇怪的是UserId字段总是显示0而不是当前登录用户的id。

如何在asp.net mvc4中添加外键数据

可以吗

   public class MovieModels
    {    
        public int id { get; set; }
        public string name { get; set; }
        [ForeignKey("UserProfile")]
        public int UserId
        {
            get { return WebSecurity.CurrentUserId; }            
        }
    }