LINQ InsertOnSubmit: NullReferenceException

前端 未结 6 2008
野的像风
野的像风 2020-12-06 16:22

I have this code:

using DC = MV6DataContext;
using MV6; // Business Logic Layer
// ...

public DC.MV6DataContext dc = new DC.MV6DataContext(ConnectionString)         


        
6条回答
  •  暖寄归人
    2020-12-06 17:18

    I had a slightly different situation than the asker, but got the same error for the same reasons. I wrote new constructors in a partial class for my database entities, then tried to use the resulting objects in InsertOnSubmit calls.

    None of these answers helped me directly, but I was able to figure out what they were getting at after reading all of them.

    The auto-generated parameterless constructor for the entity does things that need to happen for InsertOnSubmit to work, so if you overload the constructor -- like me -- or inherit from the class -- like the asker -- you need to call the base constructor from your new constructor, like so:

    public partial class Entity {
        public Entity( Type parameter ) : this() {
            // do things with the parameter
        }
    }
    

    or

    public class SubEntity: Entity {
        public SubEntity( Type parameter ) : base() {
            // do things with the parameter
        }
    }
    

提交回复
热议问题