Linq to sql OnLoaded () with SQL View?

I am trying to extend a Linq-to-Sql object with a few additional properties. These are "calculated" properties based on data from SQL View. For example, consider having a Date of Birth field, which is used to calculate the Extended Age field.

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

I get a compile-time error stating that I cannot create it. I checked the constructor code for my LTS entity class and does not have a partial definition for any of the expected extension points.

I checked several of my other LTS entity classes and they have these extension points. The only difference I see is that the one without it is loaded from SQL View, and not from the table. Is there a way to connect to the "Loaded" event when loading from an SQL view?

TIA!

+3
source share
2 answers

I found that I did not have the PrimaryKey specified for my Linq-to-Sql object class. I believe that without specifying the Primary Key, there are no extension methods generated in the entity class. As soon as I defined the main key in the definition of the LTS object class (via the constructor), I was able to extend the OnLoaded () event.

+4
source

. , . , , .

:

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

, (: 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

Source: https://habr.com/ru/post/1707455/


All Articles