C# LINQ to SQL - 使用分部类和方法扩展数据上下文时出现编译错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/611745/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 10:15:21  来源:igfitidea点击:

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

c#linqlinq-to-sqlooppartial-classes

提问by Ronnie Overby

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

我正在尝试使用我的 datacontext.designer.cs 文件中的可扩展性方法定义来进行一些验证。

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.

我不明白我做错了什么。我以前做过这个。此外,在添加上述代码后,在引用由 LINQ to SQL 创建的类的代码中,它抱怨我的实体类型不再存在。就好像分部类 LawEnforcementDataContext 完全接管了生成的代码。

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);

采纳答案by leppie

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?

DBML 设计器中的“属性”下有一个设置。也许它重置?

回答by Andrew Hare

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

partial从您的方法中删除关键字 - 生成的类没有任何部分方法。

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 Courseis not the exact same type in one of the two declarations? In other words is it possible that something like this has happened:

编辑:这里有一些东西要检查 -Course两个声明之一中的参数是否可能不是完全相同的类型?换句话说,有没有可能发生过这样的事情:

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 { }
}

回答by Duckboy

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 **.

澄清一下,它是部分的类,而不是其中的方法 - L2S 生成的 DataContext 类是一个部分类,但不包含任何部分方法 ** 下面 ** 更正。

For clarification on the difference of definingand implementingpartial 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.

好吧,我从来没有 - 我之前没有见过/使用过“#region Extensibility Method Definitions”方法……你每天都在学习一些东西!无论如何,我链接到的文章是对一般部分方法的有用讨论,与 L2S 分开。

回答by ScottS

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

该错误意味着您正在实现的部分方法尚未在 LawEnforcementDataContext 类中定义。

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.

当您将表添加到 DataContext 中时,应自动定义这些方法,在标记为 #region Extensibility Method Definitions 的 LawEnforcementDataContext 类中查找生成的源(可能是 LawEnforcement.designer.cs)中的区域,所有部分方法都将在此处定义。我希望这些方法会丢失,请尝试在 Linq 模型中删除并重新添加 Course 表以生成它们。

回答by rmw

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

为了验证 Linq 中的字段,您需要实现 OnValidate 方法而不是 Insert & Update 方法。

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;
        }
    }

回答by MAbraham1

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?

检查部分类所在的位置:您是否像我一样不小心将定义放在数据上下文括号内?