Skip to content

Entity Framework ForeignKeyAttribute [Mapping]

Victor Tomaili edited this page May 3, 2021 · 1 revision

(from the official guide)

[namespace: Serenity.Data.Mapping] - [assembly: Serenity.Data]

This attribute is used to specify foreign key columns, and add information about primary table and primary field that they are related to.

public class CustomerRow : Row
{
   [ForeignKey("Countries", "Id")]
   public string CountryId
   {
      get { return Fields.Firstname[this]; }
      set { Fields.Firstname[this] = value; }
   }
}

Here we specified that CountryId field in Customer table has a foreign key to Id field in Countries table.

The foreign key doesn't have to exist in database. Serenity doesn't check it.

Serenity can make use of such meta information, even though it doesn't affect generated queries alone.

ForeignKey is more meaningful when used along with the next attribute we'll see. [LeftJoinAttribute](LeftJoinAttribute [Mapping])

Clone this wiki locally