[C#][Linq to Sql] Ajout de données ?
Bonjour à tous,
Je me suis crée un projet et une db test pour me familiariser avec Linq.
Ma db est très simple :
- Article (ArticleId, ArticleCode, BrandId)
- Brand (BrandId, BrandLabel)
Voici mes 2 classes de mapping
Mapping sur la table Article
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
[Table(Name = "Article")]
class Article
{
private int m_ArticleId;
private string m_ArticleCode;
private int m_BrandId;
private EntityRef<Brand> m_Brand;
[Column(Storage = "m_ArticleId", IsPrimaryKey = true)]
public int ArticleId
{
get { return m_ArticleId; }
set { m_ArticleId = value; }
}
[Column(Storage = "m_ArticleCode")]
public string ArticleCode
{
get { return m_ArticleCode; }
set { m_ArticleCode = value; }
}
[Column(Storage="m_BrandId")]
public int BrandId
{
get { return m_BrandId; }
set { m_BrandId = value; }
}
[Association(Storage="m_Brand", ThisKey="BrandId")]
public Brand Brand
{
get { return m_Brand.Entity; }
set { m_Brand.Entity = value; }
}
} |
Mapping sur la table Brand
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
[Table(Name="Brand")]
class Brand
{
private int m_BrandId;
private string m_BrandLabel;
private EntitySet<Article> m_Article;
[Column(Storage = "m_BrandId", IsPrimaryKey=true)]
public int BrandId
{
get { return m_BrandId; }
set { m_BrandId = value; }
}
[Column(Storage = "m_BrandLabel")]
public string BrandLabel
{
get { return m_BrandLabel; }
set { m_BrandLabel = value; }
}
[Association(Storage="m_Article")]
public EntitySet<Article> Article
{
get { return m_Article; }
set { m_Article = value; }
}
} |
Mon DataContext fortement typé
Code:
1 2 3 4 5 6 7 8
|
class Db : DataContext
{
public Table<Brand> brand;
public Table<Article> article;
public Db(string connection) : base(connection) {}
} |
Comment je fais pour ajouter un enregistrement dans Article et dans Brand ?
Dans tous les tutos que je lis ça donne un truc du style :
- db.article.Add()
- db.brand.Add()
Sauf que je n'ai pas ces méthodes Add().
Au mieux j'ai db.brand.Article.Add() mais pour la table Article alors ?
ps : j'ai bien essayé avec ma classe générée par SqlMetal mais comme j'avais la même chose, j'ai voulu réecrire la classe au plus simple.