dotnet / dotnet/efcore

Allow IndexerProperty to allow index types other than string

Open
#36,673 0 comments 0 reactions 0 assignees View on GitHub
area-o/c-mapping customer-reported
Dominant language
C#
Stars
14.8k
Forks
3.4k
PR merge metrics
PR metrics pending

Description

### What problem are you trying to solve?

I would like to back a number of repetitive columns with an array in an entity type. Currently this would require mapping a string number to an indexer property and either parsing an integer from the string to access from the array, or to use a dictionary with a string key.

### Describe the solution you'd like

Add new IndexerProperty methods that allow a "propertyName" value other than string.
```csharp
public virtual PropertyBuilder IndexerProperty([DynamicallyAccessedMembers(IProperty.DynamicallyAccessedMemberTypes)] Type propertyType, TIndex propertyName);
public virtual PropertyBuilder IndexerProperty<[DynamicallyAccessedMembers(IProperty.DynamicallyAccessedMemberTypes)] TProperty, TIndex>(TIndex propertyName);
```

```csharp
public class Entity {
public int Id { get; set; }
public string Name { get; set; }
private int[] _Values = new int[5];

public int this[int i] {
get => _Values[i];
set => _Values[i] = value;
}
}

modelBuilder.Entity(e => {
e.IndexerProperty(0).HasColumnName("Value0");
e.IndexerProperty(1).HasColumnName("Value1");
e.IndexerProperty(2).HasColumnName("Value2");
e.IndexerProperty(3).HasColumnName("Value3");
e.IndexerProperty(4).HasColumnName("Value4");
});
// or
modelBuilder.Entity(e => {
e.IndexerProperty(typeof(int), 0).HasColumnName("Value0");
e.IndexerProperty(typeof(int), 1).HasColumnName("Value1");
e.IndexerProperty(typeof(int), 2).HasColumnName("Value2");
e.IndexerProperty(typeof(int), 3).HasColumnName("Value3");
e.IndexerProperty(typeof(int), 4).HasColumnName("Value4");
});

```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.