如何与asp身份用户建立一对多的关系 [英] How to make one-to-many relationship with asp identity users

查看:213
本文介绍了如何与asp身份用户建立一对多的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用默认的Web应用程序与MVC +身份管理用户,并尝试让每个注册用户制作他的电影列表,并能够只在其登录时看到他的列表。



所以,对于我的例子,我已经做了一个电影类

  public class Movie 
{
public int MovieId {get;组; }
public string MovieName {get;组; }
public string Year {get;组; }
public string类型{get;组; }

public virtual ApplicationUser ApplicationUser {get;组; }
}

在ApplicationUser中我已经把

  public virtual ICollection< Movie>电影{get;组; 

有一个电影列表。



<我也做了一个实体框架MovieController,使用Movie作为模型和ApplicationDbContext作为上下文。



所以我有问题查询数据库,以使每个用户只能查看他的电影列表,与默认脚手架我得到这在MoviesController的索引操作

  public ActionResult Index()
{
var movies = db.Movies.ToList();
返回查看(电影);
}

如果有任何人有我的问题的经验,请帮助,任何投入欢迎和许多

解决方案

您需要在您的电影类ApplicationUserID中添加一个属性,以创建它们之间的正确关系: / p>

  public class Movie 
{
public int MovieId {get;组; }
public string MovieName {get;组; }
public string Year {get;组; }
public string类型{get;组; }

public string ApplicationUserID {get; set; }
public virtual ApplicationUser ApplicationUser {get;组; }
}

现在,您可以为这样一个用户获取所有电影:

  public ActionResult Index()
{
ApplicationDbContext context = new ApplicationDbContext();
string userid = User.Identity.GetUserId();
IEnumerable< Movie> movies = new List< Movie>();
if(!string.IsNullOrEmpty(userid))
{
movies = context.Movies.Where(x => x.ApplicationUserID == userid);
}

return查看(电影);
}


Im using the default Web Application with MVC + Identity for managing users, and im trying to make every registered user to make his movie list and to be able to see only his list when logs in.

So, for my example i have made a movie class

public class Movie
    {
        public int MovieId { get; set; }
        public string MovieName { get; set; }
        public string Year { get; set; }
        public string Genre { get; set; }

        public virtual ApplicationUser ApplicationUser { get; set; }
    }

and in ApplicationUser i have put

public virtual ICollection<Movie> Movies { get; set; }

to have a list of movies.

I also made a Entity FrameWork MovieController, used Movie as model and ApplicationDbContext as context.

So i have problems querying the db in order to get each user to view only his movie list, with the default scaffolding i get this in index action of MoviesController

 public ActionResult Index()
        {
            var movies = db.Movies.ToList();
            return View(movies);
        }

If anyone has experience with my problem please help, any input is welcomed and much appreciated.

解决方案

You need to add one more property in your movie class of ApplicationUserID for creating proper relationship between them like this:

public class Movie
    {
        public int MovieId { get; set; }
        public string MovieName { get; set; }
        public string Year { get; set; }
        public string Genre { get; set; }

        public string ApplicationUserID { get;set; }        
        public virtual ApplicationUser ApplicationUser { get; set; }
    }

Now you can get all movies for a user like this:

public ActionResult Index()
        {
            ApplicationDbContext context = new ApplicationDbContext();
            string userid = User.Identity.GetUserId();
            IEnumerable<Movie> movies = new List<Movie>();
            if (!string.IsNullOrEmpty(userid))
            {
                movies = context.Movies.Where(x => x.ApplicationUserID == userid);
            }

            return View(movies);
        } 

这篇关于如何与asp身份用户建立一对多的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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