I have the following tables in an SQL2008 database:
This table has more columns but i have removed them here for simplicity.
As you may get, AccommodationAttributes describes the many to many relationship between Accommodations and Attributes.
I have created my model (EF5) using database first, and it has created two classes linked with a navigation property.
All this seems correct.
What i am trying to do is add values in the db, but though i am able to add Accommodations and Attributes, i don't seem to be able to make it add the corresponding values in the AccommodationAttributes table.
I am reading from an XML file.
Bellow is a code sample of what i am trying to do.
var Accommodations = from a in xe.Elements("accommodation") select a;
foreach (var accommodation in Accommodations)
var AccCode = accommodation.Element("code").Value;
Accommodation a = context.Accommodations.Where(x=>x.code == AccCode).SingleOrDefault();
bool newAccommodation = a == null ? true : false;
a = !newAccommodation ? a :
code = accommodation.Element("code") == null ? null : accommodation.Element("code").Value,
name = accommodation.Element("name") == null ? null : accommodation.Element("name").Value
foreach (var attribute in accommodation.Elements("attributes").Elements("attribute").Select(x=>x.Value))
Attribute at = context.Attributes.Where(x => x.name == attribute).SingleOrDefault();
After running this code i Run the following in SQL:
select COUNT(*) from Accommodations
select COUNT(*)from Attributes
select COUNT(*)from AccommodationAttributes
But though i see entries in the two tables, the link table comes with 0 rows.
I have tried other variations, like attaching the objects to the context, or implicitly specifying that it is a modified object.
By the time that this code will run i am sure that the Attributes are allready inserted in the db, but the Accommodation is either an Insert or Update.
I am eager to hear your thoughts,