Skip to content

Instantly share code, notes, and snippets.

@robsonfaxas
Created June 11, 2021 03:43
Show Gist options
  • Save robsonfaxas/0f6e3dd443879acd7a99c28963903cfc to your computer and use it in GitHub Desktop.
Save robsonfaxas/0f6e3dd443879acd7a99c28963903cfc to your computer and use it in GitHub Desktop.
[LinqToSql - Mapeamento entre entidades] Como fazer o mapeamento entre entidades com #linq
No LinqToSql, para mapear um relacionamento entre entidades, vamos usar o exemplo do relacionamento Category e SubCategory.
/*########################################################*/
1 - A subcategory que contém a chave estrangeira, deve conter um objeto "EntityRef<ProductCategory>" privado para ser armazenado a referência e um "Association" public, cujos Get e Set do Association apontam para o EntityRef.
/*########################################################*/
private EntityRef<ProductCategory> _ProductCategory;
[Association(Storage = "_ProductCategory", ThisKey = "ProductCategoryID", IsForeignKey = true)]
public ProductCategory ProductCategory
{
get { return this._ProductCategory.Entity; }
set { this._ProductCategory.Entity = value; }
}
/*########################################################*/
2 - A Category precisa informar um EntitySet que é a coleção de SubCategory
/*########################################################*/
[Association(OtherKey = "ProductCategoryID")]
public EntitySet<ProductSubcategory> ProductSubcategories { get; set; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment