I can’t currently understand why my update method in my repository for the style entity is throwing a NullReferenceException in my Linq to SQL domain model layer. The problem seems to be that the EntitySet<Product> Product property on the Style object that I am updating is null following the necessary use of a Detach method. The actual exception is thrown on the attach prior to the submission of changes.
My update is only meant to target the Style table, the Product reference should not really come into it, but the relationship is mapped in the code generated by SQLMetal, which is an important part of my application design.
If I delete the foreign key relationship, recreate the mapping class and adjust code accordingly, the problem goes away. I would however like to kep the forign key relationship if possible.
I will attempt to best explain the set up:
The relationship in the SQL server database is like so, with the Product table referencing the Style table:

The entity mapping class is created with SQLMetal, as far as I know, there is nothing unusual in the way this is generated.
The update method is:
public Style Update(Style style)
{
using (var dc = new ProjectERPDataContext(Config.ConnectionStringERPDB))
{
style.Detach();
dc.Style.Attach(style);
dc.Refresh(RefreshMode.KeepCurrentValues, style);
dc.SubmitChanges();
return style;
}
}
The detach method is kept in a partial class:
The detach method exists because without, the exception is thrown: “An attempt has been made to Attach or Add an entity that is not new, perhaps having been loaded from another DataContext. This is not supported.”
public partial class Style
{
public void DetachEntityRefs()
{
this._Product = default(EntitySet<Product>);
}
}
The entity class starts like:
[Table(Name="dbo.Style")]
public partial class Style : INotifyPropertyChanging, INotifyPropertyChanged
{
private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);
private int _ID;
private string _Name;
private EntitySet<Product> _Product;
So you can see exactly where the exception is thrown:

Full stack trace:
System.NullReferenceException : Object reference not set to an instance of an object.
at System.Data.Linq.Mapping.EntitySetDefSourceAccessor`2.GetValue(T instance)
at System.Data.Linq.Mapping.MetaAccessor`2.GetBoxedValue(Object instance)
at System.Data.Linq.ChangeTracker.StandardChangeTracker.StandardTrackedObject.HasDeferredLoader(MetaDataMember deferredMember)
at System.Data.Linq.ChangeTracker.StandardChangeTracker.StandardTrackedObject.get_HasDeferredLoaders()
at System.Data.Linq.ChangeTracker.StandardChangeTracker.Track(MetaType mt, Object obj, Dictionary`2 visited, Boolean recurse, Int32 level)
at System.Data.Linq.ChangeTracker.StandardChangeTracker.Track(Object obj, Boolean recurse)
at System.Data.Linq.Table`1.Attach(TEntity entity, Boolean asModified)
at ProjectERP.DomainModel.Repositories.SQLStyleRepo.Update(Style style) in SQLStyleRepo.cs: line 45
at ProjectERP.UnitTests.DomainModel.SQLStyleRepoTests.Test_can_update_style() in SQLStyleRepoTests.cs: line 67
I bet the cause is this timebomb you planted here:
default(EntitySet<Product>)isnull. Try setting it to something: