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

asp.net-mvc asp.net-mvc-4 c# entity-framework

Question

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();
        }
1
4
12/20/2013 11:54:01 AM

Accepted Answer

If the connection string has the metadata, EF thinks it is Model First or Database First. If it is a plain connection string, EF thinks it is Code First. However, if you want to start out doing model first but make EF think you are really doing code first (which is what you are doing), make sure you are using the DbContext code generator, not the default one. Code first POCOs are really that--"plain old c# objects"-- no special database aware or change tracking stuff in them at all. To use the DbContext code generator, right click on your model diagram and choose "Add new code generation item..." then select the ADO.NET DbContext Generator. Also, depending on how you named your primary and foreign keys and/or whether they are more complicated than just simple int IDs, you will probably need to fill in some code to map the relationships between your objects in the "OnModelCreating" method in your context. Delete the line throw new UnintendedCodeFirstException(); and replace it with your mapping code. Otherwise EF may not be able to figure out all the relationships (remember there's no metadata for it to rely on).

Hope this helps.

23
12/23/2011 5:07:52 PM

Popular Answer

You need the following in your config file:

<connectionStrings>
<add name="<The name of your class>" 
     connectionString="metadata=res://*/<test>.csdl|res://*/<test>.ssdl|res://*/<test>.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=<your source>;initial catalog=<your db>;persist security info=True;user id=<your user id>;password=<your password>;multipleactiveresultsets=True;App=EntityFramework&quot;" 
     providerName="System.Data.EntityClient" />
</connectionStrings>


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow