entity framework - How do you insert a complex object into the database? -


Assume that these three tables are:

  - car - id 1 2 - Event- ID Carrier Event Type Date Date 1 1 2014-04-28 12:00:00 - Event Type - ID Name 1 "Entry Created" 2 "Entry Entry in Sales Chart" 3 "Entry Entry Entry Removed " 

How can the car object be added to the database, by using the Entity Framework (EF5 in my case)? I have tried several different ways to accomplish this, including the code below, but nothing has worked yet

car newCar = new car ( } {} Event newEvent = new event () {CarID = newCar.ID, EventTypeID = 2, DateTime added = Datetime.Now; } NewCar.Event.Add (newEvent); Context.Car.Add (newCar); Context.SaveChanges ();

Any support will be highly appreciated.


Edit: After an exception reference an attempt comes in the catch. Save changelog occurs (). The exception here is:

The INSERT statement is contradictory with the foreign key constraint "FK_Event_EventType". There is a conflict in the "cart system" database, the table "dbo.EventType", column 'id'

Note: There is a connection between these three tables, including events and event types. Can this be the issue? If so, then I have not figured out how to fix it.

The type of operation you want to change the order is:

  Context.Car.Add (newCar); NewCar.Event.Add (newEvent); Context.SaveChanges ();  

In this way the INSERT call for the car line (parent) will be released before the INSERT call for the event line (child), the foreign key must be avoided.


Comments

Popular posts from this blog

import - Python ImportError: No module named wmi -

Editing Python Class in Shell and SQLAlchemy -

c# - MySQL Parameterized Select Query joining tables issue -