Laszlo Boke
Laszlo Boke

Reputation: 1329

Extending EF Class with joined properties

I want to extend my EF class with additional property that would come from entity navigation property.

Simple example from Northwind database. Territories table has "TerritoryID", "TerritoryDescription" and "RegionID" column (the foreign key to Region table) I want to add "RegionDescription" property to my entity class also.

So I created a partial class, but how to fill the RegionDescription property?

public partial class Territory
{
    public string RegionDescription { get; set; }

    partial void OnRegionIDChanging(int value)
    {
    }

    partial void OnRegionIDChanged()
    {
    }
}

Maybe in one of the events, my first thought was to use the "Region" navigation propety of the Territory entity class ( RegionDescription = this.Region.RegionDescription) but that is null when the events fired.

Upvotes: 0

Views: 119

Answers (1)

Jayantha Lal Sirisena
Jayantha Lal Sirisena

Reputation: 21366

You can do like this,

public partial class Territory
{
    public string RegionDescription
    {
        get { return Region.Description; }
        set { Region.Description = value; }
    }

    partial void OnRegionIDChanging(int value)
    {
    }

    partial void OnRegionIDChanged()
    {
    }
}

Upvotes: 2

Related Questions