Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity framework update model from database not generating cs class for newly added table in tt class

I am using entity framework 5 with visual studio 2012.

I do have an existing model. Now I want to add a new table to that existing model. For that I have opened the edxm file and using right click I updated the model successfully.

Now, in "Model Browser" under "EntityTypes" for model, I can see the table name exists. But in Solution Explorer it is not showing the auto-generated .cs file for the table I have added newly under .tt file.

I tried "run custom tool" but it has not generated the class. Also have restarted the Visual Studio but result is the same.

Can anyone help me?

Thanks

like image 953
Dev Avatar asked Nov 21 '13 12:11

Dev


2 Answers

Problems

  1. In case you have table name tblEmployee in Database But after the adding 'Entity Data Model' you have Changed Entity tblEmployee to Employee in EDMX file then Save and Build. But Classes for Changed Name are not Generated Automatically in Model1.tt file.
  2. When "Update Model From Database" for adding new table same Problem occurs.
  3. Also not available in MVC When creating view with Model Class/Create Controller With Actions using EntityFramework

This problem is for early version of VS2012.This problem is solved in upgraded version of VS2012.

Solution

we have solution for this with early version of VS2012 & EF 5.0

Follow steps

  1. Right click On Model1.tt and select 'Run Custom Tool' save and Build Now see classes are generated.
  2. Right click On Model1.Context.tt and select 'Run Custom Tool' save and Build Now see property IN Context class is generated like

    public DbSet<Employee> Employees { get; set; } 
    

Save and Build Solution

Model1Context context=new Model1Context();
List<Employee> empList= context.Employees.ToList();

This worked for me. But Keep in mind that still EF 6.0 not able to make Scaffolding when 'creating controller and view using entityframework' in MVC with this viersion of VS2012. You must Use EF 5.0 or update VS2012 with new update.

like image 197
Dnyneshwar Avatar answered Nov 12 '22 22:11

Dnyneshwar


Resolved it myself.

The problem was in files filename.Context.tt and filename.tt.

The diagram file name for variable const string inputFile specified in both of the files were different than the existing diagram file (.edmx file). Updated it with existing diagram file name and then updated model from database. Working fine now.

like image 26
Dev Avatar answered Nov 12 '22 21:11

Dev