0

I would like to add a list of actor name list to a film.

Cinema class :

public int Id{get;set;}
public double Price{get;set;}
public ICollection<string> ActorName {get;set;}

So the problem is that the table's ActorName doesn't appear. I only have the id and the price. I also try to make a class Actor and Cinema. But I have the same result.


Cinema:

public int Id{get;set;}
public double Price{get;set;}
public ICollection<Actor> Actor{get;set;}

Actor :

public Id{get;set;}
public Name{get;set;}
Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
Jack
  • 33
  • 1
  • 5
  • Checkout this question: https://stackoverflow.com/questions/20711986/entity-framework-code-first-cant-store-liststring – Oggy Jul 30 '18 at 15:28
  • How would you expect the ActorName column to look like in the database? – Camilo Terevinto Jul 30 '18 at 15:29
  • Possible duplicate of [Entity Framework - Code First - Can't Store List](https://stackoverflow.com/questions/20711986/entity-framework-code-first-cant-store-liststring) – Camilo Terevinto Jul 30 '18 at 15:31
  • @CamiloTerevinto when I insert a list to ActorName I want see the list (collection) in my database. – Jack Jul 30 '18 at 15:34

1 Answers1

1

Change your collection like so:

  public ICollection<Actor> Actors { get; set; }

Make sure you have a class named Actor with the necessary properties (ID, ActorName, Price, etc...)

Another thing I just remembered...add a constructor to Cinema:

      Cinema:
      public int Id{get;set;}
      public double Price{get;set;}
      public ICollection<Actor> Actor{get;set;}

      public Cinema(){

         this.Actors = new List<Actor>();
      }

You can use any type of collection...List, IEnumerable, IQueryable, etc...

Eager Loading:

   _context.Cinema.Include("Actor").Where(c => c.ID == id && c.Actor.Any(a => a.ActorName == "Dick Richie")).ToList();      
  • I already make this. I don't see the collection in ma table of cinema... – Jack Jul 30 '18 at 15:34
  • Do you have lazy loading enabled? If not, then you'll have to include the collection in your query to see the list. –  Jul 30 '18 at 15:40
  • What is the lazy loading ? How I disabled ? – Jack Jul 30 '18 at 15:50
  • It loads collections automatically which in my opinion you shouldn't do. Eager loading is the opposite. There are properties in VS you can check or just put a break point inside the DbContext constructor and then check the settings using "this": https://learn.microsoft.com/en-us/aspnet/mvc/overview/getting-started/getting-started-with-ef-using-mvc/reading-related-data-with-the-entity-framework-in-an-asp-net-mvc-application –  Jul 30 '18 at 15:54
  • this.Configuration.LazyLoadingEnabled = false; What is Configuration ? it's not recognized. – Jack Jul 31 '18 at 10:02