1

I have an azure web application, running MVC 4. It uses the entity framework (version 4.3.1.0) and Code First together with a data context.

I have the data context in its own project, that also have all the model files.

public class AwesomeModelContext : DbContext
{

    public DbSet<User> Users { get; set; }
    public DbSet<License> Licenses { get; set; }
    public DbSet<AppSession> AppSessions { get; set; }
    public DbSet<EditSession> EditSessions { get; set; }
    public DbSet<Space> Spaces { get; set; }
    public DbSet<SpaceUserPrivilege> SpaceUserPrivileges { get; set; }
    public DbSet<File> Files { get; set; }
    public DbSet<Resource> Resources { get; set; }
    public DbSet<Team> Teams { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {

        // Remove cascading deletes, having them turned on by default scares me.
        modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();

        modelBuilder.Entity<Space>()
            .HasMany<SpaceUserPrivilege>(s => s.SpaceUserPrivileges)
            .WithRequired(p => p.Space)
            .WillCascadeOnDelete(true);
        modelBuilder.Entity<User>()
            .HasMany<SpaceUserPrivilege>(u => u.SpaceUserPrivileges)
            .WithRequired(p => p.User)
            .WillCascadeOnDelete(true);
        modelBuilder.Entity<Team>()
            .HasMany<User>(u => u.Users)
            .WithRequired()
            .WillCascadeOnDelete(false);
        // Run migrations, if any.
        Database.SetInitializer<AwesomeModelContext >(new MigrateDatabaseToLatestVersion<AwesomeModelContext , Configuration>());
    }

}

The relevant model is "Team" and it looks like this:

namespace MyAwesomeNamespace.Model
{
    public class Team
    {

        [Key]
        public int Id { get; set; }
        public string Name { get; set; }

        public ICollection<User> Users { get; private set; }

    }
}

That should all nice and well. These are the models for my entire web app. So now I want to add controllers so that I actually can do something with the models. So I do what I usally do,

Right-click the controller folder, and press "add controller"

I name the controller "TeamController", then select the model to be "MyAwesomeNamespace.Model.Team" and the data context to "MyAwesomeNamespace.Model.AwesomeModelContext".

I now press Add. This results in the following message.

"Unable to retrieve metadata for 'MyAwesomeNamespace.Model.Team'. Invalid column name 'CreatedOn'."

Anyone know what to do here? I can not find any solutions to this.


Some extra info:

Using Azure database (online). Using Visual Studio 2010 Pro.

1
  • I have also tried to use entity framework 5.0.0-rc. This gives me a different error: "Unable to retrieve metadata for '...Model.Team'. 'FK_dbo.TeamUsers_dbo.Teams_Team_Id' is not a contraint. Could not drop constraint. See previous errors." Commented Jul 17, 2012 at 13:24

2 Answers 2

1

I would like to know whether you have a CreatedOn column in the Team table in your database. If you already have a database, please make sure your model class corresponds to the database table. For example, add the CreatedOn property to your model. If you want a code first approach, you can remove the database. Let Entity Framework automatically generate the database. You can also take the database first approach. Let the Entity Framework automatically generated the code from the database.

Best Regards,

Ming Xu.

Sign up to request clarification or add additional context in comments.

1 Comment

I did two things to try to fix this. I reinstalled the entityframework in all projects, and I deleted and rebuildt the database. This worked :)
0

Seems like a thing in EF Code first with migrations enabled, when you upgrade from EF4.* to EF 5.0. And that in combination with MiniProfiler. The table existed in dbo._MigrationHistory under system tables.

You try do a few things:

  1. You can add CreatedOn (DateTime) column manually to dbo._MigrationHistory table under System tables folder.
  2. You can stop detecting changes by setting Configuration.AutoDetectChangesEnabled = false;
  3. Comment this line MiniProfilerEF.Initialize(), disabling EF profiling.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.