如何在实体框架中的自动代码第一迁移中排除一个表?
我正在使用启用自动迁移的代码中的实体框架。现在,我有一个实体,其表应该由EF管理(迁移)。有没有一种方法可以禁用一个特定实体(即表)自动迁移?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我正在使用启用自动迁移的代码中的实体框架。现在,我有一个实体,其表应该由EF管理(迁移)。有没有一种方法可以禁用一个特定实体(即表)自动迁移?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(7)
现在可以在EF Core 5.0中使用
dubl frommigrations()
方法,但奇怪的是,您必须调用totable()
方法,然后使用tablebuilder << /代码>。
This is now possible in EF Core 5.0 using the
ExcludeFromMigrations()
method, but strangely enough you have to call theToTable()
method and then use theTableBuilder
.https://devblogs.microsoft.com/dotnet/announcing-entity-framework-core-efcore-5-0-rc1/#exclude-tables-from-migrations
在EFCORE 5.0中对我有用的另一个选择是使用 setistableExcludedFrommigrations :
Another option that worked for me in EFCore 5.0 is to use SetIsTableExcludedFromMigrations:
我的临时解决方案,仅适用于开发环境。
我有一个单独的脚本运行迁移,并且程序运行不会检查它们。因此,在意外的情况下,我可以调用
忽略&lt; contactView&gt;()
并使用此行运行迁移。完成后,我删除了这条线!My TEMPORARY solution, only for dev environments.
I have a separate script that runs migration and program run does not check them. So in unexpected case I was possible to invoke
Ignore<ContactView>()
and run migrations with this line. When it was completed, I removed this line!通过使用另一个
dbContext
访问所讨论的表。迁移与一个dbContext
(请参阅可以自动迁移一个dbcontext,而不是同一项目中的另一个迁移?)。It is possible by using another
DbContext
to access the table in question. Migrations are bound to oneDbContext
(see Is it possible to have automatic migrations for one DbContext and not for another in the same project?).不确定这是否是OP的确切情况,但是我有一个我不希望生成迁移的表。我通过使用
toview
而不是totable
在dbcontext中:对我来说有点骇人听闻,但也许不是 - 因为毕竟,我只是我只是只是,我只是只是尝试“查看”表,不要写入...
[使用.NET CORE EF 3.1.3]
***更新06/17/23:如果您与EF3一起工作,上述解决方案仍然可行但是,如果您使用的是EF5+,则应使用新的
uble dubludeFrommigrations()
函数: https://devblogs.microsoft.com/dotnet/announcing-ennouncing-ennouncing-entity-frame-frame-core-core-core-5-5-0-rc1/ #排除列表Not sure if this is the OP's exact scenario, but I had a table that I did not want a migration generated for. I accomplished this by using
ToView
instead ofToTable
within the DbContext:It feels a bit hacky to me, but maybe it's not -- because, after all, I'm just trying to "view" the table, not write to it...
[Tested with .NET Core EF 3.1.3]
*** update 06/17/23: the above solution is still viable if you are stuck working with EF3, but if you are using EF5+ you should use the new
ExcludeFromMigrations()
function: https://devblogs.microsoft.com/dotnet/announcing-entity-framework-core-efcore-5-0-rc1/#exclude-tables-from-migrations您需要在该类/实体上使用
[未图表]
注释。You want to use the
[NotMapped]
annotation on that class/entity.问题提到(一个表),但我在视图模型上遇到了这个问题。当您在SQL数据库中正确使用SQL视图及其项目中的模型时,也许您不需要在此视图的项目COZ中添加迁移。解决方案是忽略数据ApaSecontext中的写入dbset,而仅通过fluentapi config或componentmodel.dataannotations进行配置
The question mentions (one table) but I had this problem with the view model. when you properly an SQL view in an SQL database and a model of it in your project, So maybe you do not need to add migration in your project coz of this view. The solution is to ignore write DbSet in your DataBaseContext and only configure it by fluentApi config or ComponentModel.DataAnnotations