Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"An item with the same key has already been added. Key: 222 #96

Open
RussKahler1970 opened this issue Nov 8, 2021 · 0 comments
Open

Comments

@RussKahler1970
Copy link

I am using EntityFramework to return a list of IEntityBase in an response object.

public class SelectResponse
{
    [JsonInclude]        
    public virtual List<IEntityBase> Data { get; set; }
}

If I add a list of of entities where I create the entity with "new" everything is fine. when I select entities from EntityFramework I get the below error. the type from EntityFramework is {Castle.Proxies.AspNetUserProxy} a proxy which inherits from my type that includes my Discriminator.

Any thoughts on how to use polymorphic serialization with entity framework lazy loading?

JsonSerializer.Serialize(response,response.GetType(), options)
'JsonSerializer.Serialize(response,response.GetType(), options)' threw an exception of type 'System.Reflection.TargetInvocationException'
Data: {System.Collections.ListDictionaryInternal}
HResult: -2146232828
HelpLink: null
InnerException: {"An item with the same key has already been added. Key: 222"}
Message: "Exception has been thrown by the target of an invocation."
Source: "System.Private.CoreLib"
StackTrace: " at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)\r\n at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)\r\n at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture)\r\n at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)\r\n at System.Activator.CreateInstance(Type type, Object[] args)\r\n at Dahomey.Json.Serialization.Converters.Factories.AbstractJsonConverterFactory.CreateConverter(JsonSerializerOptions options, Type converterType)\r\n at Dahomey.Json.Serialization.Converters.Factories.AbstractJsonConverterFactory.CreateGenericConverter(JsonSerializerOptions options, Type genericType, Type[] typeArguments)\r\n at Dahomey.Json.Serialization.Converte
rs.Factories.ObjectConverterFactory.CreateConverter(Type typeToConvert, JsonSerializerOptions options)\r\n at System.Text.Json.Serialization.JsonConverterFactory.GetConverterInternal(Type typeToConvert, JsonSerializerOptions options)\r\n at System.Text.Json.JsonSerializerOptions.GetConverter(Type typeToConvert)\r\n at Dahomey.Json.Serialization.Converters.ObjectConverter1.Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)\r\n at Dahomey.Json.Serialization.Converters.AbstractCollectionConverter2.Write(Utf8JsonWriter writer, TC value, JsonSerializerOptions options)\r\n at Dahomey.Json.Serialization.Converters.MemberConverter2.Write(Utf8JsonWriter writer, Object obj, JsonSerializerOptions options)\r\n at Dahomey.Json.Serialization.Converters.ObjectConverter1.Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)\r\n at System.Text.Json.Serialization.JsonConverter1.TryWrite(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)\r\n at System.Text.Json.Serialization.JsonConverter1.WriteCore(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)\r\n at System.Text.Json.JsonSerializer.WriteCore[TValue](JsonConverter jsonConverter, Utf8JsonWriter writer, TValue& value, JsonSerializerOptions options, WriteStack& state)\r\n at System.Text.Json.JsonSerializer.WriteCore[TValue](Utf8JsonWriter writer, TValue& value, Type inputType, JsonSerializerOptions options)\r\n at System.Text.Json.JsonSerializer.Serialize[TValue](TValue& value, Type inputType, JsonSerializerOptions options)"
TargetSite: {System.Object InvokeMethod(System.Object, System.Object[], System.Signature, Boolean, Boolean)}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant