I'm learning Entity Framework (currently using EF6 beta) and I'm using the code first pattern on an existing database. The entities and DbContext
class are created automatically using a T4 template.
I would like to prevent the DbContext
from creating / altering anything into the database at runtime.
How can I do that?
This is usually caused by different threads using the same instance of DbContext, however instance members are not guaranteed to be thread safe. When concurrent access goes undetected, it can result in undefined behavior, application crashes and data corruption.
1 Answer. First, DbContext is a lightweight object; it is designed to be used once per business transaction. Making your DbContext a Singleton and reusing it throughout the application can cause other problems, like concurrency and memory leak issues. And the DbContext class is not thread safe.
EF and EF Core DbContext types implement IDisposable . As such, best practice programming suggests that you should wrap them in a using() block (or new C# 8 using statement). Unfortunately, doing this, at least in web apps, is generally a bad idea.
Don't dispose DbContext objects. Although the DbContext implements IDisposable , you shouldn't manually dispose it, nor should you wrap it in a using statement. DbContext manages its own lifetime; when your data access request is completed, DbContext will automatically close the database connection for you.
When you do enable-migrations command, you will be presented with folder /Migrations under which you can find file named Configuration.cs. Within constructor of that file, by the default, there is a property set to value:
AutomaticMigrationsEnabled = false;
Which will ensure that your database won't be migrated automatically, but rather by invoking each migration manually.
However, if your database is larger than your domain models, i.e. you're just operating on the portion of already existing database, then somewhere in your application start (if it is ASP.NET app, Application_Start event handler), you need to add the following code:
Database.SetInitializer<YourDbContext>(null);
Otherwise, Entity Framework will complain that there is a mismatch between database definition in your domain model and actual current state of the database.
EDIT:
If you want to be sure that YourDbContext is not attempting to change database, do this:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new YourCustomException("Code first changes are not allowed.");
}
EDIT again:
I'm trying to understand what scenario are you trying to accomplish. If you have existing database, and you want to update it but only manually, this is the approach:
Set the initialiser for your context to null
.
That should leave the database in it's existing state when the application starts up.
Note that you'll get an exception if you try to run the application after altering your dbContext class and/or your database schema, as your database schema will no longer match what the context expects.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With