简体   繁体   English

如何在ASP.NET MVC4中添加外键数据

[英]How to add foreign key data in asp.net mvc4

Ok so i have created my table in the same database that is created by the websecurity helpers. 好的,所以我已经在由websecurity助手创建的数据库中创建了websecurity

My class is 我的课是

public class MovieModels
{
    public int id { get; set; }
    public string name { get; set; }

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

each movie is added by a user. 每部电影都是由用户添加的。 Now the problem here is that i wan't to automatically fill the field of UserId based on the Id of user currently logged in. I think triggers would be used here but since i am absolutely new to asp.net mvc i don't how that can be done. 现在的问题是,我不会根据当前登录的用户ID自动填写UserId字段。我认为这里会使用触发器,但是由于我对asp.net mvc绝对陌生,所以我不怎么做可以做到的。

Any help would be appreciated. 任何帮助,将不胜感激。


Update 更新资料

Ok so i have changed the code to 好的,所以我将代码更改为

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

But now strangely the UserId field always shows 0 instead the id of user currently looged in. What's wrong here ? 但是现在奇怪的是,UserId字段始终显示0而不是当前抢劫的用户ID。这是怎么回事?

Might this work 可能会做这项工作

   public class MovieModels
    {    
        public int id { get; set; }
        public string name { get; set; }

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM