使用SQL View查看linq到sql OnLoaded()?

时间:2022-12-22 09:06:48

I am trying to extend my Linq-to-Sql entity with a few extra properties. These are "calculated" properties based on data from the underlying SQL View. For example, think of having a Date of Birth field, which is used to calculate an extended Age field.

我试图用一些额外的属性扩展我的Linq-to-Sql实体。这些是基于来自基础SQL视图的数据的“计算”属性。例如,考虑具有出生日期字段,该字段用于计算扩展的年龄字段。

I tried to extend my entity class by extending the OnLoaded() method.

我试图通过扩展OnLoaded()方法来扩展我的实体类。

I get a compile time error however stating that I cannot create it. I checked the designer code for my LTS entity class, and it doesn't have a partial definition for any of the expected extension points.

我得到一个编译时错误,但声明我无法创建它。我检查了我的LTS实体类的设计器代码,它没有任何预期扩展点的部分定义。

I checked a few of my other LTS entity classes and they do have these extension points. The only difference I see is that the one without is loaded from a SQL View, rather than a table. Is there a way to hook into a "Loaded" event when loading from a SQL View?

我检查了一些其他LTS实体类,他们确实有这些扩展点。我看到的唯一区别是没有从SQL视图而不是表加载。从SQL视图加载时有没有办法挂钩到“已加载”事件?

TIA!

2 个解决方案

#1


I found that I did not have a PrimaryKey specified for my Linq-to-Sql entity class. I believe without a Primary Key specified, no extension methods generated in the entity class. Once I specified a Primary Key on my LTS entity class definition (through the designer), I was able to extend the OnLoaded() event.

我发现我的Linq-to-Sql实体类没有指定PrimaryKey。我相信没有指定主键,实体类中没有生成扩展方法。一旦我在LTS实体类定义上指定了一个主键(通过设计器),我就能够扩展OnLoaded()事件。

#2


You can do this by means of a property. Just create a partial class with the same name as your entity. Any properties or methods that you add will automatically be part of the entity and allow to use any of its members.

您可以通过财产来做到这一点。只需创建一个与您的实体同名的部分类。您添加的任何属性或方法将自动成为实体的一部分,并允许使用其任何成员。

Here's an example of the pattern:

这是模式的一个例子:

public partial class [The Name of the Entity]
{
   public int Age
   {
      get
      {
         return CalculateAge(this.DateOfBirth);
      }
   }
}

Here's some logic on how to calculate the Age (Source: Geekpedia)

以下是关于如何计算年龄的一些逻辑(来源:Geekpedia)

public static int CalculateAge(DateTime BirthDate)
{
    int YearsPassed = DateTime.Now.Year - BirthDate.Year;
    // Are we before the birth date this year? If so subtract one year from the mix
    if (DateTime.Now.Month < BirthDate.Month || 
          (DateTime.Now.Month == BirthDate.Month && DateTime.Now.Day < BirthDate.Day))
    {
        YearsPassed--;
    }
    return YearsPassed;
}

#1


I found that I did not have a PrimaryKey specified for my Linq-to-Sql entity class. I believe without a Primary Key specified, no extension methods generated in the entity class. Once I specified a Primary Key on my LTS entity class definition (through the designer), I was able to extend the OnLoaded() event.

我发现我的Linq-to-Sql实体类没有指定PrimaryKey。我相信没有指定主键,实体类中没有生成扩展方法。一旦我在LTS实体类定义上指定了一个主键(通过设计器),我就能够扩展OnLoaded()事件。

#2


You can do this by means of a property. Just create a partial class with the same name as your entity. Any properties or methods that you add will automatically be part of the entity and allow to use any of its members.

您可以通过财产来做到这一点。只需创建一个与您的实体同名的部分类。您添加的任何属性或方法将自动成为实体的一部分,并允许使用其任何成员。

Here's an example of the pattern:

这是模式的一个例子:

public partial class [The Name of the Entity]
{
   public int Age
   {
      get
      {
         return CalculateAge(this.DateOfBirth);
      }
   }
}

Here's some logic on how to calculate the Age (Source: Geekpedia)

以下是关于如何计算年龄的一些逻辑(来源:Geekpedia)

public static int CalculateAge(DateTime BirthDate)
{
    int YearsPassed = DateTime.Now.Year - BirthDate.Year;
    // Are we before the birth date this year? If so subtract one year from the mix
    if (DateTime.Now.Month < BirthDate.Month || 
          (DateTime.Now.Month == BirthDate.Month && DateTime.Now.Day < BirthDate.Day))
    {
        YearsPassed--;
    }
    return YearsPassed;
}