简体   繁体   中英

Ignore create/change of model for a SQL View using Entity Framework

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?

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]");
}

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.

您可以使用[NotMapped]属性来停止将代码优先的类迁移到数据库创建中。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM