I have an MVC3 Project in C#.Net. I have Object1 with a list of Object2…
//navigation Property(s)
public virtual ICollection<Object2> Object2s { get; set; }
Object2 has an Object1 property and an Object1_Id property
public int Object1_Id { get; set; }
public virtual Object1 Object1 { get; set; }
When I run the Index View for Object2 I get the message “Invalid column name ‘Object1_Id1′”. Weird….it’s looking for an automated column. When I remove the Object1_Id from Object2, it does fine until I go to Save in the Create View. It then throws an error message:
{"Cannot insert the value NULL into column 'Object1_Id', table 'dbo.Object2'; column does not allow nulls. INSERT fails.\r\nThe statement has been terminated."}
I’m sure I’m just missing an easy step. Any ideas?
I figured out my question. When I call the Create post action on my Object2 Controller, I retrieve the parent Object1, create a Guid for my new Object2, add the new Object2 to the Object1.Object2s collection, set Object1 state to modified, then save the db: