3

I'm creating a new MVC 3 pilot application using Mvc3 and the MvcScaffolding NuGet, everything runs smoothly until i want to use the database i already have. The application keeps creating a database with the format:

projectname.Models.projectnameContext

I'm stuck in here, my connectionStrings is:

<connectionStrings>
   <add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient" /> 
   <add name="EnginesTrackingEntities" connectionString="metadata=res://*/Models.Model1.csdl|res://*/Models.Model1.ssdl|res://*/Models.Model1.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=WARCHLAPPY\SQLEXPRESS;initial catalog=[EnginesTracking];integrated security=True;multipleactiveresultsets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" /> 
</connectionStrings>

In which specifies that my database is EnginesTracking.

Update

I'm following the database first approach from this example.

I have everything working perfectly but when the application starts, it creates a new table instead of using the one i specified.

The only one difference is that there is no databaseEntities in my project, instead there is projectContext for which i cannot do the number 8 step

Update2

I'm kinda given up on this, going to follow codeFirst approach as this is taking to much time for only being a pilot.

This is the Model1.context.cs:

//------------------------------------------------------------------------------
// <auto-generated>
//    This code was generated from a template.
//
//    Manual changes to this file may cause unexpected behavior in your application.
//    Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace ErrorReportingSystem.Models{
  using System;
  using System.Data.Entity;
  using System.Data.Entity.Infrastructure;

  public partial class EnginesTrackingEntities : DbContext
  {
    public EnginesTrackingEntities()
        : base("name=EnginesTrackingEntities")
    {
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        throw new UnintentionalCodeFirstException();
    }

    public DbSet<Engine_Part> Engine_Part { get; set; }
    public DbSet<Engines> Engines { get; set; }
    public DbSet<Error> Error { get; set; }
    public DbSet<Has_error> Has_error { get; set; }
    public DbSet<Locations> Locations { get; set; }
    public DbSet<Operators> Operators { get; set; }
    public DbSet<sysdiagrams> sysdiagrams { get; set; }
  }
}
KoU_warch
  • 2,160
  • 1
  • 25
  • 46
  • What connection string are you specifying in your DbContext? – Mark Oreta Sep 06 '12 at 00:13
  • @MarkOreta what do you mean? when i add code generation for the ado.net dbcontext there is no database specification field. – KoU_warch Sep 06 '12 at 13:34
  • Sorry, I thought you were using CodeFirst - Are you changing the name of the connection string when it's updated? Can you look at the generated Designer code, it will show what connection string it's attempting to use. It will look like this: public Entities1() : base("name=Entities1", "Entities1") – Mark Oreta Sep 06 '12 at 13:50
  • @MarkOreta sorry for being late, there is the update for you. – KoU_warch Sep 10 '12 at 15:24
  • @MarkOreta omg, thank you very much for comment. Playing arround in a break i discovered that all i needed to do was adding ` public ErrorReportingSystemContext() : base("name=EnginesTrackingEntities") { }` to the `ErrorReportingSystemContext.cs` and done! – KoU_warch Sep 10 '12 at 19:40

2 Answers2

1

Seems like you are using Code First Entity Framework. The post will probably get the best exposure in that forum. EnginesTrackingEntities is an EF connect string.

One of the big problems in dealing with code-first is the db drop and re-create with every change to your POCO's.

I strongly prefer database first approach because it is not as hard to deploy (not hard at all) and where you are pointing is clear. The advantage of code first is being able to elegantly style and decorate your POCO's.

I'm not exactly answering your question due to lack of info, but helping direct you. With a little more info on your deployment and Entities, I may be able to help more.

Dave Alperovich
  • 32,320
  • 8
  • 79
  • 101
  • 1
    It's trivial to disable the automatic dropping of the database. Just add this to the constructor. `System.Data.Entity.Database.SetInitializer(new CreateDatabaseIfNotExists());` – Erik Funkenbusch Sep 06 '12 at 00:46
  • agreed. Can even change the db initialization type to one that drops tables instead of databases. But honestly, I just prefer to work from database. The only challenge there is to maintain your object customizations (e.g. attribute) but there are workarounds like buddy classes. re-cycling the db with code-first presents serious maintenance challenges. – Dave Alperovich Sep 06 '12 at 05:23
  • EF has had a migration system in place for over a year now for working with code first without data loss. – Erik Funkenbusch Sep 06 '12 at 06:16
  • @EH, I'm stumped. I went thru the steps and in fact you are using db first. Now I'm double stumped b/c db first does NOT generate a db. In fact syncing with an empty db should remove all your POCO's. I would like to know why EF is using sqlexpress on WARCHLAPPY\SQLEXPRESS while ApplicationServices uses local. Where does sqlexpress reside? – Dave Alperovich Sep 06 '12 at 15:59
  • @Dave The application is a pilot, so it´s all running from localhost. – KoU_warch Sep 07 '12 at 20:17
  • @Dave yes, everything i run is in my local machine, including the SQLEXPRESS server – KoU_warch Sep 07 '12 at 20:49
1

I finally found the problem. I found a similar issue here which finished pointing me in the right direction.

I had to add:

    public ErrorReportingSystemContext() : base("name=EnginesTrackingEntities") { }

To the ErrorReportingSystemContext.cs file under /Models folder.

Done!

Community
  • 1
  • 1
KoU_warch
  • 2,160
  • 1
  • 25
  • 46