Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make one-to-many relationship with asp identity users

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.

like image 369
john Avatar asked Sep 15 '15 20:09

john


2 Answers

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);
        } 
like image 164
Ankit Sahrawat Avatar answered Sep 28 '22 07:09

Ankit Sahrawat


First you should change your link to applicationuser (from Movie) to be a collection (many to many).

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

If you want to fetch a list of movies for the logged in user you can fetch the user and movie list like this:

var user = UserManager.FindById(User.Identity.GetUserId());

Debug.WriteLine(user.Email);
var movies = user.Movies.ToList();
foreach (var movie in movies)
{
     Debug.WriteLine(movie.MovieName);
}

Instead of writing to the debug window you could return a view with movies like you have done in your own code.

- I would also suggest that you make use of viewmodels for sending data to/from your views. The way you are doing it now could potentially send the entire database to your view (depending on your query ofc). You will also get a more granulated control of what you display to the user. (you could read this: what-is-viewmodel-in-mvc to get you started)

like image 25
Indregaard Avatar answered Sep 28 '22 09:09

Indregaard