0

I need to have a reference table to combine these two entities. I have built the following table, and unfortunately it is not working. Is there an easier way of doing this? Or can someone please explain why the entity will keeps throwing a null reference error, when I try and create it in the controller?

public class BridgeTable
    {
        [Key, Column(Order = 0)]
        public int Entity1ID { get; set; }

        [Key, Column(Order = 1)]
        public string Entity2ID { get; set; }

        [Required]
        public virtual Entity1 entity1 { get; set; }

        [Required]
        public virtual Entity2 entity2 { get; set; }

    }

public class Entity1
    {
        public int ID { get; set; }

        …

        public virtual List<BridgeTable> bridgeTable { get; set; }
    }

public class Entity2
    {
        public int ID { get; set; }

        …

        public virtual BridgeTable bridgeTable { get; set; }
    }

My model builder:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<BridgeTable>().HasRequired<Entity2>(e => e.bridgeTable).WithRequiredDependent();

            base.OnModelCreating(modelBuilder);
        }

Throws this exception:

Object reference not set to an instance of an object.

In my controller post method:

BridgeTable bridgeTable = new BridgeTable {

        public DbSet<Entity1> entity1{ get; set; }

        public DbSet<BridgeTable> bridgeTable { get; set; }

        public DbSet<Entity2> entity2 { get; set; }
Rasen244
  • 119
  • 1
  • 2
  • 11
  • Do you need additional fields in your bridge table? Or you just want implement many to many relation? – Pavel Nov 05 '14 at 19:04
  • possible duplicate of [What is a NullReferenceException and how do I fix it? (also known as Object reference not set to an instance of an object)](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it-also-known-as-object-refe) – Lasse V. Karlsen Nov 06 '14 at 10:53
  • I figured out what was happening, one of the things I was setting was an object. I neglected to put a hiddenfor in the view to pass it along its id so it was causing null. I posted this, because I couldn't figure out why it was saying that the object itself was a null reference, I thought it may have had to do with something related to how I was creating the object. – Rasen244 Nov 07 '14 at 21:14

1 Answers1

0

The error, "Object reference not set to an instance of an object" is just a rather obtuse way of saying you attempted to reference a property off of a variable that resolved to null at runtime. Consider the following:

Foo foo = db.Foos.Find(id);
Console.WriteLine(foo.Bar);

In the above, foo is supposed to be an instance of Foo, which does have a property Bar (for the sake of example), so the code compiles fine. However, once the code actually runs, if no Foos were found in your table with the particular id, foo is set to null, which does not have a property Bar. As a result, you get the "Object reference blah blah" error message.

If the error is triggering on the line where you have BridgeTable bridgeTable = new BridgeTable {, most likely, you're initializing one of the properties with a variable evaluating to null that you're reference a property or method on. For example:

BridgeTable bridgeTable = new BridgeTable {
    Entity1ID = entity1.ID
    ...
};

And, entity1 is actually null. Run the code in debug mode and inspect all the variables you're trying to initialize with. Fix the null, or coalesce the null somehow (entity1 == null ? 0 : entity1.ID), and you'll be fine.

Chris Pratt
  • 207,690
  • 31
  • 326
  • 382
  • Thanks, I figured out what was happening, one of the things I was setting was an object. I neglected to put a hiddenfor in the view to pass along its id so it was causing null. – Rasen244 Nov 07 '14 at 21:15