File not found error when manually deleting a SQL Server Compact 4.0 file while using Entity Framework 4.3 Migrations

StackOverflow https://stackoverflow.com/questions/11387741

Pergunta

I am testing our application to use a SQL Server Compact 4.0 database. The application uses the database if it is there or builds it using Entity Framework 4.3 Migrations when it is absent.

However, when I delete the file when the program is idle and then perform an action that will cause database reads I get a File not found error. This is hardly a real world scenario, but I do want to understand what is going on.

It seems that somewhere at the background the framework is keeping track of the fact that there has already been a check on whether the database needed to be created. I am not sure if I like that if that indeed is the case.

So, why is the database created when it is first accessed, but not recreated while the program runs? ( I understand there will be many good reasons for this, but I want to know where the implementation lies).

Foi útil?

Solução

Because database creation is part of EF initialization - initialization usually happens only once per every context type when the context is used to retrieve or persist data for the first time. Deleting database when your application runs is not EF use case so if you want to support this scenario you must handle it yourselves. You can try to reinitialize EF by using dbContext.Database.Initialize(true); Don't run this operation without a real need because initialization is very expensive operation and no thread in your app. can access the database when the initialization is running.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top