EF attempt to put wrong type of Navigation Property on Entity
Today I started getting the error below. This code worked yesterday.
It looks like it wants to create a Article.Authors as ArticleMember instead ofIList<ArticleMember>
*Error
error : [Microsoft.AspNet.Diagnostics.ErrorHandlerMiddleware] An unhandled exception has occurred: The navigation property 'Authors' cannot be added to entity type 'CodeProject.Modules.Articles.EF.Article' because its CLR type 'Sys
tem.Collections.Generic.IList`1[[CodeProject.Modules.Articles.EF.ArticleMember, CodeProject.Modules.Articles.EF, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' does not match the expected CLR type 'CodeProject.Modules.Artic
les.EF.ArticleMember'.
System.InvalidOperationException: The navigation property 'Authors' cannot be added to entity type 'CodeProject.Modules.Articles.EF.Article' because its CLR type 'System.Collections.Generic.IList`1[[CodeProject.Modules.Articles.EF.Ar
ticleMember, CodeProject.Modules.Articles.EF, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' does not match the expected CLR type 'CodeProject.Modules.Articles.EF.ArticleMember'.
at Microsoft.Data.Entity.Metadata.EntityType.AddNavigation(String name, ForeignKey foreignKey, Boolean pointsToPrincipal)
at Microsoft.Data.Entity.Metadata.Internal.InternalEntityTypeBuilder.Navigation(String navigationName, ForeignKey foreignKey, Boolean pointsToPrincipal, ConfigurationSource configurationSource, Boolean canOverrideSameSource)
at Microsoft.Data.Entity.Metadata.Internal.InternalEntityTypeBuilder.Navigation(String navigationName, ForeignKey foreignKey, Boolean pointsToPrincipal, ConfigurationSource configurationSource)
at Microsoft.Data.Entity.Metadata.Internal.InternalEntityTypeBuilder.Relationship(InternalEntityTypeBuilder principalEntityTypeBuilder, InternalEntityTypeBuilder dependentEntityTypeBuilder, String navigationToPrincipalName, String
navigationToDependentName, IReadOnlyList`1 foreignKeyProperties, IReadOnlyList`1 principalProperties, ConfigurationSource configurationSource, Nullable`1 isUnique, Nullable`1 isRequired, Func`2 onRelationshipAdding)
at Microsoft.Data.Entity.Metadata.Internal.InternalRelationshipBuilder.ReplaceForeignKey(EntityType principalType, EntityType dependentType, String navigationToPrincipalName, String navigationToDependentName, IReadOnlyList`1 forei
gnKeyProperties, IReadOnlyList`1 principalProperties, Nullable`1 isUnique, Nullable`1 isRequired, ConfigurationSource configurationSource)
at Microsoft.Data.Entity.Metadata.Internal.InternalRelationshipBuilder.ReplaceForeignKey(ConfigurationSource configurationSource, IReadOnlyList`1 dependentProperties, IReadOnlyList`1 principalProperties, Nullable`1 isUnique, Nulla
ble`1 isRequired)
at Microsoft.Data.Entity.Metadata.Internal.InternalRelationshipBuilder.NavigationToDependent(String navigationToDependentName, ConfigurationSource configurationSource, Nullable`1 strictPreferExisting)
at Microsoft.Data.Entity.Metadata.Builders.ReferenceNavigationBuilder.InverseCollectionBuilder(String collection)
at Microsoft.Data.Entity.Metadata.Builders.ReferenceNavigationBuilder`2.InverseCollection(Expression`1 collection)
at CodeProject.Modules.Articles.EF.ArticleDbContext.<>c.<OnModelCreating>b__21_2(EntityTypeBuilder`1 b)
at Microsoft.Data.Entity.ModelBuilder.Entity[TEntity](Action`1 buildAction)
at CodeProject.Modules.Articles.EF.ArticleDbContext.OnModelCreating(ModelBuilder modelBuilder)
at Microsoft.Data.Entity.Internal.ModelSource.CreateModel(DbContext context, IConventionSetBuilder conventionSetBuilder, IModelValidator validator)
at Microsoft.Data.Entity.Internal.ThreadSafeDictionaryCache`2.GetOrAdd(TKey key, Func`2 factory)
at Microsoft.Data.Entity.Internal.DbContextServices.CreateModel()
at Microsoft.Data.Entity.Internal.LazyRef`1.get_Value()
at Microsoft.Framework.DependencyInjection.ServiceProvider.ScopedCallSite.Invoke(ServiceProvider provider)
at Microsoft.Framework.DependencyInjection.ServiceLookup.Service.ConstructorCallSite.Invoke(ServiceProvider provider)
at Microsoft.Framework.DependencyInjection.ServiceProvider.ScopedCallSite.Invoke(ServiceProvider provider)
at Microsoft.Framework.DependencyInjection.ServiceProviderExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
at Microsoft.Framework.DependencyInjection.ServiceProviderExtensions.GetRequiredService[T](IServiceProvider provider)
at lambda_method(Closure , ServiceProvider )
at Microsoft.Framework.DependencyInjection.ServiceProviderExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
at Microsoft.Framework.DependencyInjection.ServiceProviderExtensions.GetRequiredService[T](IServiceProvider provider)
*Related snippets:
modelBuilder.Entity<ArticleMember>(b =>
{
b.Table("Article_Member");
b.Key(am => new { am.ArticleId, am.MemberId });
b.Property(am => am.ArticleId).Column("ArticleId");
b.Property(am => am.MemberId).Column("MemberId");
// Looks like there is a problem here!!!
b.Reference(am => am.Article).InverseCollection(a => a.Authors)
.ForeignKey(am => am.ArticleId)
.PrincipalKey(a => a.Id);
b.Reference(am => am.Member).InverseCollection(m => m.Articles)
.ForeignKey(am => am.MemberId)
.PrincipalKey(m => m.Id);
});
public class ArticleMember
{
public int ArticleId { get; set; }
public virtual Article Article { get; set; }
public int MemberId { get; set; }
public virtual Member Member { get; set; }
public bool Verified { get; set; }
}
public class Article
{
public int Id { get; set; }
public byte ArticleTypeId { get; set; }
// Some Properties removed for brevity
public virtual IList<ArticleMember> Authors { get; } = new List<ArticleMember>();
public virtual IList<ArticleTagLink> ArticleTagLinks { get; } = new List<ArticleTagLink>();
}
...
I'm seeing this issue as well. It surfaced after attempting to pull in the latest nightlies.
Are you running it as a 64-bit process?
Yes, I am running Core 64.
Not sure if this helps but... I see this error when running unit tests; not during build. Additionally, I only see the error when running the unit tests via a gated check in on VSO. The tests run fine locally. These are tests that worked fine on the build server prior to pulling in the latest nightlies.
My local build and the build server definition are both set to AnyCPU. Based on this, because I'm running on a 64bit PC I assuming that it should be running as a 64-bit process. Based on this, the VSO build servers are also 64 bit and in theory the tests should also JIT to 64 bit code.
I'll try explicitly setting them to x64 and let you know what happens.
I've updated to dnx beta6-12120, and the latest VS, and it is working. Not sure which did it, but I suspect that there has been a fix in the EF code.
Fixed in 4f5b7182c119725efc38d86e4010ff6006be61d2
Hmmm... I'm running beta6-13586 and am still seeing this. matthewDDennis's comment about "the latest VS" has me wondering... Is there potentially an issue with VS 2013 projects? However, I'm not sure why this works locally and not via VSO builds.
beta6-13586 doesn't have this fix, you need beta6-13591 or higher. We have a problem with some other projects, so the latest builds are not published on the dev feed, you would need to use this one if you want the absolutely newest build (which might be broken): https://www.myget.org/F/aspnetvolatile/api/v2
ahh thanks... curious that it started working for @matthewDDennis w/beta6-12120. will give it a shot when something >= 13591 comes out
The version of EF I am running is beta6-13566 and it works. dnx is beta6-12120 win x86
Ok, I just confirmed that I am still getting the error on clr-x64 and coreclr-x64 but not on clr-x86 or coreclr-x86.
i can confirm that beta6-13655 fixed this issue for me. AnyCPU configured builds now work locally as well as on our build server. Thanks!