none
Entity Framework DbContext generator fails when renaming classes RRS feed

  • Question

  • Hi,

    I created my entity model and added the DbContext generator as the code generation item, so I can use the EF code-first approach (the entity model is used only for reverse engineering the database and generating the POCO classes).

     

    The problem is, my database objects all use lower case letters, but I want my POCO classes to use Pascal case. So, if I have previously added the code generation item (T4 template) and I rename any class, the T4 template finds an error saying that the file could not be found, and then the only fix I know is, rename everything I need before adding the code generation item.

    And, my question is, are there any different and probably better way to allow classes renaming with the T4 template already assigned?


    If you found this post helpful, please "Vote as Helpful". If it actually answered your question, remember to "Mark as Answer".

    Se achou este post útil, por favor clique em "Votar como Útil". Se por um acaso respondeu sua dúvida, lembre de "Marcar como Resposta".

    Sunday, December 11, 2011 1:06 AM

Answers

  • Hi Juliano,

    Welcome!

    I think we can change edmx file to rename class after runing T4 Generator(Just modify and save-the generator will rerun automatically). If you just rename in your generated code, when Code First load the metadata from the connection, it will couldn't find the mapped properties. If you just want to modify the generated class, you can delete the edmx file and change the connection in your config file like-->

    <add name="EFTestEntities" connectionString="Data Source=.\sqlexpress;Initial Catalog=EFTest;Integrated Security=True;" providerName="System.Data.SqlClient" />

    In order to map to database, we should add Fluent API, for rename:

    modelBuilder.Entity<Employees1>().ToTable("Employees");

    Have a nice day.

     


    Alan Chen[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.

    Monday, December 12, 2011 3:56 AM
    Moderator
  • Hi Juliano,

    I got your point, you can try this link, it should work in your scenario: http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/958aabc6-cd3e-4b46-b0b4-7ae6d11308a8

    Pay attention to my post!

    Have a nice day.


    Alan Chen[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.

    Tuesday, December 20, 2011 6:46 AM
    Moderator

All replies

  • Hi Juliano,

    Welcome!

    I think we can change edmx file to rename class after runing T4 Generator(Just modify and save-the generator will rerun automatically). If you just rename in your generated code, when Code First load the metadata from the connection, it will couldn't find the mapped properties. If you just want to modify the generated class, you can delete the edmx file and change the connection in your config file like-->

    <add name="EFTestEntities" connectionString="Data Source=.\sqlexpress;Initial Catalog=EFTest;Integrated Security=True;" providerName="System.Data.SqlClient" />

    In order to map to database, we should add Fluent API, for rename:

    modelBuilder.Entity<Employees1>().ToTable("Employees");

    Have a nice day.

     


    Alan Chen[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.

    Monday, December 12, 2011 3:56 AM
    Moderator
  • Hi,

    I am writing to check the status of the issue on your side. Would you mind letting us know the result of the suggestions?

    If you need further assistance, please feel free to let me know. I will be more than happy to be of assistance.

    Have a nice day.


    Alan Chen[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.

    Friday, December 16, 2011 6:49 AM
    Moderator
  • Hi Alan,

    I don't know if I explained well the problem or if I misunderstood your answer.

     

    My database has a table called "employees" (lower case letters), and when I generate the model in my EDMX file, the entities are created in lower case too. So I went to the EDMX model diagram and renamed this entity so the first letter become upper case. Although, this simple change caused the error when I pressed the save button, saying that the file has not been found.

     

    This is not a critical problem, but kind of annoying IMHO.

     

    If you have any suggestions.

     

    Thank you and have a nice day too.


    If you found this post helpful, please "Vote as Helpful". If it actually answered your question, remember to "Mark as Answer".

    Se achou este post útil, por favor clique em "Votar como Útil". Se por um acaso respondeu sua dúvida, lembre de "Marcar como Resposta".

    Friday, December 16, 2011 10:38 AM
  • Hi Juliano,

    I got your point, you can try this link, it should work in your scenario: http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/958aabc6-cd3e-4b46-b0b4-7ae6d11308a8

    Pay attention to my post!

    Have a nice day.


    Alan Chen[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.

    Tuesday, December 20, 2011 6:46 AM
    Moderator