Create databasebase with entity framework code first approach - entity-framework

I do not want that my database is created when I insert a record. One reason is I do not want to create sample application, do an insert just to have a database...
I want that my database is created via the package manager console.
How can I do that?

It's a little unclear what you're asking, but I think you're trying to prevent the database from being created automatically from the application code.
The database is created on the first use of the DataContext. If you don't want the database to be created on the first use of the data context you can add Database.SetInitializer<YourContextClassHere>(null); to your Global.asax.cs file.
Then you could use the Update-Database cmdlet in Visual Studio to run the pending migrations including creating the database.

Related

Entity Framework - Update database manually

I am new to entity framework and using EF 6.1.1... using code first approach i have generated the database..
When i run my application from Visual Studio in my local machine..
and update a record from my application.. it immediately reflects in ui immediately..
but if i update the records in database manually.. then the changes are not reflected immediately in ui.. i need to rebuild the solution to and run it again to bring the updated data to ui..
Just need to know if this is how entity framework works?? if yes, is there a way to make the ui to sync with database even for manual updates without rebuilding the solution..
Please let me know if you need any other information..

Updating Database model (.edmx) Model First

I am having a few problems with updating the model in EF using Model First.
I have exhausted my efforts with the suggestions of adding migrations (which is normally done using Code First).
The Package Manager Console prints the message
Creating a DbModelBuilder or writing the EDMX from a DbContext created using Database First or Model First is not supported. EDMX can only be obtained from a Code First DbContext created without using an existing DbCompiledModel.
I have also tried this suggestion
Updating model in EF Database First project
However nothing happens when i click "Run Custom Tool"
Any suggestions please on how to update the model to reflect the changes that have been added to the database.
Open your model
right click and you will find an option Update Model from database
Click update model from database than a window will open like this:
So choose the modified table to update them in model.

The model backing the 'POSContext' context has changed since the database was created

I am developing winform application using entityframework in visual studio 2012 with database first approach. Suddenly I am facing the following error:-
The model backing the 'POSContext' context has changed since the database was created. Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?LinkId=238269).
Can any please immediately help me to resolve this issue. I shall be very thankful for timely help.
Thanks.
make sure that you don't have a database initialized. Only have to call it once so you could put it in a static constructor of your DbContext class
Database.SetInitializer<YourDbContext>(null);

How to disable code-first feature in EF (MVC4 Visual Studio 2012)

How to disable code-first feature in EF (Visual Studio 2012)
I am using Visual Studio 2012, MVC4 (Internet application template).
I want to use EF, but not with its code-first feature. I would want the application to error out, rather than create or modify my database based on my code. (i just can not live with this feeling of my database being changed behind the scenes ... i want the application to use the exact db i have created ... and if there is any thing that has to be changed, i'll do it my self)
is this possible with the new Ef (VS2012)?
i have seen many people asking this, but so far i am unable to find the answer.
You can use Code First and ensure that your database never gets updated or overwritten when you change your model by setting the database initializer to null:
Database.SetInitializer<MyDbContext>(null);
It's a static method of the Database class and should be called at the beginning of your application, for example in global.asax or in a static constructor of your context class. Doing this you have to change model class and database schema manually so that they match.
You can also use the Reverse Engineer feature to create a Code First model from an existing database. It is explained here: http://msdn.microsoft.com/en-us/data/jj200620
Or if you don't want to use Code First at all and work with a model designer you can use the Database First approach, explained here: http://msdn.microsoft.com/en-us/data/jj206878
An overview about all the possible options is here: http://msdn.microsoft.com/en-us/data/ee712907.aspx

EF Code First not working on deploy hosting

I've an application using MVC and Code First for persistence.
Everything works fine in my development, but when I'm uploading to server, it doesnt work.
In any place i try to create a database, but it keeps me returning the following error: CREATE DATABASE permission denied in database 'master'.
The only thing that i do is override the OnModelCreating method just to map my app.
Anyone has this error?
Thanks
For a tutorial series that shows how to publish your Code First database and prevent Code First from trying to re-create the database in production, see
http://www.asp.net/mvc/tutorials/deployment-to-a-hosting-provider
The third tutorial in the series shows how to set up the Web.config file. The second shows how to deploy if you are using SQL Server Compact, the tenth shows how to deploy to full SQL Server.
You'll need to publish your database out to your hosting provider. Visual Studio has an easy way of doing this. In the server explorer tab, you can navigate to your database, right click and choose publish to provider. By doing this, you will not only export the scheme of your database, but you can also export out all data, stored procs, views etc.
You will need to adjust your code so that you are no longer trying to create a database on code run. Typically this approach is used for development, and you are no longer in development if you're moving to a hosting company. The changes may be in your global.asax, the dbcontext of your solution and any other place where you modified it to create the scheme for the database.
Hope this helps you some, and good luck on your project.