I'm using EF Core 2.2 with ASP.net MVC Core 2.2.
I have an entity with a List array that is not being stored to the table. I thought that it would write out a list of foreign keys to the objects in the array, but that's not happening. What am I forgetting?
public class ProjectModel
{
[Key]
public int ID { get; set; }
public List<MyObject> ListOfObjects { get; set; }
}
public class MyObject
{
[Key]
public int ID { get; set; }
[Required]
public string Name{ get; set; }
}
Code where add and save is occurring:
MyObject cm = MethodToGenerateObject();
if(pm.ListOfObjects == null)
{
pm.ListOfObjects = new List<MyObject>();
}
pm.ListOfObjects.Add(cm);
_context.Entry(pm).State = EntityState.Modified;
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
throw;
}
But no MyObject data is saved to the database. Why?
pm.ListOfObjects.Add(cm);
you should do something like_context.yourDatabaseTableNameInContext.AddRangeAsync(listOfObject)