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

Cannot have keys other than those declared on the root type #5201

Closed
peirens-bart opened this issue Apr 28, 2016 · 2 comments
Closed

Cannot have keys other than those declared on the root type #5201

peirens-bart opened this issue Apr 28, 2016 · 2 comments

Comments

@peirens-bart
Copy link

I'm having problems mapping types in an inheritance gain.

My Models:

public class Entity
    {
        public int Id { get; set; }

        public byte[] Timestamp { get; set; }

    }
public class BinaryImage: Entity
    {
        public string Name { get; set; }

        public byte[] Data { get; set; }

        public string Extension { get; set; }
    }

My context:

protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.HasDefaultSchema("dead_user");
            FrameworkModelsMapper.Map(modelBuilder);
            SecurityModelsMapper.Map(modelBuilder);
            MyDatabaseModelsMapper.Map(modelBuilder);
        }

        #endregion

        #region DbSets
        public virtual DbSet<BinaryImage> BinaryImages { get; set; }

My Mapper

 internal class FrameworkModelsMapper
    {
        internal static void Map(ModelBuilder builder)
        {
            builder.Entity(MapBinaryImage());
            builder.Entity(MapValuta());
            builder.Entity(MapCountry());

        }

        internal static void MapEntity<T>(EntityTypeBuilder<T> builder) where T : Entity
        {
            builder.HasKey(x => x.Id);  **=> give exception**
            builder.Property(x => x.Timestamp)
            .ValueGeneratedOnAddOrUpdate()
            .IsConcurrencyToken();
        }

        private static Action<EntityTypeBuilder<BinaryImage>> MapBinaryImage()
        {
            Action<EntityTypeBuilder<BinaryImage>> config = builder =>
            {
                MapEntity(builder);
                builder.Property(x => x.Data).IsRequired();
                builder.Property(x => x.Name).IsRequired();
                builder.Property(x => x.Extension).IsRequired();
            };
            return config;
        }
}

Exception in command line
C:\Code\BookerTools\Source\Development\Version_1\Source\BookerTools.Database.EF>
dnx ef migrations add BT-0001 -c "ApplicationContext" -p "BookerTools.Database.E
F"

System.InvalidOperationException: The derived type 'BookerTools.Framework.Models
.BinaryImage' cannot have keys other than those declared on the root type.
at Microsoft.Data.Entity.Metadata.Internal.EntityType.AddKey(IReadOnlyList1 properties) at Microsoft.Data.Entity.Metadata.Internal.MetadataDictionary2.GetOrAdd(Func
1 getKey, Func1 createKey, Func2 createValue, Func2 onNewKeyAdded, Configura
tionSource configurationSource)
at Microsoft.Data.Entity.Metadata.Internal.InternalEntityTypeBuilder.HasKey(I
ReadOnlyList1 properties, ConfigurationSource configurationSource) at Microsoft.Data.Entity.Metadata.Internal.InternalEntityTypeBuilder.PrimaryK ey(IReadOnlyList1 properties, ConfigurationSource configurationSource)
at Microsoft.Data.Entity.Metadata.Builders.EntityTypeBuilder1.HasKey(Express ion1 keyExpression)
at BookerTools.Database.EF.Mappers.FrameworkModelsMapper.MapEntity[T](EntityT
ypeBuilder1 builder) at BookerTools.Database.EF.Mappers.FrameworkModelsMapper.<>c.<MapBinaryImage> b__5_0(EntityTypeBuilder1 builder)
at Microsoft.Data.Entity.ModelBuilder.Entity[TEntity](Action1 buildAction) at BookerTools.Database.EF.Mappers.FrameworkModelsMapper.Map(ModelBuilder bui lder) at BookerTools.Database.EF.Context.ApplicationContext.OnModelCreating(ModelBu ilder modelBuilder) at Microsoft.Data.Entity.Infrastructure.ModelSource.CreateModel(DbContext con text, IConventionSetBuilder conventionSetBuilder, IModelValidator validator) at System.Collections.Concurrent.ConcurrentDictionary2.GetOrAdd(TKey key, Fu
nc2 valueFactory) at Microsoft.Data.Entity.Internal.DbContextServices.CreateModel() at Microsoft.Data.Entity.Internal.LazyRef1.get_Value()
at Microsoft.Extensions.DependencyInjection.ServiceProvider.ScopedCallSite.In
voke(ServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ServiceProviderExtensions.GetServ
ice[T](IServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ServiceProvider.TransientCallSite
.Invoke(ServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.ConstructorCallSite
.Invoke(ServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ServiceProvider.TransientCallSite
.Invoke(ServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ServiceProviderExtensions.GetRequ
iredService(IServiceProvider provider, Type serviceType)
at Microsoft.Extensions.DependencyInjection.ServiceProviderExtensions.GetRequ
iredService[T](IServiceProvider provider)
at Microsoft.Data.Entity.Design.MigrationsOperations.AddMigration(String name
, String outputDir, String contextType)
at Microsoft.Data.Entity.Commands.Program.Executor.<>c__DisplayClass6_0.b__0()
at Microsoft.Data.Entity.Commands.Program.Executor.Execute(Action action)
The derived type 'BookerTools.Framework.Models.BinaryImage' cannot have keys oth
er than those declared on the root type.

Is there something that I'm missing?

@peirens-bart
Copy link
Author

peirens-bart commented May 2, 2016

Found the cause of the issue,
One of the models has a property with object as type. if I add ignore on that property everything is working (and ignore is the correct behavior for that property).
I must say, the exception message is a bit confusing, I was looking in BinaryImage but the problem was in TeamOption object. BinaryImage is just the first model that I map.

@smitpatel
Copy link
Member

Exception message has been updated after RC1 see #3727

@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
None yet
Projects
None yet
Development

No branches or pull requests

3 participants