views:

322

answers:

1

I have created a bass class in LinqToSql which has 2 sub-classes. I need to assign a different stored procedure to each of the sub-classes custom update methods. Doing this is fine, but I get an error of "Invalid object name 'xxx'".

e.g.

DataClasses1DataContext dc = new DataClasses1DataContext();
Class2 c2 = new Class2() { ID = 1, Name = "test", Type = "s" };
dc.Class1s.InsertOnSubmit(c2);
dc.SubmitChanges();

...Class2 inherits Class1 and Class2 has it's own custom INSERT method (which is what i want it to execute).

thanks.

SOLUTION: Bryan's answer gave me what I was after. Here is the code I added to the class behind the designer:

partial class DataClasses1DataContext
{
    partial void InsertClass1(Class1 instance)
    {
        instance.Insert(this);
    }
}

partial class Class1
{
    public abstract void Insert(DataClasses1DataContext dataContext);
}

partial class Class2
{
    public override void Insert(DataClasses1DataContext dataContext)
    {
        dataContext.InsertSP(((System.Nullable<int>)(ID)), Name);
    }

}

partial class Class3
{
    public override void Insert(DataClasses1DataContext dataContext)
    {
        dataContext.InsertSP2((System.Nullable<int>)(ID), Name);
    }
}

The InsertSP and InsertSP2 stored procs were dragged on using the designer, to save me the hassle of having to manually call the sp.

ALL CREDIT TO BRYAN FOR THIS ONE

+1  A: 

If this is what gets generated by the LINQ to SQL designer:

partial void InsertClass1(Class1 instance);

and you have an implementation in a partial class:

partial void InsertClass1(Class1 instance)
{
    ...call stored procedure...
}

you could instead have a virtual method in Class1 to represent that operation:

protected virtual void Insert(Class1 instance)
{
    ...call stored procedure...
}

and call it from Class1's partial method:

partial void InsertClass1(Class1 instance)
{
    Insert(instance);
}

and override it in Class2:

protected override void Insert(Class1 instance)
{
    ...call different stored procedure...
}

The only catch is that you will have to cast to Class2 in the overridden method, but it will always succeed.

Bryan Watts
I'm refering to the ORM designer and the insert/update/delete methods that you can use to specify which sp to run when SubmitChanges gets called. I think what you are saying is to basically do away with the SubmitChanges approach and write my own insert methods. Yes this is ok, but kind of defeats the purpose of using the ORM and means having to maintain my own methods instead of leaving it up to the auto-generator thingy. What I really hoped was that setting the insert method in the ORM on a sub-class would propergate up the heirachy, but it doesn't look like it does. Thanks though.
HAdes
That's not what I meant, actually. The calling code with InsertOnSubmit and SubmitChanges would remain exactly the same. I am suggesting a new method in the Class1 entity to get around the limitation of the designer. It would still be fully encapsulated within the entities. I understand the frustration about it not just working the way you have it, though.
Bryan Watts
Sorry, I think I got my own wires crossed. I looked over your solution again and with once I understood exactly what you were getting at it suddenly clicked. So I didn't need to rely on the designer, just use the datacontext's partial class (didn't realise i could do that either). I'll add what I did to my post. Many thanks Bryan, saved me bacon as I'm pushing for us to be using LinqToSql and LinqToEntities in future.
HAdes