voronov-maxim / OdataToEntity

OData .net core
MIT License
153 stars 32 forks source link

Missing foreign key property throws NullReferenceException in FKeyInfo.GetEdmMultiplicity #24

Closed techniq closed 6 years ago

techniq commented 6 years ago

If a model is missing a foreign key property, you will have a NullReferenceException thrown in FKeyInfo.GetEdmMultiplicity. It would be nice if this was handled more gracefully / better logging to point at the issue.

public class MyFinanceDbContext : DbContext
    {
        public DbSet<Car> Cars { get; set; }
        public DbSet<State> States { get; set; }

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

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

        // Missing: `public int StateId { get; set; }`
        public virtual State State { get; set; }
    }

    public class State
    {
        public int Id { get; set; }
        public string Name { get; set; }

        public virtual ICollection<Car> Cars { get; set; }
    }

I ultimately threw in some debug logging which allowed me to determine a single null entry was in the dependentStructuralProperties array

image