0

I have been struggling for a while now to convert a fairly large EntityFramework database created in model first to codefirst. I have a problem that I cant seem to resolve. I am getting an Object reference not set to an instance of an object with the following procedures on the call stack.

ModelConfiguration.Configuration.Types.EntityTypeConfiguration.Configure
ModelConfiguration.Configuration.ModelConfiguration.ConfigureEntities

to simplify the post I have created a test project that boils the problem down to its simplest form.

I have 3 classes

  • a which has an has an optional b and an optional c
  • b which has a collection of a's, and a colleciton of c's
  • c which has an optional b and a collection of a's

    public class a
    {
        public int Id { get; set; }
        [Required]
        public string name { get; set; }
    
        public virtual b b { get; set; }
        [ForeignKey("b")]
        public int? b_Id { get; set; }
    
        public virtual c c { get; set; }
        [ForeignKey("c")]
        public int? c_Id { get; set; }
    }
    
    public class b
    {
        public int Id { get; set; }
        public string name { get; set; }
    
        public virtual ICollection<a> a_s { get; set; }
        public virtual ICollection<c> c_s { get; set; }
    }
    
    public class c
    {
        public int Id { get; set; }
    
        public virtual b b { get; set; }
        [ForeignKey("b")]
        public int? b_Id { get; set; }
    
        public virtual ICollection<a> a_s { get; set; }
    }
    
    public class MyContext : DbContext
    {
        public DbSet<a> a { get; set; }
        public DbSet<b> b { get; set; }
        public DbSet<c> c { get; set; }
    
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<a>()
                .HasOptional(m => m.b)
                .WithMany(m => m.a_s);
    
            modelBuilder.Entity<b>()
                .HasMany(m => m.c_s)
                .WithRequired(m => m.b);
    
            modelBuilder.Entity<c>()
                .HasMany(m => m.a_s)
                .WithOptional(m => m.c);
    
            base.OnModelCreating(modelBuilder);
        }
    
    }
    

    when I execute the code var a = from o in db.a select o, I get the error described above. There is absolutely no information on what is hapenning, so I really dont know where to turn. Can anyone help me solve this problem, as I really want to move away from Model First.

    namespace MvcApplication2.Controllers
    {
        public class HomeController : Controller
        {
            public ActionResult Index()
            {
                var db = new MyContext();
    
                var a = from o in db.a select o;
    
                return View();
            }
    
        }
    }
    

2 Answers 2

3

The mixup of fluent configuration and data annotation caused this problem. EF team should have handled this exception and given a meaningful error message.

Remove data annotations and use fluent configuration as follows

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

    [Required]
    public string name { get; set; }

    public virtual b b { get; set; }

    public int? b_Id { get; set; }

    public virtual c c { get; set; }

    public int? c_Id { get; set; }
}

public class b
{
    public int Id { get; set; }
    public string name { get; set; }

    public virtual ICollection<a> a_s { get; set; }
    public virtual ICollection<c> c_s { get; set; }
}

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

    public virtual b b { get; set; }

    public int? b_Id { get; set; }

    public virtual ICollection<a> a_s { get; set; }
}

public class NreContext : DbContext
{
    public DbSet<a> a { get; set; }
    public DbSet<b> b { get; set; }
    public DbSet<c> c { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<a>()
            .HasOptional(m => m.b)
            .WithMany(m => m.a_s)
            .HasForeignKey(m => m.b_Id);

        modelBuilder.Entity<a>()
            .HasOptional(m => m.c)
            .WithMany(m => m.a_s)
            .HasForeignKey(m => m.c_Id);

        modelBuilder.Entity<c>()
            .HasOptional(m => m.b)
            .WithMany(m => m.c_s)
            .HasForeignKey(m => m.b_Id);

        base.OnModelCreating(modelBuilder);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

that fixes the issue. Thanks. It seems that you should be able to mix and match annotations and fluent syntax but i guess i will switch to fluent for everthing
1

Try putting 'a' into local memory:

var a = from o in db.a.ToList() select o;

1 Comment

That gives the same problem. The issue is in creating the model.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.