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.
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.