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

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

Related

trouble scaffolding entity framework based controller in ASP.Net Core 2.0

I'm using Visual Studio 2017, v15.3.1 and am trying to follow a tutorial about creating some WebAPI endpoints with Entity Framework, as outlined here
In my case I'm using the good old Northwind SQL database instead of the author's blogging example, which I don't think should be an issue?
At any rate, when I get to the bit where I'm supposed to create a controller, I right-click the controllers folder in my solution, select 'API Controller with actions, using Entity Framework' and choose my EF model class (Orders, Employees, it doesn't seem to matter) and the Data Context which I generated successfully earlier in the tutorial.
When I select Add there's an attempt to scaffold, but I'm then told in a popup: There was an error running the selected code generator: 'No parameterless constructor defined for this object'.
I've looked at this error message online and others have had similar problems, but nothing they tried quite matches my scenario, and their solutions haven't fixed this issue.
My generated EF model classes do have parameterless constructors defined, so I'm not sure what the error is referring to?

IdentityServer3 Entity Framework migrate from v2 MembershipProvider

We use IdentityServer v2 in a production environment for over 2 years now. We'd like to move to v3 ou even 4, but I don't really see how we could migrate users account which are actually in a MembershipProvider DB to the new schema defined in the project IdentityServer3.EntityFramework. The problem is that I have no clue on how I could accomplish that. Anyone has ever done this ?
I don't see a scenario where I can add Scopes or Clients either. I guess that I have to create Scope object in code and save it to context, but since I'm not a pro in EF, I'm not really sure about the best practices. Can anyone point me to a sample ? The one in GitHub project doesn't seem to add Scopes or Clients, it just manage the schema with Migration (unless I'm mistaken).
EDIT : I got the sample working so I figured out the adding part in the Factory class. The Migration configuration part is explained here. There's a comment block in the ClientConfiguration Seed method where it shows how to use the AddOrUpdate extension. The Configuration class extends DbMigrationsConfiguration so you use it when you run the Update-Database command as the ConfigurationTypeName parameter.
So only question left is : how to update from MembershipProvider ?

add scaffolded item wizard cannot resolve data model class

everyone.
I'm trying to create new Scaffolded item for simple ASP.NET Wep API application which should be based on domain object class and DbContext derivative in separate assembly. The assembly is in the solution, target app has a reference to it (and manually created code which invokes the classes from my lib is build up without any errors) and, obviously, the classes I've mentioned have access modifier public.
The problem is wizard for creating new scaffolded item cannot see my model classes. (By the way, when the model classes were in another ASP.NET MVC5 app the wizard worked well.) I'm using Visual Studio 2013 Update5.
How to fix this? Any workaround would be helpful too!
Sorry for disturbing.
the origin of the problem seems to be that I've move my data model classes from ASP.NET MVC app into my class library in wrong way.
I've not just cut/paste them, but copy-paste, cut-paste (confirmed replacement).
this is the only thing which could cause the problem (although I still don't know the details).
Never the less I've started from the beginning again and cut/past the data model files initially - everything seems to work fine now.

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);