Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code generated using the T4 templates for Database First and Model First development may not work correctly if used in Code First mode [duplicate]

I am using Entity frame work in my asp.net MVC 4 application. I have an edmx file. I am trying to use the entity from this EF model to populate a viewmodel like this:

using (var context = new VehiclesContext())
            {               

                IEnumerable<SearchedVehicles> vehicles = context.Vehicles.Select(x => new SearchedVehicles
                {

                      Year = x.Year,
                      Make = x.Make,
                      Model = x.Model,
                      Mileage = x.Mileage,
                      VIN = x.VIN
                });

                return View(vehicles);
            }      

Vehicles is entity in edmx where SearchedVehicles is viewmodel but I get this exception:

Code generated using the T4 templates for Database First and Model First development may not work correctly if used in Code First mode. To continue using Database First or Model First ensure that the Entity Framework connection string is specified in the config file of executing application. To use these classes, that were generated from Database First or Model First, with Code First add any additional configuration using attributes or the DbModelBuilder API and then remove the code that throws this exception.

on this line:

 protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }
like image 861
DotnetSparrow Avatar asked Jan 12 '23 15:01

DotnetSparrow


2 Answers

This is per design. You have created a model from database, which indicates that your code should never create a database.

The error message you get, occurs because it doesn't find the database and hence tries to create it.

To solve this you need to check your connectionstring to make sure it can find the database you want to connect to. If you really want to create the database you should go for code first. This means either to remove the "throw new exception" line and keep using the model (this means you can't update model from database anymore, as it then would reinsert the exception line), or you can reverse engineer a database to code-first model.

see http://msdn.microsoft.com/en-us/data/jj593170.aspx for reverse engineering tips.

like image 171
Zaphod Avatar answered Feb 01 '23 14:02

Zaphod


I had same error 'Code generated using the T4 templates...'. My issue was different than above. My database table schema was changed. Hence after refresh, I was editing the edmx by opening it as xml (rt click and say Open With... and select xml). After fixing all errors in xml, when I run the application, I get the above error. Hence next time I undo everything in TFS and continued by removing and adding problematic tables again from Edmx diagram page. Doing this way, helped resolve above error. Also learned, never to edit edmx file in xml mode.

Happy Coding.

like image 41
user2308925 Avatar answered Feb 01 '23 12:02

user2308925