Manually editing database in code first entity framework - entity-framework

I have been trying out EF 4.1 (code first) with MVC 3. I am thinking ahead to when the application will need changes. I tested a couple of scenarios. I like the idea of manually editing the database when the model (my POCOs) would changed.
ASP.NET error when I change the model :
"The model backing the 'CTCMContext' context has changed since the database was created. Either manually delete/update the database..."
Now, it says that I can "manually update the database", but I did and still get the same error. Am I missing something !!?!
EDIT
Does this have to do with the model hash generate by EF ?

I have had some struggles with this as well. I found that when you let EF create your database for you that a table named dbo.EdmMetadata is created and this is where/how EF tracks the state of the model. I found that if you delete this table after the database has been initially created you will put things into "manual mode" where you can now manually add/remove columns, tables, etc. from your database and EF will not throw the error that you are seeing.
If however you want to keep having EF update your database as you make changes to your model, you will need to create and call a ContextInitializer class that inherits from either DropCreateDatabaseIfModelChanges or DropCreateDatabaseAlways depending upon the behavior that you want to have happen.

change the class with the new fieldnames, delete the table "EdmMetaData" then recompile your app.
You will be responsible on modifying the fieldname on your views.
it works for me.

As I can see, there aren't really any methods built-in EF for code-first data evolution.
For what was of my initial question, the answer lies in removing the schema generation/validation. It is only then that manually editing the code and database may work.
UPDATE :
EF 5.0 now support migrations

I know this has been marked as solved but in my case it didn't do the trick.
Once I deleted dbo.EdmMetadata I was getting a different error:
Model compatibility cannot be checked because the database does not contain model metadata. Ensure that IncludeMetadataConvention has been added to the DbModelBuilder conventions.
The way it worked for me was to remove the Initializer class from Application_Start:
void Application_Start(object sender, EventArgs e)
{
// MyDB.SetInitializer<MyContext>(new MyInitializer());
}

Related

Entity Framework Core 2.0 Dynamically Creating A Database

I am new to EF Core, and as I tried using it I found out that you need to add migrations for it to create a database from models. My question is, do we have another option aside from migrations to dynamically create the database on run time just like what it was in EF 6?
Thanks.
Until a seeding mechanism is provided in EF Core you can provide your own seeding mechanism.
In earlier phases of a project, when the database is not yet fixed, I don't care that data get lost. When I want to recreate the database dynamically I call the function below. By the setting of a parameter I determine if this "recreateDatabase"-function is called yes or no. The function is included in MyOwnDbContext class.
The seed function you need to write is very similar to the one you use in EF 6.
private static void recreateDatabase(YourOwnDbContext dbContext)
{
dbContext.Database.EnsureDeleted();
dbContext.Database.EnsureCreated();
seed(dbContext);
}

Code First from database with existing Designer from database

We've been working for along time using EF Designer from database, so switching to another approach is not an option for us.
Due to a legacy version of our app i need to create relationship between tables on EF level for the current version, so those relationships don't exist in the schema.
I understand that i can do do that using Code First from database approach, but the problem how do i map the fields and make these entities part of the main datacontext which we already extended its functionalities using another partial class.. the main problem now is just mapping the new entities fields
i can't create OnCreatingModel in a new datacontext class since it does exist in the auto generated datacontext class.
to sum up.. is there anyway to use both approaches on the same DAL? taking in mind modifying auto-generated classes is absolutely not an option.
Thanks!

Code First Migration

I have read the code first migration from msdn.But in case If my database team changes data base i.e. If some one add/update/delete some fields from a table how to migrate that changes?
I mean How my code first model update that change?
Please help me.I am using Entity Framework 6.1.
I didn't fully understand your problem because the real purpose of migration is Update Database changing fields or tables. You just need to launch:
add-migration NameOfMigration
and
Update-Database.
in package manager console.
As you can see in the migration called "NameOfMigration" that is created you have 2 different method: Up and Down. This allow you to Update your database with that changes (up) or come back (down).

EFCode First - Prevent Dropping of Database when a new Property is added in a Model

I need to add a new column / property to my database and my model without EF Code First dropping my database.
Is there a way to manually map a column to property so EF would not drop my database?
Update:
Got it to work by following this post on SO.
https://stackoverflow.com/questions/6049135/manually-editing-database-in-code-first-entity-framework
Basically you just need to:
Manually add the column to your database
Manually add the property to your model
Disable any database initializer in your DataContext
As long you're using anything beyond EF 4.3, you would use Migrations to do so. You can't do it manually because EF relies on the model that is stored in order to best do the mapping to SQL. When you make a change it refreshes the model. Any attempt to add something without changing the model will result in an exception.

Changing Entity Framework Entity Class Without Changing Schema

I have an application already in production using EF 5.0. I'm about to start on the next major revision. But before I do that, I'm trying to clean up a lot of my existing code.
One thing I want to change, is use a different class for one of my table entities. The new class is functionally identical to the previous. The only real difference is the name. E.g. ReceiptEntity will become Receipt.
This is to help simplify things, and stick to a simple naming convention.
However, EF Migrations are wanting to drop-recreate the table. This is not an option because the application is already in production. And I cannot allow for any data-loss.
Is there any way to change the Entity type without recreating the table that would make EF happy?
I was able to fix this by altering the generated migration.
The generated migration tried to create a table(one that already existed), then immediately drop it(which would of resulted in the data-loss).
I deleted all of the generated code, and simply 'Update-Database'ed an empty migration, and this solved the problem for me.