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

System.Object being discovered as EntityType through navigation. #3727

Closed
Tasteful opened this issue Nov 11, 2015 · 31 comments
Closed

System.Object being discovered as EntityType through navigation. #3727

Tasteful opened this issue Nov 11, 2015 · 31 comments
Assignees
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Milestone

Comments

@Tasteful
Copy link
Contributor

I tried today to update my solution to RC1 from beta8 and get the exception The derived type 'FieldDefinitionEntity' cannot have keys other than those declared on the root type.. The root type is not mapped, only the derivied type. Should the mapping throw this exception anyway?

public class FieldDefinitionEntity : FieldDefinitionEntityBase
{
    public virtual bool MultiCulture { get; set; }
}

public abstract class FieldDefinitionEntityBase
{
    public virtual string FieldType { get; set; }
    public virtual string Id { get; set; }
}

Mapping

builder.Entity<FieldDefinitionEntity>(b =>
{
    b.ToTable("FieldDefinition", "Products");
    b.HasKey(p => p.Id);
});
@Tasteful
Copy link
Contributor Author

The derived type 'AssortmentEntity' cannot have keys other than those declared on the root type. even that the AssortmentEntity not derived from any object. Using EF from myget v7.0.0-rc1-16305.

public class AssortmentEntity
{
    public virtual string ExternalId { get; set; }
    public virtual Guid Id { get; set; }
    public virtual ICollection<string> FieldDatas { get; set; }
}

With the mapping

builder.Entity<AssortmentEntity>(b =>
{
    b.ToTable("Assortment", "Products");
    b.HasKey(p => p.Id); // <- this line will throw exception
    b.HasAlternateKey(p => p.ExternalId);

    b.Property(p => p.ExternalId).HasMaxLength(100);
});

Result StackTrace:

