InsertOnSubmit throwing NullReferenceException

Be careful when you are using your own constructor in partial classes on entities generated for LINQ to SQL (Data context entities). When you create your own constructor is really important to call the default constructor in it. Example :

Default contructor on LINQ to SQL entities in partial class


Why? Because is important to execute the default generated constructor while execution of your program. This constructor contains some Initialization under your entity:

Generated contructor of LINQ to SQL object
Otherwise you will getting following error:

[NullReferenceException: Object reference not set to an instance of an object.]
   System.Data.Linq.Mapping.EntitySetDefSourceAccessor`2.GetValue(T instance) +18
   System.Data.Linq.Mapping.MetaAccessor`2.GetBoxedValue(Object instance) +56
   System.Data.Linq.StandardTrackedObject.HasDeferredLoader(MetaDataMember deferredMember) +128
   System.Data.Linq.StandardTrackedObject.get_HasDeferredLoaders() +130
   System.Data.Linq.StandardChangeTracker.Track(MetaType mt, Object obj, Dictionary`2 visited, Boolean recurse, Int32 level) +269
   System.Data.Linq.StandardChangeTracker.Track(Object obj, Boolean recurse) +113
   System.Data.Linq.Table`1.InsertOnSubmit(TEntity entity) +383