question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Mapping arrays for properties (IReadOnlyCollection) with backing fields

See original GitHub issue

Mapping arrays feature is really cool https://www.npgsql.org/efcore/mapping/array.html However when I work with DDD I would like to expose collections as readonly using IEnumerable or IReadOnlyCollection

public class Entity
{
    public IReadOnlyCollection<Guid> Attributes => attributes ;
    private Guid[] attributes = Array.Empty<Guid>();
}

and then, when I use LINQ on entity with backing field

.Where(c => c.Attributes.Contains(id))

it will generate in memory query instead of SQL query

WHERE "guid" = ANY("c"."Attributes")

Is it possible to support LINQ with sql properly for IReadOnlyCollection with backing fields?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:10 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
rojicommented, Dec 20, 2018

Perhaps that property reference in the fluent config should have been omitted?

Yeah, that’s what I meant - check out the docs on mapping fields without properties. The point is that EF Core doesn’t need to map - or even know - the property in question, it’s just a user-facing wrapper over the private field, which provides casting.

1reaction
YohDeadfallcommented, Dec 21, 2018

Regarding Collection, that again does not correspond well to the database type - PostgreSQL arrays are an ordered type, whereas a Collection isn’t. Mapping IList<T> to arrays does work though.

Checked that the new ordered dictionary class implements IList<T>. So other ordered collections should implement it as well, and there is no reason to support collection interfaces. But ArrayHandler lacks support of read only lists.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mapping arrays for properties (IReadOnlyCollection) with ...
Is it possible to support LINQ with sql properly for IReadOnlyCollection with backing fields?
Read more >
c# - How to setup readonly collection property with backing ...
I ckeck this and it worked: private readonly List<RelatedEntity> _relatedEntitys; public IReadOnlyCollection<RelatedEntity> RelatedEntitys ...
Read more >
Untitled
... c Mapping arrays for properties (IReadOnlyCollection) with backing … ... property typed as IReadOnlyCollection, which is backed by a private field ......
Read more >
What's New in EF Core 7.0
Notice that this convention allows fields to be mapped (in addition to properties) so long as they are marked with [Persist] . This...
Read more >
Collection Types in Data Contracts - WCF
A collection is a list of items of a certain type. In the .NET Framework, such lists can be represented using arrays or...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found