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

Microsoft.EntityFrameworkCore.Internal.ReferenceEqualityComparer is not marked as serializable #17369

ganaware opened this issue Aug 22, 2019 · 2 comments


Copy link

commented Aug 22, 2019

When I serialized entities that have one-to-many relationship by BinarySerializer, an SerializationException was raised. It said that "Type 'Microsoft.EntityFrameworkCore.Internal.ReferenceEqualityComparer' in Assembly 'Microsoft.EntityFrameworkCore, Version=, Culture=neutral, PublicKeyToken=adb9793829ddae60' is not marked as serializable."

For example:

public class Book {
    public int Id { get; set; }
    public ICollection<Page> Pages { get; set; }

public class Page {
    public int Id { get; set; }
    public int BookId { get; set; }
    public Book Book { get; set; }

After loading from DB, the Book.Pages is initialized to a HashSet<Page> instance that has a Microsoft.EntityFrameworkCore.Internal.ReferenceEqualityComparer.Instance as a comparer.

The serialization of Book was legal on EFCode 2.1.
Bow now, it is illigal.
The Microsoft.EntityFrameworkCore.Internal.ReferenceEqualityComparer was introduced from 2.2.0 (cf. #12984)

Can you change ReferenceEqualityComparer to be serializable?
Or should I give up using BinarySerializer and find another way such as JsonConvert.Serialize() ?

Further technical details

EF Core version: 2.2.6
Database Provider: MySqlConnector 0.49.3
Operating system: Windows 7
IDE: Visual Studio Professional 2017 15.8.7


This comment has been minimized.

Copy link

commented Aug 26, 2019

@ganaware We will look into whether or not it makes sense to do this based on current guidance around binary serialization. For now, you can always initialize the collections explicitly, rather than letting EF do it.

@ajcvickers ajcvickers added this to the Backlog milestone Aug 26, 2019


This comment has been minimized.

Copy link

commented Aug 27, 2019

Initializing the collections explicitly is a good solution. Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
2 participants
You can’t perform that action at this time.