New Posts New Posts RSS Feed: When is InverseProperty required
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

When is InverseProperty required

 Post Reply Post Reply
Author
gregweb View Drop Down
DevForce MVP
DevForce MVP
Avatar

Joined: 10-Sep-2009
Location: Clearwater, Fl
Posts: 253
Post Options Post Options   Quote gregweb Quote  Post ReplyReply Direct Link To This Post Topic: When is InverseProperty required
    Posted: 09-Feb-2013 at 12:09pm
I have a code first project with a DbInitializer to seed the database.

It has three Entities, a Contact, a Rate, and a RateType table.

There is one FK relationship between the Rate and the Contact.

If I run the project without seeding the data, the tables are created and the FK is created properly.

However, if I try to seed the database, when a contact is added an error is thrown which says "Could not set the property/field Rates" There is an inner exception of Object Ref not set to Instance of Object.

Looking in the debug window at the contact when it is being created shows that the Rates property is null.

However, if I add the InverseProperty on Rates, then it all works fine.

        [DataMember]
        [InverseProperty("ContactEntity")]
        public RelatedEntityList<Rate> Rates { get; internal set; }

So I don't get it. When is InverseProperty required? TempHire doesn't use the property, and I can get it to generate a SQL Server db just fine.

Greg
Back to Top
mgood View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 18-Nov-2010
Location: Emeryville, CA
Posts: 583
Post Options Post Options   Quote mgood Quote  Post ReplyReply Direct Link To This Post Posted: 09-Feb-2013 at 2:11pm
The attributes are required if you don't name things according to EF Code First Conventions and/or don't describe the foreign key relationships using the fluent API. TempHire names everything according to convention, that's why you don't see the ForeignKeyAttribute and InversePropertyAttribute.

You can find more on the EF CF conventions at the following link. One thing to watch out for, DevForce requires foreign key properties in addition to the navigation properties, otherwise you gonna get problems. In EF, foreign key properties are optional, but recommended. 

http://msdn.microsoft.com/en-US/data/jj679962 
Back to Top
gregweb View Drop Down
DevForce MVP
DevForce MVP
Avatar

Joined: 10-Sep-2009
Location: Clearwater, Fl
Posts: 253
Post Options Post Options   Quote gregweb Quote  Post ReplyReply Direct Link To This Post Posted: 11-Feb-2013 at 5:42am
Thanks Marcel, this is now working.

Edited by gregweb - 13-Feb-2013 at 5:37am
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down