我正在尝试创建一个 LINQ to SQL 对象列表,我将其添加到数据上下文中并稍后插入。当我调用 SubmitChanges() 时,虽然我收到一条错误消息,指出 WeatherForecast 对象的 Postcode 外键为空。
我似乎在调用 List.Add() 时不会对依赖对象进行深层复制。有没有办法让它做一个深拷贝?
这是我正在尝试做的一个例子。
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;
}