代码首先与数据库优先 [英] Code First vs. Database First

查看:533
本文介绍了代码首先与数据库优先的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建基于现有数据库的实体框架模型,然后生成从模型​​的POCO实体。在我的web.config的连接字符串不是实体框架,它只是标准的连接字符串(它缺少CSDL,SSDL,MSL参考)。



我可以编译我的应用程序,但是当我跑我得到这个错误:




代码中使用T4模板数据库优先和模型
首先产生如果代码优先模式下使用的开发可能无法正常工作。
。要继续使用数据库优先或模型优先保证实体
框架连接字符串在
执行的应用程序的配置文件中指定。要使用这些类,从
数据库优先或模型首先生成的,与代码第一次使用属性或DbModelBuilder API,然后
删除引发此异常<代码添加任何额外的
配置/ p>





我的问题是,凡在我的代码它实现了波苏斯从自动生成来和我怎样才能得到它表现得像代码优先?我不想引用CSDL等在我的连接字符串。


解决方案

如果在连接​​字符串的元数据,EF认为它是第一型或数据库第一。如果是一个普通的连接字符串,EF认为这是第一守则。但是,如果你要开始了第一次做模型,但使EF觉得你真的做代码首先(这是你在做什么),请确保您使用的DbContext代码生成器,而不是默认的。代码首先是波苏斯真的 - 普通的老C#对象 - 没有特殊的识别数据库或更改其跟踪的东西都没有。要使用的DbContext代码生成器中,右键点击你的模型图,然后选择添加新的代码生成项目...,然后选择ADO.NET的DbContext发生器。此外,这取决于你如何命名的主键和外键便和/或是否比只是简单的INT标识更加复杂,你可能需要填写一些代码到你的对象之间的关系,在OnModelCreating方法映射您上下文。删除行抛出新UnintendedCodeFirstException(); 并与您的映射代码替换它。否则EF未必能找出所有关系(记得有没有元数据,它依赖)。



希望这有助于。


I created an Entity Framework model based on an existing database, then generated the POCO entities from the model. The connection string in my web.config isn't Entity Framework, it's just the standard connection string (it's missing the CSDL, SSDL, MSL references).

I can compile my application, but when I run I get this error:

Code generated using the T4 templates for Database First and Model First development may not work correctly if used in Code First mode. To continue using Database First or Model First ensure that the Entity Framework connection string is specified in the config file of executing application. To use these classes, that were generated from Database First or Model First, with Code First add any additional configuration using attributes or the DbModelBuilder API and then remove the code that throws this exception


My question is, where in my code does it realize the POCOs came from auto generation, and how can I get it to behave like Code First? I don't want to reference the CSDL etc in my connection string.

解决方案

If the connection string has the metadata, EF thinks it is Model First or Database First. If it is a plain connection string, EF thinks it is Code First. However, if you want to start out doing model first but make EF think you are really doing code first (which is what you are doing), make sure you are using the DbContext code generator, not the default one. Code first POCOs are really that--"plain old c# objects"-- no special database aware or change tracking stuff in them at all. To use the DbContext code generator, right click on your model diagram and choose "Add new code generation item..." then select the ADO.NET DbContext Generator. Also, depending on how you named your primary and foreign keys and/or whether they are more complicated than just simple int IDs, you will probably need to fill in some code to map the relationships between your objects in the "OnModelCreating" method in your context. Delete the line throw new UnintendedCodeFirstException(); and replace it with your mapping code. Otherwise EF may not be able to figure out all the relationships (remember there's no metadata for it to rely on).

Hope this helps.

这篇关于代码首先与数据库优先的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