In EF Core 10, how is a List<string> of finishes on a Product mapped without configuration, and how do queries over it translate?
answer
- no child table, no converter
- primitive collections since EF Core 8
- SQL Server: JSON array in one column
- Npgsql: a native text[] array
- Contains becomes OPENJSON or = ANY
basics
~20 sSince EF Core 8, a public read-write List<string> on an entity is a primitive collection: SQL Server stores it as a JSON array in one string column, and Npgsql uses a native PostgreSQL array. Contains, Any and Count on it translate to SQL.
solid answer
~40 sEF Core 8 made collections of primitive values first-class. `public List<string> Finishes { get; set; }` on `Product` is mapped by convention: on SQL Server to a JSON array in an unbounded string column, on PostgreSQL by Npgsql to a `text[]` array. No converter or comparer is written; EF Core supplies element-wise comparison, so `product.Finishes.Add("walnut")` is detected and saved. LINQ works over the column: `Where(p => p.Finishes.Contains("oak"))` becomes an `EXISTS` over `OPENJSON` on SQL Server and `'oak' = ANY (p."Finishes")` with Npgsql, and `Count` or `Any` with a predicate translate the same way. `PrimitiveCollection(p => p.Finishes)` configures it, including `ElementType()` facets such as `HasMaxLength` per element. Use a child entity instead when elements need keys, foreign keys or their own rows.
code
csharp · 14 linespublic class Product
{
public int Id { get; set; }
public required string Name { get; set; }
public List<string> Finishes { get; set; } = [];
}
var oak = await db.Products
.Where(p => p.Finishes.Contains("oak"))
.ToListAsync(ct);
var product = await db.Products.SingleAsync(p => p.Id == id, ct);
product.Finishes.Add("walnut"); // detected: no comparer needed
await db.SaveChangesAsync(ct);go deeper
Recall that a List<string> on an entity is mapped automatically since EF Core 8 and that LINQ can filter on it.
Explain the provider difference, JSON on SQL Server versus arrays on PostgreSQL, and how Contains translates on each.
Replace old serializing converters with primitive collections for tracking and queries, and know when elements deserve their own table.
Set a rule for when lists of values stay in a column and when they become tables, weighing indexing, constraints and reporting.
## What a primitive collection is A **primitive collection** is a property that holds a list of values the provider can map one by one: `List<string>`, `string[]`, `int[]`, `List<DateOnly>`, a list of enums, and so on. "Primitive" here means "a type the provider can store", including types it reaches through a built-in converter. In a furniture shop, a `Product` has `Finishes` (oak, walnut, white) and `Tags`; neither needs an identity or a table of its own. Before EF Core 8, the choices were a child table with a foreign key, or a hand-written value converter that serialized the list into one column. **EF Core 8 added built-in primitive collections**, and EF Core 10 keeps that model. ## Mapping by convention A public property with a getter and a setter whose type is a collection of a mappable element type is mapped automatically: | Provider | Column | Example value | |---|---|---| | SQL Server | an unbounded Unicode string holding a JSON array | `["oak","walnut"]` | | PostgreSQL (Npgsql) | a native array of the element type, such as `text[]` or `integer[]` | `{oak,walnut}` | Configure it with the `PrimitiveCollection` builder, which has its own `ElementType()` builder for per-element facets: ```csharp modelBuilder.Entity<Product>() .PrimitiveCollection(p => p.Finishes) .ElementType(e => e.HasMaxLength(30)); ``` On SQL Server, `HasMaxLength` on the collection itself bounds the whole JSON string; per-element facets go through `ElementType()`. A default for every collection of one type can be set in `ConfigureConventions`, as for any other property type. ## Change tracking comes for free Because EF Core knows the property is a collection of values, it gives it an element-wise comparer and a snapshot that copies the list. The common in-place edits are detected: - `product.Finishes.Add("walnut")` marks the property modified; - `product.Finishes.Remove("white")` does too; - replacing the whole list works as well. A hand-written `HasConversion` that serializes the list does not get this behaviour unless you also supply a `ValueComparer`; EF Core even logs a model-validation warning (`CoreEventId.CollectionWithoutComparer`) for such properties. That difference is the main reason to drop old converters in favour of primitive collections. ## Collection and element types - The property can be typed `List<T>`, `T[]`, `IList<T>`, `ICollection<T>` or `IEnumerable<T>`. For an interface type EF Core instantiates a `List<T>` when it materializes the value; an array property gets an array. - Element nullability follows the element type: a `List<string?>` may hold nulls, and `ElementType(e => e.IsRequired())` forbids them. - Elements can be any type the provider maps, including enums and types with a built-in converter, but not entity or complex types. Lists of structured values are complex collections mapped with `ToJson()`. - Nested collections such as `List<List<int>>` are not primitive collections. ## Querying the column The collection is queryable, not just storable. EF Core translates LINQ operators over it into the provider's array or JSON functions: 1. `Where(p => p.Finishes.Contains("oak"))`: SQL Server uses `EXISTS (SELECT 1 FROM OPENJSON([p].[Finishes]) ...)`; Npgsql sends `'oak' = ANY (p."Finishes")`. 2. `Where(p => p.Finishes.Count > 2)`: a `COUNT` over the unpacked JSON array on SQL Server, `cardinality(p."Finishes")` with Npgsql. 3. `Where(p => p.Finishes.Any(f => f.StartsWith("w")))`: a subquery over the unpacked elements. 4. Typed element functions work, because EF Core knows the element type: a `List<DateOnly>` can be filtered on `.Year`. The same machinery is used in the other direction for in-memory lists in a query (`ids.Contains(p.Id)`), which is a separate topic: how EF Core sends parameterized collections changed again in EF Core 10. ## When to use a table instead - Elements need **their own key** or must be referenced from elsewhere. - Elements need **foreign keys** to other tables, for example a finish that must exist in a `Finishes` catalogue table. - You need an **index on individual elements** that the provider cannot build over the array or JSON column. - Elements carry several values each; that is a complex collection mapped to JSON, or an entity. ## Summary For a short list of plain values read with its owner, a primitive collection is the lightest correct mapping: no converter, correct change tracking, translated queries and a provider-native column on PostgreSQL.
- An older model maps Finishes with HasConversion to a JSON string. What do you lose by keeping it?Query translation and correct change tracking. LINQ cannot reach inside a value-converted property, so `Contains` on it does not translate. Without a `ValueComparer`, `Finishes.Add(...)` on the loaded list is missed, and EF Core logs the `CollectionWithoutComparer` warning. Removing the converter lets EF Core 8+ map it as a primitive collection; check the generated migration for the column type change.
- Why does the same List<string> produce different columns on SQL Server and PostgreSQL?Each provider picks its native representation. PostgreSQL has real array types, so Npgsql maps the list to `text[]` and translates `Contains` to `= ANY`. SQL Server has no array column, so EF Core stores a JSON array in a string column and unpacks it with `OPENJSON` when a query needs the elements.
- A List<string> property always needs a child table or a custom converter in EF Core.
- Adding an item to a primitive collection is not saved unless the list is replaced.
- Contains on a primitive collection is evaluated in memory after loading every product.
- Npgsql stores a List<string> as a JSON string just like SQL Server.
Assumes EF Core 10; primitive collections have been mapped by convention since EF Core 8, before which a value converter or a child table was needed.