【问题标题】:code first and existing database mixture代码优先和现有数据库混合
【发布时间】:2014-02-19 00:55:24
【问题描述】:

我不知道是否可以关注,但我想听听其他意见。

我有一个现有的数据库。我将只使用一定数量的表和这些表的某些列。为此,我首先创建了类并在这些类和数据库表之间创建了特定的映射。然后实现了DbContext...到目前为止我没有任何问题。

在项目的第二部分,有些类可能不存在于数据库中。因此,当我运行项目时,如果它们不存在,我想实现它们,就像普通的 codefirst 实现一样,但是我不应该触及数据库中的其他表。

有什么想法吗?

最好的

【问题讨论】:

    标签: entity-framework ef-code-first ef-database-first


    【解决方案1】:
     In the second part of the project there are classes which might not exist in the database. So when I run the project if they don't exist I want to implement those, like a normal code first implementation however I shouldn't touch other tables in the database
    

    在您的 dbcontext 中使用忽略属性,覆盖模型构建器属性

    modelBuilder.Entity<YourClass>().Ignore(); 
    

    如果此类未映射到数据库中,这不会妨碍您的数据库。 希望这会有所帮助。

    【讨论】:

    • 感谢您的回复。然而,Ignore() 本身所做的还不够。我需要类似的东西: if (thisTableDoesNotExist) then createTheTable
    • "if (thisTableDoesNotExist) then createTheTable" 你可以通过迁移轻松实现。有关详细信息,请参阅此msdn.microsoft.com/en-us/data/jj591621.aspx 我希望这会有所帮助。 ;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-27
    • 1970-01-01
    相关资源
    最近更新 更多