Mapping Database Views to EF 5.0 Code First w/Migrations

不问归期 提交于 2019-11-26 22:55:02

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());
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!