Adding an existing database table to a EF6 Code first repository - entity-framework

I have an Entity Framework 6 code first application. As a one-off, I want to add a entity class based on a table that has been added externally to the database originally generated from the code first application. I know how to do this manually, but I wondered if there is a tool or method that could generate the class for me.

You can make a new project, right click on it -> Add new item -> ADO.NET Entity Data Model -> Database first
Then let entity framework generate models for your database and just copy the one you need to your project. That's probably the easiest solution :)

Related

Entity Framework - Conext.tt and Model.tt files missing

I'm new to Entity Framework and adding a 'ADO.NET Entity Data Model' item to my Azure Function for a Database table and only two out of the four files I was expecting were generated automatically. Solution explorer screenshot below of what was created vs expectations. What I am I missing so the Entity Class and Context files will generate automatically?
I have Visual Studio version 16.11.5 installed and I am using Entity Framework version 6.4.4
What was created
enter image description here
What I was expecting
enter image description here
Figured this out. The problem I had was I was creating the Entity Model within my Azure Function. I created a separate .NET Framework Class Library and all the model files created as expected.

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.

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

Added new entity to model - although it doesnt show up in generated code

I have added a new entity to our edmx. We have the ..dataaccess.g.cs file that is generated. This generated file does not seem to show the new entity I created, even when I delete it and rebuild our Entity project. I need this because it is a navigation property that we need to be able to access. any thoughts would be appreciated.
I was missing some needed namespaces in a partial classes that we created off this domain service