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

"Object reference not set to an instance of an object" within OeEfCoreEdmModelMetadataProvider.GetForeignKey #16

Closed
techniq opened this issue Jun 3, 2018 · 7 comments
Labels

Comments

@techniq
Copy link
Contributor

techniq commented Jun 3, 2018

I'm attempting to migrate from Microsoft.AspNetCore.OData to OdataToEntity but I'm receiving the following exception during dataAdapter.BuildEdmModelFromEfCoreModel() witihin OeEfCoreEdmModelMetadataProvider.GetForeignKey() when I added my relatively complex DbContext/Models (same one I'm currently using with Microsoft.AspNetCore.OData)

System.NullReferenceException: "Object reference not set to an instance of an object."
  at OdataToEntity.EfCore.OeEfCoreEdmModelMetadataProvider.GetForeignKey(PropertyInfo propertyInfo)
   at OdataToEntity.ModelBuilder.FKeyInfo.GetDependentStructuralProperties(OeEdmModelMetadataProvider metadataProvider, EntityTypeInfo dependentInfo, PropertyInfo dependentProperty)
   at OdataToEntity.ModelBuilder.FKeyInfo.Create(OeEdmModelMetadataProvider metadataProvider, Dictionary`2 entityTypes, EntityTypeInfo dependentInfo, PropertyInfo dependentNavigationProperty)
   at OdataToEntity.ModelBuilder.EntityTypeInfo.BuildProperty(Dictionary`2 entityTypes, Dictionary`2 enumTypes, Dictionary`2 complexTypes, PropertyInfo clrProperty)
   at OdataToEntity.ModelBuilder.EntityTypeInfo.BuildProperties(Dictionary`2 entityTypes, Dictionary`2 enumTypes, Dictionary`2 complexTypes)
   at OdataToEntity.ModelBuilder.OeEdmModelBuilder.BuildEdmModel()
   at OdataToEntity.EfCore.OeEfCoreDataAdapterExtension.BuildEdmModelFromEfCoreModel(OeDataAdapter dataAdapter)
   at OdataToEntityTest.Startup.ConfigureServices(IServiceCollection services) in /Users/techniq/Documents/Development/playground/odata-to-entity/OdataToEntityTest/Startup.cs:line 34
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Microsoft.AspNetCore.Hosting.ConventionBasedStartup.ConfigureServices(IServiceCollection services)
   at Microsoft.AspNetCore.Hosting.Internal.WebHost.EnsureApplicationServices()
   at Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()
   at Microsoft.AspNetCore.Hosting.WebHostBuilder.Build()
   at OdataToEntityTest.Program.BuildWebHost(String[] args) in /Users/techniq/Documents/Development/playground/odata-to-entity/OdataToEntityTest/Program.cs:line 21
   at OdataToEntityTest.Program.Main(String[] args) in /Users/techniq/Documents/Development/playground/odata-to-entity/OdataToEntityTest/Program.cs:17
@voronov-maxim
Copy link
Owner

Please send project reproduce error or sample DbContext

@techniq
Copy link
Contributor Author

techniq commented Jun 3, 2018

@voronov-maxim Thanks for the quick reply. When I try to load my full context I receive this (which has around 100 models). I was able to load just 2 models without issue (at least to be able to come across #17). Let me try to deduce which model(s) of the 100 might be the issue when I'll report back how it's configured. Might be a day or two before I can work it out. Thanks

@techniq
Copy link
Contributor Author

techniq commented Jun 4, 2018

@voronov-maxim Initially I found the issue is due to missing mappings in my model that do not appear to be handled gracefully when not found. For example in OeEfCoreEdmModelMetadataProvider if DependentToPrincipal or PrincipalToDependent is null on any of the fkey instances then the reference to get Name is throwing a null pointer exception.

I don't know if these could simply be fkey.DependentToPrincipal?.Name and fkey.PrincipalToDependent?.Name although it would be nice if a warning/error about the specific property/issue was raised to draw attention to the problem (or if we can ignore/work around the issue as these were not required when using Microsoft.AspNetCore.OData in these cases). When this happened in my model, it was from adding an ICollection<Account> reference but the missing property was completely unrelated to either of the other models involved.

I'm still digging in to get my full list of DbSets enabled and now running into an issue where the order returned from OeEfCoreEdmModelMetadataProvider#GetOrder is larger than propertyList in OeEdmModelMetadataProvider#SortClrPropertyByOrder.

I'll try to provide a simple model example once I fully understand the problem, but if you have any thoughts on how to provide better logging I would greatly appreciate it.

@voronov-maxim
Copy link
Owner

If EF build valid model from entity with invalid navigation property, then also need ignored this property

@techniq
Copy link
Contributor Author

techniq commented Jul 15, 2018

Sorry this took a bit to find and boil down to a simple model.

If I uncomment Dept.Accounts the problem goes away, but without it, I throw a NullReferenceException within OeEfCoreEdmModelMetadataProvider.GetForeignKey

public class MyFinanceDbContext : DbContext
    {
        public DbSet<Acct> Accts { get; set; }
        public DbSet<Dept> Depts { get; set; }
        public DbSet<Stat> Stats { get; set; }

        public MyFinanceDbContext(DbContextOptions options) : base(options)
        {
        }
    }

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

        //public virtual ICollection<Acct> Accts { get; set; } // Adding fixes issue
        public virtual ICollection<Stat> Stats { get; set; }
    }

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

        public int? DeptId { get; set; }
        public virtual Dept Dept { get; set; }
    }

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

        public int? DeptId { get; set; }
        public virtual Dept Dept { get; set; }
    }

To try to figure this one out, I had to throw in a bunch of debug logging and did some trail and error (and guessing)
image

@techniq
Copy link
Contributor Author

techniq commented Jul 15, 2018

@voronov-maxim see previous comment, I couldn't find a way to re-open the issue now that I have an example model

@techniq
Copy link
Contributor Author

techniq commented Jul 15, 2018

@voronov-maxim thanks, could you cut a NuGet release that includes this fix?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants