NullReferenceException when doing InsertOnSubmit in LINQ to SQL

后端 未结 2 1990
旧巷少年郎
旧巷少年郎 2021-02-03 23:09

In my database I have a table called StaffMembers

when I bring this into my .net Project as through linq-to-sql an entity class StaffMember is created

Now I have

2条回答
  •  有刺的猬
    2021-02-03 23:53

    Alright I found my answer on http://social.msdn.microsoft.com/Forums/en/linqprojectgeneral/thread/0cf1fccb-6398-4f16-920b-adef9dc4ac9f

    in case some is still looking for an answer.

    This problem happens when you overload the constructor in the partial class, and not call the default constructor in it.

    The default constructor of the entity does few things thats required by the Context object.

    Hence if you have an overloading constructor in your partial class and using it to create the object, make sure the default constructor is called in the first line

    in C# you can do this by

    eg.

     Customer(string custID)
    

    you need to add a

     Customer(string custID):this()
    

    in C# where Customer is my class and Customer(string custID):this() is my overload constructor in my partial class.

提交回复
热议问题