, , ,

DexExpress XPO Code First makes sure you spend a minimum amount of time in the Database, because you can manage the structure of your data solely in code. This is one of the stronger points of XPO compared to other Object-Relational Mapping frameworks.

However, there are some cases in which changes in code are not persisted in the database. This is unfortunate, because it weakens the Code First approach. The case of this article is: if you remove the inheritance of one object to the other in code, the Foreign Key relation in the database is not removed.

Database XPO foreign keys

In the following code (which is simplified for the purpose of being an example), I defined an object named TestObject, and an object which is inherited from it, named InheritedObject. I gave it a MapInheritanceType of OwnTable, which results in a Foreign Key relation between the two tables.

public class TestObject
	[DevExpress.Xpo.Key(AutoGenerate = true)]
	public int ID { get; set; }

public class InheritedObject

However, when I remove this inheritance relation in code, in the database the Foreign Key is not removed. That means that the relation between the tables is persisted, and my table has a constraint that it should not have. If I try to insert an item in the InheritedObject table, that has an ID that is already present in the TestObject table, it will throw an error.

(This behavior is experienced in DevExpress 13.1)