LINQ to SQL deep copy object with dependencies

I am trying to create a list of LINQ to SQL objects that I will add to datacontext and insert later. When I call SubmitChanges () even though I get an error indicating that the foreign key of the zip code for the WeatherForecast object is null.

It looks like when List.Add () is called, it doesn't do a deep copy of the dependent objects. Is there a way to do this to make a deep copy?

This is an example of what I am trying to do.

function List<WeatherForecast> CreateForecast(Postcode postcode) 
{ 
   var forecasts = List<WeatherForecast>();

   var wf = new WeatherForecast() 
   { 
      ForecastDate = DateTime.Today
      , CurrentTemperature = (decimal)today.Attribute(XName.Get("temperature"))
   };
   wf.Postcode = postcode;

   forecasts.Add(wf); 

   ...
   Keep adding forecast objects into the list
   ...

   return forecasts;
}

      

0


a source to share


1 answer


List.Add doesn't create a copy at all. Assuming WeatherForecast is a reference type, you simply keep the reference to the generated WeatherForecast.



The problem is probably because you are using a Postcode object that has not yet been added to the database.

0


a source







All Articles