at Microsoft.Data.Entity.Metadata.Internal.EntityType.AddKey(IReadOnlyList`1 properties)
   at Microsoft.Data.Entity.Metadata.Internal.InternalEntityTypeBuilder.<>c__DisplayClass14_0.<HasKey>b__1()
   at Microsoft.Data.Entity.Metadata.Internal.MetadataDictionary`2.GetOrAdd(Func`1 getKey, Func`1 createKey, Func`2 createValue, Func`2 onNewKeyAdded, ConfigurationSource configurationSource)
   at Microsoft.Data.Entity.Metadata.Internal.InternalEntityTypeBuilder.HasKey(IReadOnlyList`1 properties, ConfigurationSource configurationSource)
   at Microsoft.Data.Entity.Metadata.Internal.InternalEntityTypeBuilder.PrimaryKey(IReadOnlyList`1 properties, ConfigurationSource configurationSource)
   at Microsoft.Data.Entity.Metadata.Internal.InternalEntityTypeBuilder.PrimaryKey(IReadOnlyList`1 clrProperties, ConfigurationSource configurationSource)
   at Microsoft.Data.Entity.Metadata.Builders.EntityTypeBuilder`1.HasKey(Expression`1 keyExpression)
   at App.DataModel.<>c.<OnModelCreating>b__1_0(EntityTypeBuilder`1 b) in \Core\Products\DataModel.cs:line 89
   at Microsoft.Data.Entity.ModelBuilder.Entity[TEntity](Action`1 buildAction)
   at App.DataModel.OnModelCreating(DbContext dbContext, ModelBuilder builder) in \Core\Products\DataModel.cs:line 87
   at AppDbContext.OnModelCreating(ModelBuilder builder) in \Data\AppDbContext.cs:line 113
   at Microsoft.Data.Entity.Infrastructure.ModelSource.CreateModel(DbContext context, IConventionSetBuilder conventionSetBuilder, IModelValidator validator)
   at Microsoft.Data.Entity.Infrastructure.ModelSource.<>c__DisplayClass8_0.<GetModel>b__0(Type k)
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
   at Microsoft.Data.Entity.Infrastructure.ModelSource.GetModel(DbContext context, IConventionSetBuilder conventionSetBuilder, IModelValidator validator)
   at Microsoft.Data.Entity.Internal.DbContextServices.CreateModel()
   at Microsoft.Data.Entity.Internal.LazyRef`1.get_Value()
   at Microsoft.Data.Entity.Internal.DbContextServices.get_Model()
   at Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions.<>c.<AddEntityFramework>b__0_5(IServiceProvider p)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.FactoryService.Invoke(ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.ScopedCallSite.Invoke(ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.ConstructorCallSite.Invoke(ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.ScopedCallSite.Invoke(ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.<>c__DisplayClass12_0.<RealizeService>b__0(ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderExtensions.GetService[T](IServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions.<>c.<AddEntityFramework>b__0_0(IServiceProvider p)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.FactoryService.Invoke(ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.ScopedCallSite.Invoke(ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.ClosedIEnumerableService.CallSite.Invoke(ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.TransientCallSite.Invoke(ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.ConstructorCallSite.Invoke(ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.ScopedCallSite.Invoke(ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.ConstructorCallSite.Invoke(ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.ScopedCallSite.Invoke(ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.ConstructorCallSite.Invoke(ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.ScopedCallSite.Invoke(ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.ConstructorCallSite.Invoke(ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.ScopedCallSite.Invoke(ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.<>c__DisplayClass12_0.<RealizeService>b__0(ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderExtensions.GetRequiredService[T](IServiceProvider provider)
   at Microsoft.Data.Entity.Storage.DatabaseProviderServices.GetService[TService]()
   at Microsoft.Data.Entity.Storage.RelationalDatabaseProviderServices.get_QueryContextFactory()
   at Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions.<>c.<AddQuery>b__1_0(IServiceProvider p)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.FactoryService.Invoke(ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.ScopedCallSite.Invoke(ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.ConstructorCallSite.Invoke(ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.ScopedCallSite.Invoke(ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.ConstructorCallSite.Invoke(ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.ScopedCallSite.Invoke(ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.<>c__DisplayClass12_0.<RealizeService>b__0(ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderExtensions.GetRequiredService[T](IServiceProvider provider)
   at Microsoft.Data.Entity.Infrastructure.AccessorExtensions.GetService[TService](IInfrastructure`1 accessor)
   at Microsoft.Data.Entity.Internal.InternalDbSet`1.<.ctor>b__2_0()
   at Microsoft.Data.Entity.Internal.LazyRef`1.get_Value()
   at Microsoft.Data.Entity.Internal.InternalDbSet`1.System.Linq.IQueryable.get_Provider()
   at System.Linq.Queryable.Select[TSource,TResult](IQueryable`1 source, Expression`1 selector)

@smitpatel
Copy link
Contributor

Tried running both repro using rc1-final packages. First one gave no errors. Second one throws exception that FieldDatas is not added the model. which is expected.
Can you provide full model and context for repro?

@Tasteful
Copy link
Contributor Author

@smitpatel The second exceptions seems to be related to the first one. I have put out all related parts in a new project, Project is working in beta8, in later versions it will throw exception. Have tested with the rc1 from aspnetrelease and rc2 from aspnetvnext myget feeds.

beta8: https://dl.dropboxusercontent.com/u/16590863/EF-DeriviedExceptionTest-beta8.zip
rc1: https://dl.dropboxusercontent.com/u/16590863/EF-DeriviedExceptionTest-rc1.zip
rc2-16128: https://dl.dropboxusercontent.com/u/16590863/EF-DeriviedExceptionTest-rc2-16128.zip

@smitpatel
Copy link
Contributor

Thanks for the codes. The only change I had to make was fixing xunit.runner.aspnet in rc1 bits since it had wrong reference to xunit runner.

Everything works fine in beta8, following exception is thrown in rc1 & rc2
System.InvalidOperationException : The derived type 'TestProject.FieldDefinitionEntity+FieldDataEntity' cannot have keys other than those declared on the root type.
with rc1 we have base type discovery convention added so the convention is assigning base type to FieldDataEntity but looking at the model, it should not be assigned.
I will debug more into it.

@smitpatel smitpatel self-assigned this Nov 12, 2015
@rowanmiller
Copy link
Contributor

@smitpatel

With rc1 we have base type discovery convention added

We should only include base types in the model if they are explicitly configured. Are you saying we automatically pull base types into the model now?

@smitpatel
Copy link
Contributor

I am debugging into it. The exception is thrown because we have set base type to System.Object by convention which is a bug.

@smitpatel
Copy link
Contributor

Found the issue. The model entity type FieldDataEntity has property of type object, since it cannot be mapped by type mapper, we try to add it as navigation which works and creates entity type of System.Object. Convention will put that as base type for every other entity type.

Fix should be - do not discover object as navigation type since the target type is never valid entity type.

@smitpatel smitpatel changed the title "The derived type '[object]' cannot have keys other than those declared on the root type." is throw even if the root type not is mapped. System.Object being discovered as EntityType through navigation. Nov 12, 2015
@divega
Copy link
Contributor

divega commented Nov 12, 2015

@smitpatel Agreed that it is safe to always discard System.Object as not valid but shouldn't we have logic already that figures out that System.Object is not a valid entity type because it doesn't have keys? 😄

@smitpatel
Copy link
Contributor

@divega - Our logic for discard entities without keys happen in ModelValidator. Since System.Object is base type for all clr types, the entity type for it will be set as base type for all other entity types which will cause exceptions before even model validator stage is reached.

@divega
Copy link
Contributor

divega commented Nov 12, 2015

Ok, understood that it works that way, but how would this case be different from having any other base type that doesn't have keys and appears as the type of a property in in the model? Or are you saying a model in which there is a property of type System.Object is valid without an Ignore() on it? I think that is debatable 😄

@AndriySvyryd
Copy link
Member

In general we don't know whether an entity type will have a primary key before discovering it.
But, in my opinion it is safe to assume System.Object will not.

@smitpatel
Copy link
Contributor

Or are you saying a model in which there is a property of type System.Object is valid without an Ignore() on it?

If type mapper supports mapping object to primitive type, then it should be valid model. Else it becomes invalid model and will throw that property is not added to the model or marked as ignored.

@divega
Copy link
Contributor

divega commented Nov 12, 2015

I don't disagree we can special case System.Object, I just want to make sure that we don't miss other examples of the same issue with other types.

@divega
Copy link
Contributor

divega commented Nov 12, 2015

If type mapper supports mapping object to primitive type, then it should be valid model

Agreed... Hang on, System.Object with a key explicitly configured in shadow could be a valid entity type too! (not that I would recommend anyone doing that and not my highest concern to enable it).

@smitpatel
Copy link
Contributor

System.Object with a key explicitly configured in shadow could be a valid entity type too!

And that will be base type of all other entity types in the model. 😄
Hence everything will just merge into 1 large table (at least till we have only TPH)

@AndriySvyryd
Copy link
Member

We could use KeyDiscoveryConvention to test whether the PK would be discovered before trying to add the entity type to the model, but we decided not to do that since it would make it harder to find out why an entity type wasn't discovered when the user expected that it would

@smitpatel smitpatel added this to the 7.0.0-rc2 milestone Nov 13, 2015
@smitpatel
Copy link
Contributor

@Tasteful - for the issue you reported,
With rc1 we have full TPH support now. #3441 added base type discovery convention which sets hierarchy for entity types added in the model if they are in clr hierarchy.
The model has property of type object since it cannot be mapped by typemapper it is being added as entity type and since object is clr base type for any other type, any other entity type will get base type set on it and HasKey throws exception for derived types.
For resolution,
Since object is not mapped by type mapper as primitive, either you should ignore the property if not being used or change data type to something else which can be mapped as primitive by type mapper.
In rare case if you are trying to use object as entity type, let us know what is the requirement for you causing it.

@Tasteful
Copy link
Contributor Author

@smitpatel
The object property is converted into a json-string property before saving and reverse after loading and the property is included in the mapping as b.Ignore(p => p.JsonValue); but the type discovery convention is run before this is executed. I don't need to be able to map an object directly with a sql-column, should be with ms-sql2016 when we have a json datatype on the serverside and maybe can map object directly to the column.

The fix you was doing, will that popup in the aspnetrelease or aspnetvnext myget channel?
Will the change be included in rc1-final-package?

@ErikEJ
Copy link
Contributor

ErikEJ commented Nov 13, 2015

@Tasteful There will not be any Json datatype in SQL 2016... http://blogs.msdn.com/b/jocapc/archive/2015/05/16/json-support-in-sql-server-2016.aspx

@Tasteful
Copy link
Contributor Author

@ErikEJ SQL2016 will have JSON support, even if the datatype in SQL will be nvarchar (regarding to the link you was referring to). But that article will not handle how the conversion between object and JSON is made in the .net side, that can be manual part as today or full automatic. I haven't dig deeper into that than I have read that SQL2016 will support JSON.

@smitpatel
Copy link
Contributor

@Tasteful - The change will appear in nightly builds - that is aspnetvnext feed in couple of days. It will not be part of rc1 release though.
To unblock yourself, Try using [NotMapped] attribute on the property or call Ignore before HasKey in OnModelConfiguring) that should ignore it before running relationship discovery. That should avoid the exception.

EF supports SQL2008 onwards, further to support a datastore type, we need conversion from data store type to CLR type so mapping json datatype to object is not just matter of SQLServer supporting json data type. EF has plan to add support for user-defined type mapping (#242) which would allow user to map any CLR type to any data store using user provided conversion for mapping.

@miroslavsiska
Copy link

I have the same problem. beta 8 worked, rc1-final not working... Here is my model:

    public class AllergenQuantity 
     {              
                public decimal Quantity { get; set; }
                //...model simplified
     }

public class AllergenQuantityMaterial : AllergenQuantity
{
    public Guid MaterialId { get; set; }
    public virtual Material Material { get; set; }

    public Guid AllergenId { get; set; }
    public virtual Allergen Allergen { get; set; }   
}

public class Material
   public Material()
    {
        this.Id = Guid.NewGuid();             
    }

    public virtual ICollection<AllergenQuantityMaterial> AllergenQuantityMaterial { get; set; }

    [Key]
    public Guid Id { get; set; } 

    [Display(Name = "Název")]
    public string Name { get; set; }  
}

 public class Allergen
{  
    public Allergen()
    {
         this.Id = Guid.NewGuid();
     }
     public virtual ICollection<AllergenQuantityMaterial> AllergenQuantityMaterial { get; set; }

    [Key]
    public Guid Id { get; set; } 

    public string Name { get; set; }
}

Fluent API:

builder.Entity<AllergenQuantityMaterial>().HasKey(l => new { l.MaterialId, l.AllergenId });

...AND here is error message:
error

Why i need it:
I have in my model many customized join tables derived from one table: AllergenQuantity...

public class AllergenQuantityUsed : AllergenQuantity
    {
      //here is only other many-to-many mapping
    }
public class AllergenQuantityMaximum: AllergenQuantity
    {
         //here is only other many-to-many mapping
    }

Of coarse similar pattern has been used by me for many other tables. I have complex model... You can use in Beta 8 one table with defined columns and next tables derived from it, until you can specify keys in derived tables... Now is this not working. Is it bug or new feature please... ????
Of coarse, I can write each class like this:

public class AllergenQuantityUsed 
{              
        public decimal Quantity { get; set; }
        //...AND HERE COPY ALL OF PROPERTIES BY HAND...
}

 public class AllergenQuantityMaximum
{              
        public decimal Quantity { get; set; }
        //...AND HERE COPY ALL OF PROPERTIES BY HAND TOO...
}

.....but previous pattern was better...

Thank you

@divega
Copy link
Contributor

divega commented Nov 23, 2015

@miroslavsiska this seems to be a different issue, even if the symptoms are similar.

I believe what is happening is that Code First is bringing AllergenQuantity as an entity type to the model, making it the base class of the inheritance hierarchy. The exception then is thrown because the whole hierarchy is supposed to use the shame key definition. Most likely if this worked in Beta8 was because inheritance support was incomplete.

Since you have simplified your model there are a couple of things that aren't clear:

  1. How does the base type get pulled into the model? This shouldn't happen unless you have a DbSet<AllergenQuantity> property or you call ModelBuilder.Entity<AllergenQuantity>() or there is a navigation property of type AllergenQuantity (or some collection of element type AllergenQuantity)
  2. Does AllergenQuantity have a property that EF would match as an entity key by convention? If it doesn't then I am afraid this exception isn't clear.

cc @smitpatel

@rowanmiller
Copy link
Contributor

@miroslavsiska - that is a different scenario that the one being discussed in this issue. Can you open a new issue with details of your scenario.

Please be sure to include a complete code listing to reproduce the issue as I tried creating a console app and everything worked as expected...

using Microsoft.Data.Entity;
using System;
using System.Collections.Generic;

namespace Repro
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var db = new MyContext())
            {
                db.Database.EnsureCreated();

                var peanutButter = new Material { Name = "Peanut Butter" };
                db.Materials.Add(peanutButter);

                var peanuts = new Allergen { Name = "Peanuts" };
                db.Allergens.Add(peanuts);

                db.AllergenQuantityMaterials.Add(new AllergenQuantityMaterial
                {
                    Allergen = peanuts,
                    Material = peanutButter,
                    Quantity = 1000
                });

                db.SaveChanges();
            }
        }
    }


    public class MyContext : DbContext
    {
        public DbSet<Material> Materials { get; set; }
        public DbSet<Allergen> Allergens { get; set; }
        public DbSet<AllergenQuantityMaterial> AllergenQuantityMaterials { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder
                .Entity<AllergenQuantityMaterial>()
                .HasKey(l => new { l.MaterialId, l.AllergenId });
        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=Repro;Trusted_Connection=True;");
        }
    }

    public class AllergenQuantity
    {
        public decimal Quantity { get; set; }
    }

    public class AllergenQuantityMaterial : AllergenQuantity
    {
        public Guid MaterialId { get; set; }
        public virtual Material Material { get; set; }

        public Guid AllergenId { get; set; }
        public virtual Allergen Allergen { get; set; }
    }

    public class Material
    {
        public Material()
        {
            Id = Guid.NewGuid();
        }

        public Guid Id { get; set; }
        public string Name { get; set; }
        public virtual ICollection<AllergenQuantityMaterial> AllergenQuantityMaterial { get; set; }
    }

    public class Allergen
    {
        public Allergen()
        {
            Id = Guid.NewGuid();
        }

        public Guid Id { get; set; }
        public string Name { get; set; }
        public virtual ICollection<AllergenQuantityMaterial> AllergenQuantityMaterial { get; set; }
    }

}

@miroslavsiska
Copy link

@divega
My complete classes:

public class AllergenQuantity
{              
    public Guid UnitId { get; set; }
    public virtual Unit Unit { get; set; }         

    [Display(Name = "Množství obsahu")]
    public decimal Quantity { get; set; }
}


 public class Unit 
{
    public Unit()
    {
        this.Id = Guid.NewGuid();
    }
    public virtual ICollection<AllergenQuantity> AllergenQuantity { get; set; }

    [Key]
    public Guid Id { get; set; }     

    [Display(Name = "Jednotka")]
    public string Name { get; set; }

    [Display(Name = "Typ Jednotky")]
    public string UnitType { get; set; }

    [Display(Name = "Použité v kulturách")]
    public string Cultures { get; set; }
}


  public class TenantDbContext : IdentityDbContext<TenantUser, TenantRole, Guid>
{
    private string _connectionString { get; set; }

    public TenantDbContext(DbContextOptions<TenantDbContext> options) : base(options) 
    {
        this._connectionString = "CON STRING";
    }

    public TenantDbContext()       
    {
    }   
    public TenantDbContext(string ConnectionString)
    {
        this._connectionString = ConnectionString;
    }
    public static TenantDbContext Create(string ConnectionString)
    {
        return new TenantDbContext(ConnectionString);
    }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    { 
        optionsBuilder.UseSqlServer(_connectionString);  
    }

    //  BASE
    public DbSet<Unit> Unit { get; set; }

    //  Stock
    public DbSet<Allergen> Allergen { get; set; }  
    public DbSet<AllergenQuantityMaterial> AllergenQuantityMaterial { get; set; }        //Inherited from AllergenQuantity
    public DbSet<Material> Material { get; set; }

    protected override void OnModelCreating(ModelBuilder builder)
    {      
        base.OnModelCreating(builder);


        //builder.Ignore<AllergenQuantity>();   // IF UNCOMENTED =  OTHER EXCEPTION: Object reference not set to object instance...


        //Composites keys mapping
        builder.Entity<AllergenQuantityMaterial>().HasKey(l => new { l.MaterialId, l.AllergenId });  
    }
}

If is this new Issue, I send you my complete solution... But according your comment. maybe is problem this:
public Guid UnitId { get; set; }
public virtual Unit Unit { get; set; }

@divega
Copy link
Contributor

divega commented Nov 23, 2015

Yes, I think it would be very useful to have this as a new issue.

From what I see it must be the Unit.AllergenQuantity collection navigation property what is causing AllergenQuantity to be included in the model.

If you are actually planning to use that navigation property to hold instances of different derived types of AllergenQuantity then you cannot get away with not having AllergenQuantity as part of the model and define the entity key on it. The other alternatives you have are:

  1. Remove the property
  2. Ask EF to ignore it
  3. Configure it explicitly (using casts in the HasMany() API call) to hold instances of a more specific derived type.

Regardless of that, the exception you are getting should be improved.

The NullReferenceException from the call to Ignore<T>() sounds like a new issue as well.

@divega
Copy link
Contributor

divega commented Nov 23, 2015

Yes, I think it would be very useful to have this as a new issue.

@miroslavsiska Sorry, what I meant is to ask you to open a new bug with this information. But don't worry. I can do it myself.

@markolbert
Copy link

I recently ran across this problem, and tried to solve it by calling Ignore() before doing anything else in OnModelCreating(). Unfortunately, that doesn't solve the problem -- System.Object still gets discovered.

However, applying the NotMapped annotation to the property in the class definition does solve the problem.

It seems odd that the annotation approach works but the fluent approach does not. Not sure why that is.

@divega
Copy link
Contributor

divega commented Apr 22, 2016

@markolbert what version of the bits are you using? This was fixed post RC1.

@markolbert
Copy link

markolbert commented Apr 22, 2016

Diego,
I’m using 7.0.0-rc1-final (the “stock” configuration that comes up when you create a new ASPNET5 web project in VS2015).
I’m open to trying something more recent. What’s the easiest way to do that?

@divega
Copy link
Contributor

divega commented Apr 22, 2016

@markolbert RC2 hasn't shipped the closest we have is staged in this NuGet feed: https://www.myget.org/F/aspnetrelease/api/v3/index.json

(More information about the different feeds at https://github.com/aspnet/Home/wiki/NuGet-feeds)

I don't think we have any comprehensive guidance on how to move an existing RC1 project to RC2 yet.

@ajcvickers ajcvickers removed this from the 1.0.0-rc2 milestone Oct 15, 2022
@ajcvickers ajcvickers added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Oct 15, 2022
@ajcvickers ajcvickers added this to the 1.0.0 milestone Oct 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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

No branches or pull requests

9 participants