4

The situation as follows:

we have one big MVC project with database first approach on EF5.0:

ObjectContext constructor:

namespace xxx.Models
{
    (...)
        public partial class xxxEntities : ObjectContext
        {
            #region Constructors

            /// <summary>
            /// (...)
            /// </summary>
            public xxxEntities() : base("name=xxxEntities", "xxxEntities")
            {
                this.ContextOptions.LazyLoadingEnabled = true;
                OnContextCreated();
            }

    (...)

connection string:

    <add name="xxxEntities" 
         connectionString="metadata=res://*/Models.xxxModel.csdl|
         res://*/Models.xxxModel.ssdl|res://*/Models.xxxModel.msl;
         provider=System.Data.SqlClient;provider connection string=&quot;
         data source=.;Initial catalog=xxxdb;
         integrated security=True;
         multipleactiveresultsets=True;
         App=EntityFramework&quot;"
         providerName="System.Data.EntityClient" />

We choose Code First for testing the new development on separated namespace.

connection string:

<add name="xxxCFContext" 
     connectionString="Data Source=.;
     Initial Catalog=xxxdb;
     Integrated Security=True;
     User Instance=False;
     MultipleActiveResultSets=True" 
     providerName="System.Data.SqlClient" />

DbContext constructor:

namespace xxx.Models.CodeFirst
{
    public partial class xxxCFContext : DbContext
    {
        static xxxCFContext()
        {
            Database.SetInitializer<xxxCFContext>(new ValidateDatabase<xxxCFContext>());
        }

        public xxxCFContext()
            : base("Name=xxxCFContext")
        {
        }

(...)

We run add-migrations, update-database without problem, the build completed with success. But on the first time with the code-first db access:

xxxCFContext cfdb = new xxxCFContext();

foreach (Xobject xobject in cfdb.Xobjects)

Appear the error:

"Could not find the conceptual model type 'xxx.models.yyyclass'", but this yyyclass exist in edmx, NOT the codefirst part.

Uninstall EF5.0, install EF6.0 and the error disappeared. But i need the EF5.0 not the 6.0 alfa3 prerelease.

What's wrong? How can we use edmx mixed with codefirst with EF5.0?

I would be very grateful for any idea.

EDIT

I know this workarounds, but not help for me:

Have anyone used Entity Framework with code first approach mixed with Edmx file?

http://blog.oneunicorn.com/2012/02/26/dont-use-code-first-by-mistake/

Could not find the conceptual model type

http://blog.oneunicorn.com/2012/02/26/dont-use-code-first-by-mistake/

Entity Framework (4.3) looking for singular name instead of plural (when entity name ends with "s")

Building an MVC 3 App with Database First and Entity Framework 4.1

The problem of generating (with xxxmodel.Context.tt and xxxModel.tt) the large existing edmx heavily exploits the advantages of ObjectContext, so we can not simply change from the ObjectContext to the DbContext (behind the existing edmx).

Edit II

From DB First to CodeFirst, we choose the following:

(Right-click into EDMX editor and add code generation item. If you do not seem, to be installed: Tools menu, Extensions and Updates, EF 5.x DbContext Fluent Generator) enter image description here

  • The model objects and the context is copied from the EDMX.
  • The EDMX was deleted anything that was under him.
  • Set the ConnectionString from was difficult EDMX-style to simple codefirst form.
  • and tadam, in about 10 minutes, we moved from Database First to Code First. During development, with existing 80 tables in db.

  • run Enable-Migrations in Power Management Console, and continued with CodeFirst.

Community
  • 1
  • 1
Gábor Plesz
  • 1,203
  • 1
  • 17
  • 28

1 Answers1

2

It may help to keep the different "types" of EF (code-first, database first, etc.) in separate assemblies: a limitation of EF is that it is not possible use code-first in an assembly with certain database-first attributes - though a newer version of EF may fix this (perhaps that's why EF6 alphas work for you).

Eamon Nerbonne
  • 47,023
  • 20
  • 101
  • 166
  • Thank you @Eamon! I know the solution, but I would like to avoid if possible. – Gábor Plesz Mar 07 '13 at 10:09
  • Unfortunately, it's not possible to avoid this in EF5. However, if it works in EF6, then perhaps that's a workaround you can live with - I guess it depends on how soon your app needs to be stable and whether you're OK taking a little risk since EF6 is obviously not finished yet. – Eamon Nerbonne Mar 07 '13 at 15:19
  • Thank you for your answers, I accept, because I could not find any other solution. We start the codefirst development in a separate assembly, and if the conversion is done, we will combine the projects. – Gábor Plesz Mar 08 '13 at 11:08
  • Unfortunately, I had to do exactly the same thing last year. Should you happen to stumble across some alternative - please give a shout! – Eamon Nerbonne Mar 08 '13 at 11:10
  • Yeah, I did something vaguely similar, though I don't think that the fluent generator was around back then - it was some power tool extra pack that did a reasonable job. Is the fluent generator any good? – Eamon Nerbonne Apr 19 '13 at 14:05
  • Everyone's surprise, the fluent generator alone made ​​all the work. (Available from 13/12/2012) – Gábor Plesz Apr 19 '13 at 14:18