This question is locked. New answers and comments are not allowed.
I am running into an issue where when I add an entity to a Telerik DbContext, the navigation properties are being automatically populated which is causing problems downstream.
For example, I have a DeliverableEntity class that looks like (not the ENTIRE class code, but hopefully the relevant pieces)...
And each of the DeliverableAttributeEntity classes look like ...
When I Add this to the context (I'm using generics to do this) ...
Even though the *Deliverable* property of the *entity* argument was null when it when it's passed in, it is being populated in the returned *entity* parameter when the Add() method is called.
Normally I would think of this as a good thing but is leading to some issues downstream. Is there some way to disable this behavior?
UPDATE:
I tried removing the "managed" flag from the *Attributes* navigation property on the *DeliverableEntity* ...
This resolved the issue with the Deliverable being added to the Attribute on the Add() method. However it is now being populated when the SaveChanges() is called.
For example, I have a DeliverableEntity class that looks like (not the ENTIRE class code, but hopefully the relevant pieces)...
01.
public
partial
class
DeliverableEntity {
02.
03.
[System.ComponentModel.DataAnnotations.Required()]
04.
[System.ComponentModel.DataAnnotations.Key()]
05.
public
virtual
long
ID { ... }
06.
07.
[Collection(InverseProperty =
"Deliverable"
, IsManaged =
true
)]
08.
public
virtual
IList<DeliverableAttributeEntity> Attributes { ... }
09.
}
And each of the DeliverableAttributeEntity classes look like ...
01.
public
partial
class
DeliverableAttributeEntity {
02.
[System.ComponentModel.DataAnnotations.Required()]
03.
[System.ComponentModel.DataAnnotations.Key()]
04.
public
virtual
long
ID { ... }
05.
06.
public
virtual
long
DeliverableId { ... }
07.
08.
public
virtual
DeliverableEntity Deliverable { ... }
09.
}
They each contain a reference to each other. The problem comes when I attempt to Add a new DeliverableAttributeEntity that has the DeliverableId defined but the Deliverable property is null.
1.
AttributeEntity.ID = 0;
// It does not yet exist in the database
2.
AttributeEntity.DeliverableId = 134;
// Id of an existing Deliverable record
3.
AttributeEntity.Deliverable =
null
;
// No reference to the existing Deliverable
When I Add this to the context (I'm using generics to do this) ...
01.
// T = DeliverableAttributeEntity
02.
public
virtual
T AddEntity(T entity)
03.
{
04.
this
.DbContext.Add(entity);
// here it is populating the "Deliverable" Navigation Prop
05.
06.
if
(!
this
.IsPassedInContext) {
07.
this
.DbContext.SaveChanges();
08.
}
09.
10.
return
entity;
11.
}
Even though the *Deliverable* property of the *entity* argument was null when it when it's passed in, it is being populated in the returned *entity* parameter when the Add() method is called.
1.
AttributeEntity.ID = 0;
// It does not yet exist in the database
2.
AttributeEntity.DeliverableId = 134;
// Id of an existing Deliverable record
3.
AttributeEntity.Deliverable = DeliverableEntity;
// Reference now exists
Normally I would think of this as a good thing but is leading to some issues downstream. Is there some way to disable this behavior?
UPDATE:
I tried removing the "managed" flag from the *Attributes* navigation property on the *DeliverableEntity* ...
01.
public
partial
class
DeliverableEntity {
02.
03.
[System.ComponentModel.DataAnnotations.Required()]
04.
[System.ComponentModel.DataAnnotations.Key()]
05.
public
virtual
long
ID { ... }
06.
07.
[Collection(InverseProperty =
"Deliverable"
)]
08.
public
virtual
IList<DeliverableAttributeEntity> Attributes { ... }
09.
}
This resolved the issue with the Deliverable being added to the Attribute on the Add() method. However it is now being populated when the SaveChanges() is called.
01.
// T = DeliverableAttributeEntity
02.
public
virtual
T AddEntity(T entity) {
03.
04.
this
.DbContext.Add(entity);
// Not populating DeliverableEntity here anymore
05.
06.
if
(!
this
.IsPassedInContext)
07.
{
08.
this
.DbContext.SaveChanges();
// populating DeliverableEntity here now
09.
}
10.
11.
return
entity;
12.
}