我正在尝试创建一个LINQ to SQL对象列表,我将添加到datacontext并稍后插入。当我调用SubmitChanges()虽然我收到一个错误,说WeatherForecast对象的Postcode外键为null。
我看来,当调用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;
}
答案 0 :(得分:0)
List.Add根本不制作副本。假设WeatherForecast是一个引用类型,您只需存储对刚刚创建的WeatherForecast的引用。
问题可能是您正在使用尚未添加到数据库的Postcode对象。