2014-02-19 25 views
0

我不知道以下是否可能,但我想听听其他意见。代码优先和现有的数据库混合

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

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

有什么想法?

最佳

回答

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(); 

如果该类未在数据库映射这不会妨碍你的数据库。 希望这有助于。

+0

谢谢你的回应。然而,Ignore()本身并不足够。我需要这样的东西:if(thisTableDoesNotExist)然后createTheTable – rkmorgan

+0

“如果(thisTableDoesNotExist)然后createTheTable”你可以很容易地实现是通过迁移。详细信息请参阅http://msdn.microsoft.com/en-us/data/jj591621.aspx我希望这可以帮助。 ;) – MKMohanty

相关问题