Question

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?

1
1
2/14/2019 7:47:57 PM

Popular Answer

Thanks to @Sam, I searched for navigation properties and found this helpful guide: https://www.learnentityframeworkcore.com/conventions/one-to-many-relationship

the first one to many relationship example helped solve my problem.

0
2/14/2019 8:20:45 PM


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