7

If I create a SQL view and write a model to represent that view I will still want it to map up to be able to read data from the SQL view. The Add-Migration code will see this as a new table because entity framework (version 6) doesn't understand views.

Is there an attribute that I could assign that would prevent migration code from thinking its a new table it needs to create or something in the model builder?

tereško
  • 58,060
  • 25
  • 98
  • 150
Csharpfunbag
  • 395
  • 3
  • 19
  • possible duplicate of [Mapping Database Views to EF 5.0 Code First w/Migrations](http://stackoverflow.com/questions/20862807/mapping-database-views-to-ef-5-0-code-first-w-migrations) – Colin Feb 25 '14 at 11:34
  • I don't think this is a duplicate, I know that I can use the up/down code to control the migration but I would like future instances of migration code to not evaluate certain models (because the represent already created views) by means of an attribute like the suggestion from @Mashton below. – Csharpfunbag Feb 26 '14 at 14:21

3 Answers3

2

add migration nomaly. and after migration rewrite to:

public override void Up()
{
    Sql("CREATE VIEW [viewname] AS SELECT {any select expression} ");
}

public override void Down()
{
    Sql("DROP VIEW [viewname]");
}
1

From this SO answer:

Add-Migration IgnoreViewClasses –IgnoreChanges

(Works for EF 4.3.1+)

This creates an empty migration, ignoring any newly created classes from now on.

Community
  • 1
  • 1
Dunc
  • 18,404
  • 6
  • 86
  • 103
-1

You can use the [NotMapped] attribute to stop code-first classes being migrated into database creation.

Mashton
  • 6,037
  • 2
  • 25
  • 35
  • NotMapped would cause the property to not link to the column in the table it represents which is not the desired goal in this case. – Csharpfunbag Mar 09 '14 at 01:40