8

I am trying to use the Extensibility Method Definitions from my datacontext.designer.cs file to do some validation.

So I created a new file and added this code:

public partial class LawEnforcementDataContext : System.Data.Linq.DataContext
{

    partial void InsertCourse(Course instance) // this definition copied from generated file
    {
        ValidateCourse(instance);
        this.ExecuteDynamicInsert(instance);
    }

    partial void UpdateCourse(Course instance) // this definition copied from generated file
    {
        ValidateCourse(instance);
        this.ExecuteDynamicUpdate(instance);
    }

    private void ValidateCourse(Course instance)
    {
        if (instance.StartDate > instance.EndDate)
        {
            throw new ApplicationException("Start date cannot be later than end date.");
        }
    }
}

I can't compile because of these errors:

Error   1   No defining declaration found for implementing declaration of partial method 'LawEnforcementDataContext.InsertCourse(reservation.lawenforcement.Course)'
Error   2   No defining declaration found for implementing declaration of partial method 'LawEnforcementDataContext.UpdateCourse(reservation.lawenforcement.Course)'

I don't see what I am doing wrong. I have done this before. Also, after adding the above code, in code that references the classes created by the LINQ to SQL, it complains that my entity types no longer exist. It's as if the partial class LawEnforcementDataContext is completely taking over the generated code.

EDIT

Here are the other halves of the partial declarations from the generated code:

    // the class
    public partial class LawEnforcementDataContext : System.Data.Linq.DataContext

    // the methods
    partial void InsertCourse(Course instance);
    partial void UpdateCourse(Course instance);
Ronnie Overby
  • 45,287
  • 73
  • 267
  • 346
  • Your edit showing the partial method definitions ruins my guess that they are missing. Another guess, do you have a Course class in multiple namespaces? Is reservation.lawenforcement.Course the correct class? – ScottS Mar 04 '09 at 18:20
  • No I only have 1 Course class. reservation.lawenforcement.Course is correct. – Ronnie Overby Mar 04 '09 at 18:23
  • Maybe the best thing to do is to distill this problem into something complete that we can see - can you remove as much code as possible to get this down to a small example of the problem? – Andrew Hare Mar 04 '09 at 18:30
  • I don't know what else to include. I'm starting to worry a bit here. Teamviewer? – Ronnie Overby Mar 04 '09 at 18:58
  • Unfortunately the answer to your question is that the compiler is unable to match up your partial method to its partner. The reason this is happening is impossible to tell from what you have posted - there must be something else that you can post that will give us some clues. – Andrew Hare Mar 04 '09 at 19:34

7 Answers7

14

Your two partial classes are defined in different namespaces, so the compiler does not 'share' them.

There is a setting under Properties in the DBML designer for this. Perhaps it reset?

leppie
  • 115,091
  • 17
  • 196
  • 297
4

In order to validate fields in Linq, you need to implement the OnValidate method not the Insert & Update methods.

For example:

partial void OnValidate(System.Data.Linq.ChangeAction action)
    {
        //All content items need titles
        if (Description == null || Description == "")
            throw new Exception("The description field is empty!");

        //Content types of image need...images
        if (ContentItemTypeId == (int)ContentItemTypes.Image && ImageData == null)
            throw new Exception("An image is required in order to save this content item!");

        //New Content Items don't have ids.  If a new one comes through, set the default values for it.
        if (this.ContentItemId == 0)
        {
            this.CreatedOn = DateTime.Now;
            this.LastUpdatedOn = DateTime.Now;
            this.IsDeletable = true;
        }
    }
rmw
  • 1,253
  • 1
  • 12
  • 20
1

Remove the partial keyword from your methods - the generated class does not have any partial methods.

Edit: Partial methods only work when they are defined like this:

partial class Foo
{
    partial void foo();
}

partial class Foo
{
    partial void foo() { }
}

One of the declarations needs to be written like it is an abstract method or interface method declaration. If the compiler finds one partial method with an implementation and cannot find the matching partial method declaration elsewhere it generates this error.

Edit: Here is something to check - is it possible that the parameter Course is not the exact same type in one of the two declarations? In other words is it possible that something like this has happened:

partial class Foo
{
    partial void foo(Biz.Parameter p);
}

partial class Foo
{
    partial void foo(Baz.Parameter p) { }
}

namespace Baz
{
    class Parameter { }
}

namespace Biz
{
    class Parameter { }
}
Andrew Hare
  • 344,730
  • 71
  • 640
  • 635
  • I referenced many other projects where I have done the same thing I am doing here. I included the partial keyword in the past and they are all working great. When I removed the partial keyword in this example, it just brought about some more errors. – Ronnie Overby Mar 04 '09 at 18:08
  • Can you post the other halves of the partial methods from the generated file? – Andrew Hare Mar 04 '09 at 18:11
  • The first error it complains about after removing the partial keyword is: 'System.Data.Linq.DataContext' does not contain a constructor that takes '0' arguments --- Like I said, it's as if the new class is hiding the generated one. – Ronnie Overby Mar 04 '09 at 18:11
  • There are no other classes with the name Course in any other namespaces. – Ronnie Overby Mar 04 '09 at 18:58
0

Ronnie, I just ran into this same issue. Caveat: Be cautious when you have multiple classes in one entity file.

Check where the partial class located: Did you accidentally put the definition inside the data context brackets like I did?

MAbraham1
  • 1,717
  • 4
  • 28
  • 45
0

To clarify, it's the class that's partial, not the methods within it - the DataContext class generated by L2S is a partial class but doesn't contain any partial methods ** correction below **.

For clarification on the difference of defining and implementing partial method declarations, this might help.

Edit

Well I never - I'd not seen/used "#region Extensibility Method Definitions" methods before... you learn something every day! Anyway, the article I linked to is a useful discussion on partial methods in general, separate from L2S.

Duckboy
  • 572
  • 3
  • 10
  • Actually in the generated code there is a region block called Extensibility Method Definitions where it defines a ton of partial methods (insert, update, delete for every table) – Ronnie Overby Mar 04 '09 at 18:16
  • Yes, thanks Ronnie - I noticed that and mentioned it in my edit yesterday, at about the same time you left this comment :) – Duckboy Mar 05 '09 at 15:37
  • So, ahem, any chance of a +1 for being humble admitting my error and adding a correction? Or do I get a -1 for having the temerity to ask for a +1, resulting in a zero net effect? Just wondering how things work around here! – Duckboy Mar 05 '09 at 15:40
0

That error means that the partial method your are implementing has not been defined in the LawEnforcementDataContext class.

These methods should be automatically defined when you add tables into your DataContext, look in the generated source (probably LawEnforcement.designer.cs) for a region inside the LawEnforcementDataContext class labelled #region Extensibility Method Definitions all the partial methods will be defined here. I expect the methods will be missing, try removing and re-adding the Course table in your Linq model to generate them.

ScottS
  • 8,455
  • 3
  • 30
  • 50
0

So, I was using a source generator when I came across this error.

The issue was that in the source code of the generator, I needed to add the namespace.

string source = $@" namespace ORMSoda.Tests.TestProgram {{ public static partial class Program {{ static partial void HelloFrom(string name) {{ System.Console.WriteLine($""Generator says: Hi from '{{name}}'""); }} }} }} ";

mattylantz
  • 312
  • 4
  • 10