0

I have 2 model classes movie and customer. I made a one to many foreign key so that I can add a movie to a list in customer. But now I am unable to add a movie to a customer and I don't get any errors either.

Here is my code.

Movie class

public class Movie
{
    [Key]
    public int MovieId { get; set; }
    public String Name { get; set; }

    [Column(TypeName = "datetime2")]
    public DateTime? DateCreated { get; set; }

    public int? CustomerId { get; set; }
    public Customer Customer { get; set; }
}

Customer class

public class Customer
{
    [Key]
    public int CustomerId { get; set; }
    public string Name { get; set; }
    public string LastName { get; set; }
    public bool IsCreated { get; set; }
    public int MaxMovies { get; set; }

    [Column(TypeName = "datetime2")]
    public DateTime Created { get; set; }

    [Column(TypeName = "datetime2")]
    public DateTime? LastEdited { get; set; }

    [ForeignKey("MovieId")]
    public List<Movie> Movies = new List<Movie>();
}

And here is the code in my CustomerController class that should add the movie to my customer

[HttpGet]
public IActionResult AddMovie(int id)
{
    List<Movie> movieList = new List<Movie>();
    movieList = dbContext.Movies.ToList();

    AddMovieViewModel viewModel = new AddMovieViewModel()
    {
        movies = movieList,
        customer = dbContext.Customers
           .Where(s => s.CustomerId == id)
           .FirstOrDefault()
    };

    return View(viewModel);
}

[HttpPost]
public IActionResult AddMovie (int id,int cid)
{
    Customer customer = dbContext.Customers
        .Where(s => s.CustomerId == cid)
        .FirstOrDefault();
    Movie movie = dbContext.Movies
        .Where(s => s.MovieId == id)
        .FirstOrDefault();
    movie.CustomerId = customer.CustomerId;
    customer.Movies.Add(movie);

    dbContext.SaveChanges();
    return RedirectToAction("Index");
}

I hope someone can tell me whats wrong with my code.

Selim Yildiz
  • 5,254
  • 6
  • 18
  • 28
remco
  • 73
  • 9
  • And why do you think something is wrong with your code? Can you please tell us which compiler error or exception you're getting, as well as the line on which you are getting it? – Lasse V. Karlsen Mar 10 '20 at 12:29
  • Also, it seems you're loading an existing movie, changing the customer id on it, and then saving it back. This will not create a new movie, it will update the existing one. Could this be the problem? – Lasse V. Karlsen Mar 10 '20 at 12:30
  • In my view it shows a count of movies for a customer but its always 0 – remco Mar 10 '20 at 12:30
  • @LasseV.Karlsen Yes thats it but i try to make a new movie and add that one but I can't find a way to do that. – remco Mar 10 '20 at 12:32

2 Answers2

0

You are not creating new Movie, but searching through existing movies. So create new movie and assign a CustomerId:

Customer customer = dbContext.Customers
    .Where(s => s.CustomerId == cid)
    .FirstOrDefault();
Movie movie = new Movie();
movie.CustomerId = customer.CustomerId;
movie.Name = "My new movie";
movie.DateCreated = DateTime.Now;

dbContext.Customers.Add(customer);
dbContext.Movies.Add(movie);

dbContext.SaveChanges();

EntityFramework will insert a new entity.

StepUp
  • 36,391
  • 15
  • 88
  • 148
  • The MovieId of movie is 0 and it doesn't add it to the list. – remco Mar 10 '20 at 12:51
  • Hi, sorry for the late anwser but I get this sql error now "SqlException: Cannot insert explicit value for identity column in table 'Customers' when IDENTITY_INSERT is set to OFF." – remco Mar 11 '20 at 07:09
  • Okay what happens now is. A new movie is created but not added to a list and so now when i look at all movies its empty. – remco Mar 11 '20 at 07:23
  • Yes it does create a new one with a id but nothing else. – remco Mar 11 '20 at 07:40
  • Okay but this is not what it is supposed to do what it does now is just make a movie but not add it to the list of customer movies. – remco Mar 11 '20 at 07:58
  • @remco please, see my updated answer and see this post to solve your error [https://stackoverflow.com/questions/11173562/entity-framework-error-cannot-insert-explicit-value-for-identity-column-in-tabl](https://stackoverflow.com/questions/11173562/entity-framework-error-cannot-insert-explicit-value-for-identity-column-in-tabl) – StepUp Mar 11 '20 at 08:17
  • I found out what i need but i don't know how to do it. What i need is an extra table that contains both movieId and CustomerId. And also date when rented and when its returned. But I don't know how to add a extra table like that. – remco Mar 11 '20 at 13:54
  • 1
    @remco just create [a junction table](https://www.learnentityframeworkcore.com/configuration/many-to-many-relationship-configuration) – StepUp Mar 11 '20 at 13:58
  • I fixed it by making a many to many relation and in the junction table i also set the renting date and return date. – remco Mar 13 '20 at 11:13
  • @remco You are great!:) [https://stackoverflow.com/help/someone-answers](https://stackoverflow.com/help/someone-answers) – StepUp Mar 13 '20 at 11:20
0

I believe this is the model structure you're looking for

Customer

public class Customer
{

    [Key]
    public int CustomerId { get; set; }

    public string Name { get; set; }
    public string LastName { get; set; }
    public bool IsCreated { get; set; }
    public int MaxMovies { get; set; }

    [Column(TypeName = "datetime2")]
    public DateTime Created { get; set; }

    [Column(TypeName = "datetime2")]
    public DateTime? LastEdited { get; set; }

    public ICollection<Movie> Movies = new List<Movie>();

}

Movie

public class Movie
{

    [Key]
    public int MovieId { get; set; }

    public String Name { get; set; }

    [Column(TypeName = "datetime2")]
    public DateTime? DateCreated { get; set; }

    [ForeignKey("Customer")]
    public int? CustomerId { get; set; }

    public Customer Customer { get; set; }

}

Then when creating/editing a movie, simply assign the movie a CustomerId, you can then retrieve a customer's movies by accessing Customer.Movies

PeterT
  • 269
  • 1
  • 2
  • 9