views:

24

answers:

2

I have inherited a medium sized database that we are trying to use with Entity Framework in a MVC2 rewrite we are working on. We used the existing database to generate the data model (.edmx file) and all was good. Until I realized that I could not use the dot notation access all the fields.

appointment.Employee.name   // works fine
appointment.Supervisor.name // this fails
appointment.SupervisorID    // works and is an int

When I looked closer I could see that the first case that worked was most likely a fluke. There are several columns in the table for things like SupervisorID, SecretaryID, DogGroomerID and so, on all pointing to the Employee table and labeled as foreign keys. It looks like Visual Studio almost did the right thing, it just named all this associations after the roles involved, then attached numbers to make them unique. So the folllowing code works:

appointment.Employee1.name // My name
appointment.Employee7.name // My Boss's Name
appointment.Employee5.name // Fluffy's groomer's name

But that really defeats the purpose. So what did I do wrong or what is misconfigured that would make Visual Studio generate such obviously dumb names? I tried changing them in the visual modeling tool and it worked. Renaming Employee7 to Supervisor had the desired effect but that is not really a scalable solution for me to spend the next 10 hours renaming things. What do we do when the database gets revved by the customer in the field again and I need to pull in new schema?

Thanks for any insight and help!

-Eric

A: 

In the context of ASP.NET MVC you can use DisplayNameAttribute to add metadata to your model. This blog post can get you started: http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-2-modelmetadata.html

marcind
Thanks, but I am not sure if that is my issue, or at least I am not seeing how to apply it. The model classes are being generated from the .edmx which is built from the database itself, only after that is there a place to attach such metadata. My problem is that the Navigation Properties are getting useless '`foo4` type names in the generation phase. (I think.) (I really am hoping for the PEBCAK explanation to come and save the day.)
Ukko
A: 

Coming back to this the real problem was at a step earlier in the tool chain. We just solved things bu editing the names in the .edmx file and being very careful about merging changes when the database changed. Not a good solution but you can only spend so much time on things.

Ukko