To remove an owned entity from the parent and delete it from the database, set the parent's navigational property for that owned entity to null. You must use OwnedEntity() in the mapping for the removal to be processed.
var node = new TestNode
{
Title = "Parent Node",
OneToOneOwned = new OneToOneOwnedModel
{
Title = "Owned Entity"
}
};
using (var context = new TestDbContext())
{
context.Nodes.Add(node);
context.SaveChanges();
}
// Remove the owned entity by setting it to null
node.OneToOneOwned = null;
using (var context = new TestDbContext())
{
context.UpdateGraph(node, map => map
.OwnedEntity(p => p.OneToOneOwned));
context.SaveChanges();
}
var node = new TestNode
{
Title = "Parent Node",
OneToOneOwned = new OneToOneOwnedModel
{
Title = "Owned Entity"
}
};
using (var context = new TestDbContext())
{
context.Nodes.Add(node);
context.SaveChanges();
}
// Simulate detach
node.OneToOneOwned = null;
using (var context = new TestDbContext())
{
// Setup mapping
context.UpdateGraph(node, map => map
.OwnedEntity(p => p.OneToOneOwned));
context.SaveChanges();
}