none
Passing derived class to InsertOnSubmit RRS feed

  • Question

  •  

    Hi All,

     I used a designer to create a DB context which includes a table i dropped in the designer to create a model class.

    I then extended the class to add additional fields that i could use in the UI.

    While calling the InsertOnSubmit passing the derived calss i get an error that indicates Object reference not initialised exception and the stack trace message is :at System.Data.Linq.Table`1.InsertOnSubmit(TEntity entity)

     

    When i use the base class(the one generated by the designer) it works well, when i pass a derived class typecasted to the base class, it gives the error. Why?

     

    Eg:

     

    class A\\build by the designer

    {

    }

     

    class B : A

    {

    }

     

    pulbic static main()

    {

    ..

    B b = new B();

    saveEntitiy(b);

    ...

    }

     

    void saveEntity(A a)

    {

    dbcontext.As.InsertOnSubmit(a);//throws Object reference not initialised exception

    }

     

    Thanks,

    Preetham

    Monday, June 9, 2008 6:20 PM

Answers

  • Thank y'all for responding. I figured out the solution. I had to call the base class constructor from my derived class constructor. Since the base class is created by the designer, i found that it performs a lot of initializations(i dont know if that is the right way).

     

    Thursday, June 12, 2008 4:10 AM

All replies

  • I am trying to narrow the scope of your problem.

     

    Can you make sure that the attributes on the base class are appropriately set for derived mapping. [InheritanceMapping(Code="...", Type=typeof(derived))] . Also have you set the discriminator column in the mapping?

    Monday, June 9, 2008 7:20 PM
  • As Deepti says if you want to use derived classes you need to specify the mapping by specifying a field in the underlying base class to be used as a discriminator property and then to specify what value should be present in this field for each subclass.

     

    The easiest way to do this is to use the designer and perform the following steps:

    1. Right-click designer workspace and choose Add > Class
    2. Rename the new class to something useful
    3. Rick click existing underlying class and choose Add > Inheritence
    4. Chose the new class in the derived class drop-down and press OK
    5. Right-click on the association line between the classes and choose Properties
    6. Map the Discrimnator Property to the field that shall be used to determine when to subclass
    7. Type the value this property should contain to create the subclass into Derived Class Discriminator
    8. Type the value this property should contain for the base class into Base Class Discriminator

    [)amien

    Monday, June 9, 2008 10:40 PM
    Moderator
  • Thank y'all for responding. I figured out the solution. I had to call the base class constructor from my derived class constructor. Since the base class is created by the designer, i found that it performs a lot of initializations(i dont know if that is the right way).

     

    Thursday, June 12, 2008 4:10 AM