My project is using EF Core 3.1, and targeting Azure Cosmos as the database.
I have an entity like this:
public class MyEntity
{
public IEnumerable<string> Names {get;set;}
... other fields
}
That I would like to end up in a Cosmos document like this:
{
"Names": ["Name1", "Name2"]
}
With the entity as is (IEnumerable<string>
) I get the error:
The property 'MyEntity.Names' could not be mapped, because it is of type 'IEnumerable' which is not a supported primitive type or a valid entity type.
If I change the entity to:
public class NameEntity
{
public string Name {get;set;}
}
public class MyEntity
{
public IEnumerable<NameEntity> Names {get;set;}
... other fields
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<MyEntity>(e =>
{
e.OwnsMany(p => p.Identifiers);
});
}
The document looks like this:
{
"Id": "XXXXXX",
"Names:" [
{},
{}
],
}
So I change the OnModelCreating:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<MyEntity>(e =>
{
e.OwnsMany(p => p.Identifiers, o=> o.HasKey(k=>k.Name));
});
}
And I then get:
{
"Names": [
{
"Name": "<string1>",
"Id": "XXXX"
},
{
"Identifier": "<string2>",
"Id": "XXXX"
}
]}
I've also looked at value converters, but I think that is more for converting property values between types, rather than converting an entity to a string.
Any help would be appreciated.