I\'m trying to map a SQL View to an entity in EF 5.0 Code First w/Migrations for displaying some basic information on a page without having to query multiple tables for that
You have specified that the ClientStatisticsView
entity should be mapped to a table named "ClientStatistics". So entity framework will generate a migration containing an instruction to create that table. But you have independently created that view in the database so to prevent the error you are getting you should remove the CreateTable
instruction from the Up
migration.
I think a better way to do it is to create the view in the migration by running sql like this:
public override void Up()
{
Sql("EXEC ('CREATE View [dbo].[ClientStatistics] AS --etc"
}
public override void Down()
{
Sql(@"IF EXISTS (SELECT
*
FROM sys.views
WHERE object_id = OBJECT_ID(N'dbo.ClientStatistics'))
DROP VIEW dbo.ClientStatistics)")
}
That way your views and tables are specified in one place and you can safely migrate up and down
Reference
http://elegantcode.com/2012/04/12/entity-framework-migrations-tips/
I'm actually working with Entity Framework "Code First" and views, the way I do it is like this:
1) Create a class
[Table("view_name_on_database")]
public class ViewClassName {
// View columns mapping
public int Id {get; set;}
public string Name {get; set;}
// And a few more...
}
2) Add the class to the context
public class ContextName : DbContext {
// Tables
public DbSet<SomeTableClassHere> ATable { get; set; }
// And other tables...
// Views
public DbSet<ViewClassName> ViewContextName { get; set; }
// This lines help me during "update-database" command
protected override void OnModelCreating(DbModelBuilder modelBuilder) {
// Remove comments before "update-database" command and
// comment this line again after "update-database", otherwise
// you will not be able to query the view from the context.
// Ignore the creation of a table named "view_name_on_database"
modelBuilder.Ignore<ViewClassName>();
}
}
A little bit late but I hope this helps somebody.
If there was a way to ignore the creation of a table named as the view during "update-database" and not-ignore after this that would be great.
Add the ToTable attribute to your entity and include it as a DbSet in your context.
[ToTable("ClientStatics")]
public class ClientStaticsView{}
public class DataContext : DbContext
{
public DbSet<ClientStaticsView> ClientStatics { get; set; }
}
Or if you don't want to add the DbSet to your context, create an EntityTypeConfiguration for your ClientStatisView and include the ToTable attribute with the fluent api instead of an attribute. You can then add the entity to your context in your context's OnModelCreating:
public class ClientStaticsViewConfiguration : EntityTypeConfiguration<ClientStaticsView>
{
public ClientStatusViewConfiguration
{
ToTable("ClientStatics");
}
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new ClientStatisViewConfiguration());
}