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

Cosmos: Dictionary<int, string> properties do not generate any errors, but don't map correctly #25749

Closed
ajcvickers opened this issue Aug 27, 2021 · 1 comment · Fixed by #25827
Labels
area-cosmos area-model-building closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Milestone

Comments

@ajcvickers
Copy link
Contributor

Code below works if the dictionary is Dictionary<string, string>.

public static class CosmosPrimitiveTypesSample
{
    public static void Collections_and_dictionaries_of_primitive_types()
    {
        Console.WriteLine($">>>> Sample: {nameof(Collections_and_dictionaries_of_primitive_types)}");
        Console.WriteLine();

        Helpers.RecreateCleanDatabase();

        #region Insert
        using var context = new BooksContext();

        var book = new Book
        {
            Title = "How It Works: Incredible History",
            Quotes = new List<string>
            {
                "Thomas (Tommy) Flowers was the British engineer behind the design of the Colossus computer.",
                "Invented originally for Guinness, plastic widgets are nitrogen-filled spheres.",
                "For 20 years after its introduction in 1979, the Walkman dominated the personal stereo market."
            },
            Notes = new Dictionary<int, string>
            {
                { 121, "Fridges" },
                { 144, "Peter Higgs" },
                { 48, "Saint Mark's Basilica" },
                { 36, "The Terracotta Army" }
            }
        };

        context.Add(book);
        context.SaveChanges();
        #endregion

        #region Updates
        book.Quotes.Add("Pressing the emergency button lowered the rods again.");
        book.Notes[48] = "Chiesa d'Oro";

        context.SaveChanges();
        #endregion

        Console.WriteLine();
    }

    public static class Helpers
    {
        public static void RecreateCleanDatabase()
        {
            using var context = new BooksContext(quiet: true);

            context.Database.EnsureDeleted();
            context.Database.EnsureCreated();
        }
    }

    #region BookEntity
    public class Book
    {
        public Guid Id { get; set; }
        public string Title { get; set; }
        public IList<string> Quotes { get; set; }
        public Dictionary<int, string> Notes { get; set; }
    }
    #endregion

    public class BooksContext : DbContext
    {
        public DbSet<Book> Books { get; set; }

        private readonly bool _quiet;

        public BooksContext(bool quiet = false)
        {
            _quiet = quiet;
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Book>().ToContainer("books");
        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder
                .EnableSensitiveDataLogging()
                .UseCosmos(
                "https://localhost:8081",
                "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==",
                "EFCoreSample");

            if (!_quiet)
            {
                optionsBuilder.LogTo(
                    Console.WriteLine,
                    new[] { CosmosEventId.ExecutedCreateItem, CosmosEventId.ExecutingSqlQuery, CoreEventId.SaveChangesCompleted });
            }
        }
    }
}

Document:

{
    "Id": "b5940ee9-36ea-4986-87a6-76163a45f40a",
    "Discriminator": "Book",
    "Quotes": [
        "Thomas (Tommy) Flowers was the British engineer behind the design of the Colossus computer.",
        "Invented originally for Guinness, plastic widgets are nitrogen-filled spheres.",
        "For 20 years after its introduction in 1979, the Walkman dominated the personal stereo market.",
        "Pressing the emergency button lowered the rods again."
    ],
    "Title": "How It Works: Incredible History",
    "id": "Book|b5940ee9-36ea-4986-87a6-76163a45f40a",
    "Notes": {},
    "_rid": "ESsgAJPQFeoBAAAAAAAAAA==",
    "_self": "dbs/ESsgAA==/colls/ESsgAJPQFeo=/docs/ESsgAJPQFeoBAAAAAAAAAA==/",
    "_etag": "\"00000000-0000-0000-9b53-8a4362ae01d7\"",
    "_attachments": "attachments/",
    "_ts": 1630076113
}
@ajcvickers
Copy link
Contributor Author

Note from triage: only string-keyed dictionaries are supported. This should throw in model validation or similar.

AndriySvyryd added a commit that referenced this issue Sep 1, 2021
Reenable test for 24684

Fixes #25749
Fixes #24684
@AndriySvyryd AndriySvyryd added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Sep 1, 2021
@AndriySvyryd AndriySvyryd removed their assignment Sep 1, 2021
AndriySvyryd added a commit that referenced this issue Sep 1, 2021
Reenable test for 24684

Fixes #25749
Fixes #24684
AndriySvyryd added a commit that referenced this issue Sep 1, 2021
Reenable test for 24684

Fixes #25749
Fixes #24684
AndriySvyryd added a commit that referenced this issue Sep 1, 2021
@ajcvickers ajcvickers modified the milestones: 6.0.0, 6.0.0-rc2 Sep 2, 2021
@ajcvickers ajcvickers modified the milestones: 6.0.0-rc2, 6.0.0 Nov 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-cosmos area-model-building closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants