移动实体框架模型转换成类库的web项目 [英] Moving Entity Framework model into class library from web project

查看:95
本文介绍了移动实体框架模型转换成类库的web项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用实体框架和近期又实现具有相同的解决方案中的另一个项目中的EF模型,这样我可以从它建立多个用户界面的好处。

I am using Entity Framework and recently came to realize the benefits of having your EF model in another project within the same solution so that I can build multiple UIs from it.

我把它转移到一个新的类库项目和web项目使用由项目产生的新的DLL更新到实体的所有引用。一切都已经顺利,除了一个小的障碍。当我搬到EF到新的项目,在某种程度上它仍然在Web项目中的web.config中读取它的连接字符串(不要问我怎么样,因为我不知道)。

I moved it over to a new class library project and updated all the references to the entities in the web project to use the new dll generated by the project. Everything has gone smoothly, except for one small snag. When I moved EF over to the new project, somehow it was still reading its connection string from the web.config in the web project (don't ask me how because I have no clue).

我用从数据库更新模型中的EF设计,并没有找到一个连接字符串(因为我只是把它移到新的项目后,预期),所以我用向导生成一个新的连接字符串,它做了就好了。新的连接字符串现在位于App.config的类库项目中。在属性窗口中的连接字符串现在是正确的,设计师从App.Config中阅读。我说干就干,删除了Web项目web.config中的连接字符串

I used "Update Model from Database" in the EF designer and it did not find a connection string (as I expected after moving it over to the new project) so I used the wizard to generate a new connection string, which it did just fine. The new connection string now resides in App.config within the class library project. The connection string in the properties window is correct now, and the designer is reading it from the App.Config. I went ahead and deleted the connection string from Web.Config in the web project.

现在运行应用程序我碰到下面的错误时:

Now when running the application I get the following error:

指定的命名连接或者未在配置中发现,不打算与EntityClient提供者,或不是有效的使用。

The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid.

如果我粘贴连接字符串回的Web.Config这一切工作就好了。我不想从头开始创建一个新的EF模型,因为它是一个相当复杂的模型,我做了很多的调整从数据库中拉后。我已经倒在生成的CS文件以及​​在EDMX文件中的XML,并不能找到什么有用的东西。任何帮助深表感谢。显然,对于现在,直到我想出解决办法,我只是离开,因为web.config中的连接字符串,无论出于何种原因,这似乎工作。

If I paste the connection string back into the Web.Config it all works just fine. I do not want to create a new EF model from scratch because it is a fairly complicated model and I did a lot of restructuring after pulling in from the DB. I have poured over the generated CS file as well as the XML in the edmx file and cannot find anything useful. Any help is much appreciated. Obviously for now, until I figure this out, I'm just leaving the connection string in web.config since, for whatever reason, that seems to work.

推荐答案

这是由设计;而在类库中的配置文件是设计师会用什么,实际的应用程序的配置文件的是什么将获得在运行时使用。不管是对Web.config文件的ASP.NET项目或App.config中的一个WinForms或WPF项目,它的应用程序配置文件(或更高的东西了,像Machine.config中),将被使用;类库中的文件不是应用程序的一部分。

This is by design; while the config file in the class library is what the designer will use, the configuration file of the actual application is what will get used at runtime. Whether that's Web.config for an ASP.NET project or App.config for a Winforms or WPF project, it's the application configuration file (or something higher up, like Machine.config) that will be used; the file in the class library is not part of the application.

如果你想提供一个EF模式,将无需指定在连接字符串工作应用程序或Web配置文件,那么你就必须要存储连接字符串一些其他的方式(你总是可以硬编码),并把它传递到您的上下文的构造适当的过载。

If you're trying to provide an EF model that will work without having to specify the connection string in the application or web configuration file, then you'll have to store the connection string some other way (you could always hard-code it) and pass it into the appropriate overload of your context's constructor.

我的解决办法通常是提供调用此重载使用适当的连接字符串的上下文本身的静态参数的功能。

My solution is generally to provide a static parameterless function on the context itself that calls this overload with the appropriate connection string.

这篇关于移动实体框架模型转换成类库的web项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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