I got an entity “LICENSE” that contains some relationsships, including three 1..* relationships.
I’m working on a GUI + Manager to allow to edit the entity and save it again, later.
When I call the save event on a button I overwrite the properties and relationships of my pre loaded entity I’m editing by reading the values from my controls.
I’m changing three relationships by loading the new selected entity with the same ObjectContext and overwrite the relationship itself. in 2 of 3 cases it works perfectly. even in the last case, the relationship is changed (another entity is stored) successful and the saving process could be done very well. However, in the third case there is a weird behaviour I don’t understand neither know how to fix it:
When I change the entity of the relationship, it overwrites the ID of my LICENSE object, that makes no sense at all.
Here is the code:
GUI:
private LICENSE lizenz { get; set; } // in the load event, this object will be filled correctly
private string form2obj() // this method is getting called in my button save event
{
//...
// I removed some conditions (they all successed on testing, so believe my controls are set right on testing this
this.lizenz.ADRESSE_KONTAKTE_Lieferant = LizenzManager.LoadContact(Convert.ToInt32(this.ddLieferantAnsp.SelectedValue)); // ADRESSE_KONTAKTE_Lieferant is a 1..* relationship, this.ddLieferantAnsp.SelectedValue contains the ID of the object I want to load into the relationship
this.lizenz.ADRESSE_KONTAKTE_Betreiber = LizenzManager.LoadContact(Convert.ToInt32(this.ddBetreiberAnsp.SelectedValue)); // ADRESSE_KONTAKTE_Betreiber is a 1..* relationship, this.ddBetreiberAnsp.SelectedValue contains the ID of the object I want to load into the relationship
this.lizenz.DB_TYPES = LizenzManager.LoadDatabaseType(Convert.ToInt32(this.ddDatenbanktyp.SelectedValue)); // DB_TYPES is a 1..* relationship, too, this.ddDatenbanktyp.SelectedValue contains the ID of the object I want to load into the relationship
// last line overwrites this.lizenz.ID with the value of Convert.ToInt32(this.ddDatenbanktyp.SelectedValue), this is wrong
//...
}
BLL (LizenzManager):
// context is the same ObjectContext in all three calls!
//...
public static ADRESSE_KONTAKTE LoadContact(int id)
{
return context.ADRESSE_KONTAKTE.Where(x => x.ID == id).Single(); // this seems to success
}
public static DB_TYPES LoadDatabaseType(int id)
{
return context.DB_TYPES.Where(x => x.ID == id).Single(); // this works, but it overwrites the ID of my LICENSE object I load this into ...
}
//...
As you see, the loading ways are pretty much the same, both tables got the ID column, but the last relation overwrites this.lizenz.ID with the ID of the DB_TYPES entity I load.
Could you explain me, how to fix this issue?
Ok, this was a very tiny and stupid issue.
When I was creating the relationship in the SQL Management Studio before, I forgot to set the correct foreign key in my LICENSE table. So it was still on ID and forced to overwrite the ID of my license entity when changing the relationship.
Thanks to all people who took the time to read this question.