3

In our database, we have the following tables

Tags
  Id (int)
  Name (string)
  IsActive (bool)
  TagType (string)

and

DocumentStyles
  Id (int)
  Name (string)
  StyleRules (string)
  IsAvailable (bool)
  ThumbnailFileId (int nullable)
  ConceptTagId (int nullable)

With the EF 4.2 designer, I've created the appropriate entities, and am trying to link the foreign key of ConceptTagId to the Tag model.

When I add the association (0..1 to many from Tag to DocumentStyle), it correctly links the foreign key and adds the navigation property of ConceptTag to the DocumentStyle object. I do not want a navigation property on the Tag object.

However, when calling the following code in a repository

db.DocumentStyles.Include(d => d.ConceptTag).ToList();

the resulting query attempts to access a property DocumentStyle_ID on the Tag table, which does not exist, nor should it. The foreign key is the ConceptTagId on the DocumentStyle table.

Where does this id column come from, and how can I get rid of it?

From the properties window of the relevant association:

End1 Multiplicity: * of DocumentStyle
End1 Nav Property: ConceptTag
End2 Multiplicity: Zero of One of Tag
End2 Nav Property: {NULL} (its blank in the property)

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Thomas Jones
  • 4,892
  • 26
  • 34
  • if I am not mistaken, just by looking at your description of what's going on.. it's appears that there is pluralization going on, it's been a while since I've messed around with entiryframe work but by default If I recall, if you have a table called Document for example it would create the entity as Doucments you have to go thru the designer to change the name of the entity name(s) – MethodMan Dec 12 '11 at 16:03
  • Checkout this article to see if this may help if I am off base please let me know and I will edit / delete my entry.. thanks http://stackoverflow.com/questions/3444013/why-does-entity-framework-pluralize-and-capitalize-entity-class-names – MethodMan Dec 12 '11 at 16:20
  • Seems very strange. I've created almost the similar structure by myself (except repository) and it works with no problems. – Alex Kovanev Dec 12 '11 at 16:27
  • 1
    How did you create relation - did you follow [this](http://stackoverflow.com/questions/5444450/ado-net-ef-how-to-define-foreign-key-relation-in-model-first-approach/5445806#5445806) approach? Btw. model first is not for working with existing database. You should simply add referential constraint to the database and update model from database. – Ladislav Mrnka Dec 12 '11 at 17:00
  • Its not about the pluralization. I'll edit the post to reflect the right pluralization of the tables (as they are pluralized in our database). The relation was added by right clicking the entity in the designer and using "Add Association" wizard with the correct multiplicities. I have solved the issue with the fluent api, but cant answer the question myself, I'll leave another comment. As far as using model first, this is the path our architect has chosen for the moment, and we can't move to code first. – Thomas Jones Dec 12 '11 at 17:42
  • Under further investigation, it comes with breaking the convention of naming styles. To resolve the issue, I had to implement in the OnModelCreating event the following rule builder.Entity().HasOptional(ds => ds.ConceptTag).WithMany(); this allows the framework to know that Tag has no reciprocal property to expect in the relationship and it doesnt attempt to find the DocumentStyle_Id property in future queries – Thomas Jones Dec 12 '11 at 17:44

1 Answers1

1

Under further investigation, it comes with breaking the convention of naming styles. To resolve the issue, I had to implement in the OnModelCreating event the following rule

builder.Entity<DocumentStyle>().HasOptional(ds => ds.ConceptTag).WithMany(); 

This allows the framework to know that Tag has no reciprocal property to expect in the relationship and it doesnt attempt to find the DocumentStyle_Id property in future queries

Thomas Jones
  • 4,892
  • 26
  • 34