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

Dictionary property is being stored as an empty dictionary in cosmos db? #28374

Closed
Keerthikan opened this issue Jul 6, 2022 · 2 comments
Closed
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported

Comments

@Keerthikan
Copy link

Keerthikan commented Jul 6, 2022

I've been having some issues with storing my dictionary into cosmos db via Ef core.
My situation is similar to this SO post : https://stackoverflow.com/questions/64982780/dictionary-property-in-cosmos-db-using-ef

Include your code

I have a datacontract as such

    public class DbModel
    {
        public Guid Id { get; set; }

        public string FormName { get; set; }
        
        public Dictionary<string, object> Form { get; set; }
    }

and store it into the database as such

                  IEnumerable<DbModel> forms = jsonResponse.Select(x => new DbModel()
                    {
                        Id = new Guid(x["guid"].ToString()),
                        FormName = x["name"].ToString(),
                        Form = x
                    });
                dbContext.AddRange(forms);
                dbContext.SaveChanges();

what I seen in CosmosDb is this though:

    "Id": "000f8fa2-f0db-484c-869b-cfd864391e2f",
    "Discriminator": "DbModel",
    "FormName": "Some name",
    "id": "DbModel|000f8fa2-f0db-484c-869b-cfd864391e2f",
    "Form": {},
    "_rid": "58gcAMf4b08BAAAAAAAAAA==",
    "_self": "dbs/58gcAA==/colls/58gcAMf4b08=/docs/58gcAMf4b08BAAAAAAAAAA==/",
    "_etag": "\"bf0005bf-0000-0d00-0000-62c55e700000\"",
    "_attachments": "attachments/",
    "_ts": 1657101936

An empty form, which should at least contain a Guid and a FormName.

But according to the github issue linked in the SO post (dotnet/EntityFramework.Docs#2895), should this issue have been resolved in version 5?
so why am I having it in version 6?

Include provider and version information

EF Core version:
Database provider: Microsoft.EntityFrameworkCore.Cosmos (6.0.6) and Microsoft.EntityFrameworkCore (6.0.6)
Target framework: .NET 6.0
Operating system:
IDE: Visual Studio 2022

@AndriySvyryd AndriySvyryd added the closed-no-further-action The issue is closed and no further action is planned. label Jul 8, 2022
@AndriySvyryd
Copy link
Member

AndriySvyryd commented Jul 8, 2022

Only collections of primitive types are supported by EF Core 6, So Form needs to be typed as Dictionary<string,string>

Dictionary<string,object> is recognized as a nested entity type, you could use it, but you'd need to explicitly configure all the keys that it can contain:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<DbModel>()
        .OwnsOne(t => t.Form, c =>
        {
            c.Property<string>("name");
            c.Property<Guid?>("guid");
        });
}

@AndriySvyryd AndriySvyryd closed this as not planned Won't fix, can't repro, duplicate, stale Jul 8, 2022
@lukelimasilva
Copy link

Hi @AndriySvyryd ! I have a question. Eveytime I try to map a list of complex type, the same problem mentioned above occurs. But, I tried to add a collection of complex types without mapping it using entity and everything worked! Entity knew how to create the document and retrieve it without the mapping.

Well, since entity knows how to do it (or at least it seemed to me that it knows), is there any way to map the collection of complex type itself without having to use a dictionary?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported
Projects
None yet
Development

No branches or pull requests

3 participants