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

asp.net-identity asp.net-mvc entity-framework-6

Question

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.

1
4
9/15/2015 8:02:08 PM

Accepted Answer

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);
        } 
5
9/16/2015 9:21:54 AM

Popular Answer

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)



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow