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?

2
  • possible duplicate of Mapping Database Views to EF 5.0 Code First w/Migrations Commented Feb 25, 2014 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. Commented Feb 26, 2014 at 14:21

3 Answers 3

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]");
}
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

-1

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

1 Comment

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.

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.