Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ to SQL - Compile error when extending data context with partial class and methods

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);
like image 261
Ronnie Overby Avatar asked Mar 04 '09 18:03

Ronnie Overby


2 Answers

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?

like image 132
leppie Avatar answered Oct 12 '22 10:10

leppie


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;
        }
    }
like image 27
rmw Avatar answered Oct 12 '22 11:10

rmw