1

I have a few tables previously created in my database that I want to map to a few model classes:

"SISTEMA.Voyages" => public class Voyage
"SISTEMA.Puerto"  => public class Port

I understand that in ASP.MVC 4 with Entity framework this can be done either of two ways. However for both of them I am getting errors which I do not know how to resolve.

The first in Voyage.cs:

[Table("SISTEMA.Voyages")]
public class Voyage

Or the second in Context.cs:

public class ApplicationEntities : DbContext
{
  protected override void OnModelCreating(DbModelBuilder modelBuilder)
  {
    modelBuilder.Entity<Voyage>().ToTable("SISTEMA.Voyages");
  }
}

For the first version I get this error when I previously assumed this was something automatic:

The type or namespace name 'Table' could not be found (are you using directive or an assembly reference?)
The type or namespace name 'TableAttribute' could not be found (are you using directive or an assembly reference?)

Fore the second I get this error which I didn't expect because I assumed this was a configuration issue and leaves me really confused:

The model backing the 'ApplicationEntities' context has changed since the database was created. Consider using Code First Migrations to update the Database.

Where is this history even recorded?

For the record, I am used to dealing with this sort of issue in Rails by typing in:

class Voyage
    self.table_name = "SISTEMA.Voyages"
end

And I am not very familiar with C# / ASP.NET. Just publishing what I am looking up for the next hour unless somebody tells me where I am going wrong first.

ovatsug25
  • 7,786
  • 7
  • 34
  • 48
  • 1
    For the first version have you added the using directive ? (using System.ComponentModel.DataAnnotations) – Jack Jun 24 '13 at 18:17
  • I just did. Thanks. And I'm now getting the error that happens with the second case. :( Looking into Migrations right now, but I'm still confused... – ovatsug25 Jun 24 '13 at 18:30
  • Closer to solution after reading: http://stackoverflow.com/questions/3600175/the-model-backing-the-database-context-has-changed-since-the-database-was-crea Basically I added `using System.Data.Entity`; in `Global.asax` and also added `Database.SetInitilaizer(null);` – ovatsug25 Jun 24 '13 at 18:40
  • Now I just need the right syntax because it is being interpreted as `dbo.SISTEMA.Voyages` instead of `SISTEMA.Voyages` – ovatsug25 Jun 24 '13 at 18:46
  • Have you seen http://stackoverflow.com/questions/10473184/invalid-object-name-dbo-tablename-when-retreiving-data-from-generated-table – Jack Jun 24 '13 at 19:11
  • Perfect. Thanks, I actually got it through the IntelliSense (which I'm growing to like for these types of things) Thanks! If you want some extra points, do an answer for the question and I can fill it in. Your help definitely got me there. Otherwise I'll add one in a day or two so the question has an answer. – ovatsug25 Jun 24 '13 at 19:16

2 Answers2

1

For the first way, you are missing a using directive for the Table attribute, add this to your class:

using System.ComponentModel.DataAnnotations;

For the second way, I'm guessing that something has changed in your model and now it recommends using Code First to update the database. When you run your application, and it first hits the database, it verifies that your models match the schema - if not it can give you this error.

I recommend checking out some of these links for help getting started with EF / Code First / Migrations - they are really handy

EF + Code First
Handling Many to Many with Payload
How to work with Migrations
MSDN EF Site

Community
  • 1
  • 1
Jack
  • 9,156
  • 4
  • 50
  • 75
0

Please Add:

using System.ComponentModel.DataAnnotations.Schema;

Raj
  • 800
  • 7
  • 8