0

My EF5 model

public Layout()
{
   public int Id {get;set}
   public BindingList<Column> Columns {get;set}
}
public Column()
{
   public int Id {get;set}
   public string Name {get;set}

   [Required]
   [ForeignKey("LayoutId")
   public virtual Layout Layout {get;set}
   public int LayoutId {get;set}

}

In my context I only need to specify

DBSet<Layout> Layouts {get;set}

I dont need to specify DBSet Columns for the database to create the way I want it with both tables.

Also I am happy in my code to only ever access columns via the layout object However i cant figure out how to persist the layout object with its columns correctly.

This question here describes how to save parent objects with children, but it would require me to create a DBSet Columns property in my context. Do I really have to do that?

Community
  • 1
  • 1
Kirsten
  • 15,730
  • 41
  • 179
  • 318

1 Answers1

0

It seems I can get by without having a DBSet in the context if instead of using

Context.Columns.Remove(col)

I use

Layout.Columns.Remove(col)
Context.Entry(col).State = EntityState.Deleted;
Kirsten
  • 15,730
  • 41
  • 179
  • 318