代码首先和现有数据库混合

时间:2014-02-19 00:55:24

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

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

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

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

有什么想法?

最佳

1 个答案:

答案 0 :(得分: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中使用Ignore属性,覆盖modelbuilder属性

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

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