4

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();
        }
DotnetSparrow
  • 27,428
  • 62
  • 183
  • 316

2 Answers2

9

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.

Zaphod
  • 1,412
  • 2
  • 13
  • 27
  • I dont see any connection string in app.config file ( edmx is in a class library project) should i add connection string in app.config or in web.config which uses this edmx? Also, when I created edmx, why didnt it create the connection string in app.config – DotnetSparrow Dec 20 '13 at 12:24
  • web.config is the one used by your web app. But it's strange that it wasn't generated a connection string for you... – Zaphod Dec 23 '13 at 08:40
  • If you are using model-first (model generated from a database) and are specifying an explicit connection string via an additional constructor, ensure you're using the correct connection string style, i.e. via [here](http://stackoverflow.com/a/5942978/845584). – PeterX Nov 24 '14 at 06:21
1

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.

  • This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](http://stackoverflow.com/questions/ask). You can also [add a bounty](http://stackoverflow.com/help/privileges/set-bounties) to draw more attention to this question once you have enough [reputation](http://stackoverflow.com/help/whats-reputation). – ForceMagic Aug 29 '15 at 19:24
  • @ForceMagic, 99% of people coming here will have simply Googled that error message. They may not even read the question. Many of these people will be having the same issue as user2308925. – user1172763 Aug 11 '16 at 16:58
  • @user1172763 http://stackoverflow.com/help/how-to-answer – ForceMagic Aug 11 '16 at 17:02
  • @ForceMagic Yeah, this site has a lot of rules. It's one of the worst things about it. – user1172763 Aug 11 '16 at 18:06
  • @user1172763 That's the only way this site can be managable, the usual reflex of new people here is to use it like a forum, which isn't how it works. But once you get use to it, you'll notice it's actually faster and easier to find answers because they lies where they should be :) – ForceMagic Aug 11 '16 at 18:43