[Entity Framework] Insert avec clé étrangère
Bonjour,
Alors, j'utilise l'Entity Framework et l'ADO.NET Data Service pour la gestion de ma db.
Je n'arrive pas à insérer un tuple. A chaque coup il me sort une exception.
Voici l'exception :
http://img168.imageshack.us/img168/8...ceptionpx8.png
Voici le schéma simplifié :
http://img515.imageshack.us/img515/2749/schemaxx5.png
Je suis dans un projet Silverlight. Quand je lance l'application j'initialise les champs :
Code:
1 2 3
| this.Database = new JobHuntersExtEntities(new Uri("../WebDataService.svc", UriKind.Relative));
this.Database.SaveChangesDefaultOptions = SaveChangesOptions.Batch;
this.Database.MergeOption = MergeOption.AppendOnly; |
Ensuite toujours dans l'initialisation je lance un handler que j'utilise pour récupérer une variable de session venant d'un projet asp.net lié (enfin c'est pas très important ici) :
Code:
1 2 3 4 5 6 7 8 9 10
| this.CompanyId = new Guid(e.Result.ToString());
// Récupération de l'objet de l'entreprise
var queryCompany = (from c in this.Database.Companies.Expand("Ads")
where c.Id == this.CompanyId
select c) as DataServiceQuery<Company>;
queryCompany.BeginExecute(
(ar) => this.Company = queryCompany.EndExecute(ar).First<Company>(),
null); |
La valeur de this.Company est bien différente de null et ce à quoi je m'attends.
Quand je clique sur le bouton de mon formulaire pour ajouter le tuple à la db :
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
| private void UiButtonSubmit_Click(object sender, RoutedEventArgs e)
{
// Date de publication
DateTime publication;
if (this._uiDatePickerPublication.SelectedDate == null)
publication = DateTime.Now;
else
publication = (DateTime)this._uiDatePickerPublication.SelectedDate;
// Ajout de l'annonce
Ads ad = new Ads();
ad.Title = this._uiTextBoxTitle.Text;
ad.Description = this._uiTextBoxDescription.Text;
ad.DateCreation = DateTime.Now;
ad.DatePublication = publication;
ad.Validity = Int16.Parse(this._uiComboBoxValidity.SelectionBoxItem.ToString());
ad.Salary = Decimal.Parse(this._uiSalary.Text);
ad.ContractType = (ContractType)this._uiComboBoxType.SelectedItem;
ad.Company = this.Company;
this.Database.AddToAds(ad);
this.Database.AddLink(ad, "Company", this.Company);
this.Database.BeginSaveChanges(SaveChangesOptions.Batch, (asyncResult) =>
{
try
{
this.Database.EndSaveChanges(asyncResult);
MessageBox.Show("Ok");
}
catch (Exception ex)
{
MessageBox.Show(ex.InnerException.Message);
}
}, null);
} |
L'exception est lancée dans le BeginSaveChanges.
J'ai essayé sans et avec le this.Database.AddLink(ad, "Company", this.Company); mais le résultat est le même, il me balance l'exception.
Est ce que quelqu'un sait comment faire ? Surement un problème avec l'objet this.Company qui n'a pas les bonne référence mais je ne vois pas comment faire autrement.
Yoshio.