Entity Framework : How do you refresh the model when the db changes?

前端 未结 9 948
囚心锁ツ
囚心锁ツ 2020-12-01 01:56

If you build the edmx file from the database and then the db changes, how do you get the model to pick up the change?

Do you delete the whole model and regenerate or

相关标签:
9条回答
  • 2020-12-01 02:24

    Update CodeFirst Model is not possible automatically. I don't recommend either. Because one of the benefits of code first is you can work with POCO classes. If you changed this POCO classes you don't want some auto generated code to destroy your work.

    But you can create some template solution add your updated/added entity to the new model. then collect and move your new cs file to your working project. this way you will not have a conflict if it is a new entity you can simply adding related cs file to the existing project. if it is an update just add a new property from the file. If you just adding some couple of columns to one or two of your tables you can manually add them to your POCO class you don't need any extra works and that is the beauty of Working with Code-First and POCO classes.

    0 讨论(0)
  • 2020-12-01 02:24

    Here:

    1. Delete the Tables from the EDMX designer
    2. Rebuild Project/SLN (this will clear the model class)
    3. Update Model from Database(readd all the tables you want)
    4. Rebuild project/SLN (this will recreate your model class including the new columns)
    0 讨论(0)
  • 2020-12-01 02:30

    You need to be careful though, You need to setup the EDMX file exactly as it was before deleting it (if you choose the delete/regenerate route), otherwise, you'll have a naming mismatch between your code and the EF generated model (especialy for pluralization and singularization)

    Hope this will prevent you some headaches =)

    0 讨论(0)
提交回复
热议问题