Inhalte aufrufen

Profilbild
- - - - -

"GenericAttributeService" Fehler bei Funktion: GetAttributesForEntity

GenericAttributeService GetAttributesForEntity InvalidOperationException GenericAttribute DeleteAttribute InsertAttribute

  • Bitte melden Sie sich an, um eine Antwort zu verfassen.
1 Antwort zu diesem Thema

#1 Lighthouse

Lighthouse

    Member

  • Members
  • PunktPunkt
  • 17 Beiträge

Geschrieben: 16 March 2022 - 17:11

Hallo zusammen,

 

mir ist ein spannendes Problem aufgefallen..

Sofern ich mir über den "GenericAttributeService" und über die Funktion "GetAttributesForEntity" für mehrere Entitäten die "GenericAttributes" hole,

diese dann anlege (sofern sie nicht vorhanden sind)

und diese anschließend direkt wieder lösche kommt es zu einer: "System.InvalidOperationException".

 

Hier der Fehler:

 

System.InvalidOperationException: 'Attaching an entity of type 'SmartStore.Core.Domain.Common.GenericAttribute' failed because another entity of the same type already has the same primary key value. This can happen when using the 'Attach' method or setting the state of an entity to 'Unchanged' or 'Modified' if any entities in the graph have conflicting key values. This may be because some entities are new and have not yet received database-generated key values. In this case use the 'Add' method or the 'Added' entity state to track the graph and then set the state of non-new entities to 'Unchanged' or 'Modified' as appropriate.'

 

 

Mit folgendem Code kann man das Ganze nachstellen:

var genericAttributes = _genericAttributeService.GetAttributesForEntity(new int[] { 935, 936 }, "Product");

if (!genericAttributes.ContainsKey(935))
{
	_genericAttributeService.InsertAttribute(new GenericAttribute() { EntityId = 935, KeyGroup = "Product", Key = "TestValue1", Value = "100001", StoreId = 0 });
}
if (!genericAttributes.ContainsKey(936))
{ 
	_genericAttributeService.InsertAttribute(new GenericAttribute() { EntityId = 936, KeyGroup = "Product", Key = "TestValue2", Value = "100002", StoreId = 0 });
}

var delEntities = _genericAttributeService.GetAttributesForEntity(new int[] { 935, 936 }, "Product");
foreach (var entities in delEntities.Values)
{
	foreach( var genericAttribute in entities)
	{ 
		Debug.WriteLine("Delete: " + genericAttribute.EntityId + " : " + genericAttribute.Value);
		_genericAttributeService.DeleteAttribute(genericAttribute);

	}
}

Sofern ich statt der Int[] Überladung die Single Int Überladung verwende, tritt dieses Problem nicht auf:

var delEntity1 = _genericAttributeService.GetAttributesForEntity(935, "Product");
var delEntity2 = _genericAttributeService.GetAttributesForEntity(936, "Product");

foreach (var genericAttribute in delEntity1)
{
	Debug.WriteLine("Delete: " + genericAttribute.EntityId + " : " + genericAttribute.Value);
	_genericAttributeService.DeleteAttribute(genericAttribute);
}
foreach (var genericAttribute in delEntity2)
{
	Debug.WriteLine("Delete: " + genericAttribute.EntityId + " : " + genericAttribute.Value);
	_genericAttributeService.DeleteAttribute(genericAttribute);
}

Woran könnte das Verhalten liegen?

 

Beste Grüße
Lighthouse IVM GmbH

 

 

 

 

 

 

 



#2 Marcus Gesing

Marcus Gesing

    SmartStore AG

  • Administrators
  • 3801 Beiträge

Geschrieben: 16 March 2022 - 20:30

Weil GetAttributesForEntity(int[], string) ungetrackte Entitäten liefert (s. TableUntracked), DeleteAttribute zum Löschen aber getrackte benötigt. Um getrackte zu erhalten würde ich IRepository<GenericAttribute> direkt verwenden.

  • Lighthouse gefällt das

Marcus Gesing

Smartstore AG