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

DbContext.Database.EnsureCreated() for model with multiple indexed properties throws ArgumentException #14913

Closed
ArtemMihailov opened this issue Mar 4, 2019 · 0 comments
Assignees
Labels
closed-out-of-scope This is not something that will be fixed/implemented and the issue is closed. customer-reported punted-for-3.0

Comments

@ArtemMihailov
Copy link

We have a model which needs several indexed properties. If we are using DbContext.Database.EnsureCreated() for context with such model it throws ArgumentException. If we create/migrate DB manually, adding/querying with several indexed properties are working as expected.

Exception message: An item with the same key has already been added. Key: System.Object Item [System.String]
Stack trace: 
{System.ArgumentException: An item with the same key has already been added. Key: System.Object Item [System.String]
   at System.Collections.Generic.Dictionary`2.TryInsert(TKey key, TValue value, InsertionBehavior behavior)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.GetSortedProperties(IEntityType entityType) in /_/src/EFCore.Relational/Migrations/Internal/MigrationsModelDiffer.cs:line 647
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.GetSortedProperties(TableMapping target) in /_/src/EFCore.Relational/Migrations/Internal/MigrationsModelDiffer.cs:line 595
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.Add(TableMapping target, DiffContext diffContext)+MoveNext() in /_/src/EFCore.Relational/Migrations/Internal/MigrationsModelDiffer.cs:line 552
   at System.Linq.Enumerable.SelectManySingleSelectorIterator`2.MoveNext()
   at System.Linq.Enumerable.ConcatIterator`1.MoveNext()
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.Sort(IEnumerable`1 operations, DiffContext diffContext) in /_/src/EFCore.Relational/Migrations/Internal/MigrationsModelDiffer.cs:line 144
   at Microsoft.EntityFrameworkCore.Storage.RelationalDatabaseCreator.GetCreateTablesCommands() in /_/src/EFCore.Relational/Storage/RelationalDatabaseCreator.cs:line 134
   at Microsoft.EntityFrameworkCore.Storage.RelationalDatabaseCreator.CreateTables() in /_/src/EFCore.Relational/Storage/RelationalDatabaseCreator.cs:line 116
   at Microsoft.EntityFrameworkCore.Storage.RelationalDatabaseCreator.EnsureCreated() in /_/src/EFCore.Relational/Storage/RelationalDatabaseCreator.cs:line 226
   at ConsoleApp1.Program.Main() in C:\source\repos\ConsoleApp1\ConsoleApp1\Program.cs:line 60}

Steps to reproduce

using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;

namespace ConsoleApp1
{
    public class IdentityUser
    {
        public int Id { get; set; }

        public object this[string indexedPropertyName]
        {
            get
            {
                if (this.customValues.ContainsKey(indexedPropertyName))
                {
                    return this.customValues[indexedPropertyName];
                }

                return null;
            }

            set => this.customValues.Add(indexedPropertyName, value);
        }

        private readonly Dictionary<string, object> customValues = new Dictionary<string, object>();
    }

    public class BloggingContext : DbContext
    {
        public DbSet<IdentityUser> Users { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder
                .UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=Test;ConnectRetryCount=0");
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<IdentityUser>(
                d =>
                {
                    d.Metadata.AddIndexedProperty("Field1", typeof(decimal));
                    d.Metadata.AddIndexedProperty("Field2", typeof(Guid));
                });

            base.OnModelCreating(modelBuilder);
        }
    }

    public class Program
    {
        public static void Main()
        {
            using (var context = new BloggingContext())
            {
                context.Database.EnsureDeleted();
                context.Database.EnsureCreated();
            }
        }
    }
}

Further technical details

EF Core version: 3.0.0-preview.19074.3
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Windows 10 1809
IDE: Visual Studio 2017 15.9.7

@ajcvickers ajcvickers added this to the 3.0.0 milestone Mar 4, 2019
@ajcvickers ajcvickers modified the milestones: 3.0.0, Backlog Jun 27, 2019
@bricelam bricelam removed this from the Backlog milestone Sep 4, 2019
@ajcvickers ajcvickers added the closed-out-of-scope This is not something that will be fixed/implemented and the issue is closed. label Mar 10, 2022
@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-out-of-scope This is not something that will be fixed/implemented and the issue is closed. customer-reported punted-for-3.0
Projects
None yet
Development

No branches or pull requests

4 participants