locked
How to Change Navigation Property Name with t4 Template RRS feed

  • Question

  •     Generally, we desgine database tables and then generate edmx. But when the tables is designed like this:

        Product:

    • ProductID
    • CategoryEnumID
    • QuelityEnumID
    • CompanyEnumID

        Enum:

    • EnumID
    • Display
    • Value

        In the Product table, there exists 3 foreign keys related with Enum table. So in the edmx file, Product Entity Object has 3 Navigation Property, Enum1, Enum2, Enum3. Enum1, Enum2, Enum3 is not convenient whe we program and I want to change the navigation property names to CategoryEnum, QuelityEnum, CompanyEnum.

        Now, I want to change the Navigation Property Name with t4 file. (How to do this, please reference this paper .)

        I do this successfully, but when I run my code, a exception show like this:

    System.Data.MappingException was unhandled by user code
    Message=CLR. "Eps.Dao.BusinessPolicy" 's member "ServiceFeeTypeEnumDictionary" doesn't exist in Conceptual Model Type "Eps.Dao.BusinessPolicy"。
        I think whether that means the navigation property's name must match with the type name in conceptual model. If it's true, we should modify the conceptual model? That is horrible.

        How to solve the problem or Is there another way to solve the problem? Thanks.

    • Edited by JohnnyFee Monday, February 28, 2011 7:42 AM
    • Moved by Rowan Miller Wednesday, March 9, 2011 7:08 PM Moving from pre-release as this relates to a released product. (From:ADO.NET Entity Framework and LINQ to Entities (Pre-Release))
    Monday, February 28, 2011 1:15 AM

Answers

  • If you are using POCO, then the conceptual model names and the class/proeprty names must match. If you update your EDMX file to have the correct/desired navigation proeprty names, the navigation properties will have correct names when they are code generated. Is there a reason you don't want to update the edmx file?

    Jeff


    This posting is provided "AS IS" with no warranties, and confers no rights.
    • Proposed as answer by Jackie-Sun Monday, March 14, 2011 2:40 AM
    • Marked as answer by Jackie-Sun Friday, March 18, 2011 3:31 AM
    Monday, February 28, 2011 7:37 AM
  • Hello Johnny,

     

    Thanks for your post.

    As I know, you could use the self-tracking entities.  Starting with the Entity Framework version 4, self-tracking entities can help you track changes in any tier. A self-tracking entity is an entity built from a Text Template Transformation Toolkit (T4) template that generates entity types that have the ability to record changes to scalar, complex, and navigation properties.

    The ADO.NET Self-Tracking Entity Generator Template is available in Visual Studio 2010. For more information, see Walkthrough: Serialize Self-Tracking Entities.

    I hope this can help you.

     

    Have a nice day, 


    Jackie Sun [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Proposed as answer by Jackie-Sun Monday, March 14, 2011 2:40 AM
    • Marked as answer by Jackie-Sun Friday, March 18, 2011 3:31 AM
    Thursday, March 10, 2011 3:08 AM

All replies

  • If you are using POCO, then the conceptual model names and the class/proeprty names must match. If you update your EDMX file to have the correct/desired navigation proeprty names, the navigation properties will have correct names when they are code generated. Is there a reason you don't want to update the edmx file?

    Jeff


    This posting is provided "AS IS" with no warranties, and confers no rights.
    • Proposed as answer by Jackie-Sun Monday, March 14, 2011 2:40 AM
    • Marked as answer by Jackie-Sun Friday, March 18, 2011 3:31 AM
    Monday, February 28, 2011 7:37 AM
  • Thank you for your reply. I'm not using POCO, just generel edm. What I'm doing is that, after code generated, I want to modify the navingation property name with code generation item. For example, change Product's navigation property names "Enum1, Enum2, Enum3" to "CategoryEnum, QuelityEnum, CompanyEnum".
    • Proposed as answer by Jackie-Sun Monday, March 14, 2011 2:40 AM
    • Unproposed as answer by Jackie-Sun Monday, March 14, 2011 2:40 AM
    Monday, February 28, 2011 9:01 AM
  • Hello Johnny,

     

    Thanks for your post.

    As I know, you could use the self-tracking entities.  Starting with the Entity Framework version 4, self-tracking entities can help you track changes in any tier. A self-tracking entity is an entity built from a Text Template Transformation Toolkit (T4) template that generates entity types that have the ability to record changes to scalar, complex, and navigation properties.

    The ADO.NET Self-Tracking Entity Generator Template is available in Visual Studio 2010. For more information, see Walkthrough: Serialize Self-Tracking Entities.

    I hope this can help you.

     

    Have a nice day, 


    Jackie Sun [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Proposed as answer by Jackie-Sun Monday, March 14, 2011 2:40 AM
    • Marked as answer by Jackie-Sun Friday, March 18, 2011 3:31 AM
    Thursday, March 10, 2011 3:08 AM
  • Hello

    I have this problem too, do you find any way?

    Monday, October 7, 2013 11:42 AM