Thursday, 15 May 2014

c# - Cascade on delete - Complex type mapped to a table -



c# - Cascade on delete - Complex type mapped to a table -

please consider next relationship:

car (entity) 1 ----------------------> * wheels (entity)

wheel (entity) 1 --------->1 nut (complex type)

wheel (entity) 1 --------->1 rim (complex type)

both nut , rim complex types mapped tables named nuts , rims. using wheel id nuts , rims primary key.

now, when attempting delete auto using code, next exception:

system.invalidoperationexception: operation failed: relationship not changed because 1 or more of foreign-key properties non-nullable. when alter made relationship, related foreign-key property set null value. if foreign-key not back upwards null values, new relationship must defined, foreign-key property must assigned non-null value, or unrelated object must deleted. @ system.data.entity.core.objects.objectcontext.preparetosavechanges(saveoptions options) @ system.data.entity.core.objects.objectcontext.savechangesinternal(saveoptions options, boolean executeinexistingtransaction) @ system.data.entity.core.objects.objectcontext.savechanges(saveoptions options) @ system.data.entity.internal.internalcontext.savechanges() @ system.data.entity.internal.lazyinternalcontext.savechanges() @ system.data.entity.dbcontext.savechanges()

when attempting in ms- ssms, error: delete statement conflicted reference constraint "fk_dbo.nuts_dbo.wheel_id". conflict occurred in database "databasename", table "dbo.nuts", column 'id'.

given, complex types required parameters entity , have one-to-one relationship, why wouldn't cascade delete on default in scenario? second, how should go deleting auto , related many wheels along associated nuts , rims. lastly, in case, auto has thousands of wheels. thought in code or utilize stored procedures?

thanks.

as ben robinson mentions in comment, purpose of classes attributed [complextype] have properties mapped columns within table of containing type. there should no nut or rim tables. if isn't desired behaviour shouldn't complex types.

let's assume shouldn't complex types , work out why not cascade deleting. concentrating on nut, , assuming not using fluent configuration, suspect need have navigation property wheel on nut, wheelid. wheelid marked [key] , [foreignkey(wheel)], think. main point navigation property.

with fluent configuration have navigation property wheel , then, in configuration wheel, have:

hasrequired(wheel => wheel.nut) .withrequireddependent(nut => nut.wheel);

another point note cascade delete, if isn't generated taste, can modified within migration in foreign key creation via method argument.

finally, reply question batch delete: cascade delete done in database if delete auto , cascade deletes wheel, , should fast. if want perform deletes of many wheels auto separately have @ entity framework extensions: http://efe.codeplex.com/

c# entity-framework ef-code-first domain-driven-design entity-framework-6

No comments:

Post a Comment