Deleting an object that has an unloaded child object

I need some cleansing of the recascade in the Entity Framework and, despite searching for the clock, no closer to the solution.

In a nutshell, if I have an object (MyObject) that has a relationship of 1-> 0..1 with a child (MyObjectDetail), then I cannot delete MyObject without an exception with an UpdateException with restrictions - but ONLY IF the child MyObjectDetail file does not load in memory.

System.Data.UpdateException: A relationship is being added or deleted from an AssociationSet 'FK_MyObjectDetail_MyObject'. With cardinality constraints, a corresponding 'MyObjectDetail' must also be added or deleted.

If the child is loaded (for example, MyObject.MyObjectDetailReference.Load () before deleting MyObject), then it works fine.

My problem, of course, is that I don’t want (read: I can’t) load the varbinary field from MyObjectDetail every time I want to delete the parent MyObject. Is this possible, or will I have to crack some manual SQL calls to make this work?

In SQL Server:

MyObject [PK: Id <int>] -> MyObjectDetail [PK: MyObjectId <int>, Data <varbinary>]

The relationship is set to Cascade to update and delete the relationship.

In EF Designer:

MyObject [1] -> [0..1] MyObjectDetail

In EF XML:

SSDL:

        <Association Name = "FK_MyObjectDetail_MyObject">
          <End Role = "MyObject" Type = "MyEntities.Store.MyObject" Multiplicity = "1">
            <OnDelete Action="Cascade" />
          </End>
          <End Role="ObjectDetail" Type="MyEntities.Store.ObjectDetail" Multiplicity="0..1" />
          <ReferentialConstraint>
            <Principal Role="MyObject">
              <PropertyRef Name="Id" />
            </Principal>
            <Dependent Role="ObjectDetail">
              <PropertyRef Name="MyObjectId" />
            </Dependent>
          </ReferentialConstraint>
        </Association>

CSDL:

     <Association Name="FK_MyObjectDetail_MyObject">
          <End Type="MyEntities.MyObject" Role="MyObject" Multiplicity="1">
            <OnDelete Action="Cascade" />
          </End>
          <End Type="MyEntities.MyObjectDetail" Role="ObjectDetail" Multiplicity="0..1" />
          <ReferentialConstraint>
            <Principal Role="MyObject">
              <PropertyRef Name="Id" /></Principal>
            <Dependent Role="ObjectDetail">
              <PropertyRef Name="MyObjectId" />
            </Dependent>
          </ReferentialConstraint>
      </Association>

(The <OnDelete> CSDL , http://codepolice.net/2008/12/16/cascade-delete-in-entity-framework/)

- , v1 EF?

+3
3

, , myObject.myPropertyReference = new EntityKey .. - :

myObject.myPropertyReference.EntityKey = EFExtensions.GetEntityKey(, "myObjectID", myObject.myObjectID);

EFExtension.. , EntityKey .

, !

+2

EF , .

, . EF , . , , , ObjectContext , , , .

, , , EF , , .

. 33 - .

?

Entity Framework

+3

, .

+1
source

Source: https://habr.com/ru/post/1715003/


All Articles