Attribute Not Showing Up On Entity Object RRS feed

  • Question

  • Entity Experts - I need your help!  I've got two tables in SQL Server with the following attributes:

    Table - SalesPersonnel (relationship - FK_Title_SalesTerritory_Title_SalesPersonnel)

    • SalesPersonnelId - int - Primary Key
    • Username - nvarchar(50)

    Table - SalesTerritory (relationship - FK_Title_SalesTerritory_Title_SalesPersonnel)

    • SalesTerritoryId - int - Primary Key
    • SalesPersonnelId - int
    • TerritoryDescription - nvarchar(100)

    I've created a new Entity object in VS2K8.  In my Entity Designer window, I've brought in the SalesPersonnel and SalesTerritory tables.  The tables were set up and a one-to-many between the SalesPersonnel and SalesTerritory tables was automatically set up.

    In my code, I'm using the Repository pattern to return a list of my SalesTerritory records.  I then iterate over the collection, and I would expect to say something like:

    var salesTerritoryRepository = new SalesTerritoryRepository();
                    var mySalesTerritories = salesTerritoryRepository.List();
                    foreach (var territory in mySalesTerritories)
                        var myValue = territory.SalesPersonnelId.ToString();

    but SalesPersonnelId is NOT an available property on the territory object.  If I inspect the properties available on the territory object, they are SalesPerson, SalesPersonReference, SalesTerritoryId.  I would expect to see SalesPersonnelId, but I don't.

    I have no idea what's going on.  I've set up Entities before between tables with relationships like this and I've never really had an issue like this before.

    Can anyone offer any tips or suggestions?



    Wednesday, April 7, 2010 7:51 PM


  • Actually in EF v1 (.NET 3.5 sp1) that is normal behavior.  Foreign keys are represented as associated objects....So you could go


    In the new version of EF 4.0 (they changed version nnumbers to coincide with the .NET Framework releases), you have the option of including the foreign keys in the model and therefore being able to do what you want.  Once you get used to it, for the most part, it's okay.

    Incidentally,  all those "var" declarations in your code makes it hard to understand...I had to look a few times before understanding it.

    Isn't it really

    //don't know what the first one returns so I guess var is okay
    var salesTerritoryRepository = new SalesTerritoryRepository();
    List<SalesTerritory> mySalesTerritories = salesTerritoryRepository.List();
    foreach (SalesTerritory territory in mySalesTerritories)
         int myValue = territory.SalesPerson.SalesPersonnelId;  
         Console.WriteLine("The value is {0}", myValue);  

    But, that's completely subjective and the source of much debate... http://www.infoq.com/news/2008/05/CSharp-var



    Thursday, April 8, 2010 2:44 AM