简体   繁体   English

EF和Web服务错误“ System.InvalidOperationException”

[英]EF and Webservice Error “System.InvalidOperationException”

I started a new project. 我开始了一个新项目。 Created a class library added EF item to it under a DB namespace and then create a class(Stripped down) for each entity that i can expose in WS. 在DB命名空间下创建了一个向其添加EF项的类库,然后为我可以在WS中公开的每个实体创建一个类(Stripped down)。 I ref the CL in a windows test app to see if everything was working and it was.So i created a WS add reference addedd the connectionstring for EF and then created a webmethod that retruns the object i created for each entity. 我在Windows测试应用程序中引用了CL来查看一切是否正常,因此我创建了一个WS add引用,为EF添加了连接字符串,然后创建了一个Web方法来重新运行为每个实体创建的对象。

so my namspaces looks like this 所以我的namspaces看起来像这样

[projectName].CL.Item - created object [projectName].CL.DB.Item - Ef Item [projectName].WS - Webservice namespace [projectName] .CL.Item - created object [projectName] .CL.DB.Item - Ef Item [projectName] .WS - Webservice名称空间

So i ran the ws and tested it. 所以我跑了ws并测试了它。 and i get this lovely little exception. 我得到了这个可爱的小例外。

System.InvalidOperationException: Unable to generate a temporary class (result=1).
error CS0012: The type 'System.Data.Objects.DataClasses.EntityObject' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Data.Entity, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.

   at System.Xml.Serialization.Compiler.Compile(Assembly parent, String ns, XmlSerializerCompilerParameters xmlParameters, Evidence evidence)
   at System.Xml.Serialization.TempAssembly.GenerateAssembly(XmlMapping[] xmlMappings, Type[] types, String defaultNamespace, Evidence evidence, XmlSerializerCompilerParameters parameters, Assembly assembly, Hashtable assemblies)
   at System.Xml.Serialization.TempAssembly..ctor(XmlMapping[] xmlMappings, Type[] types, String defaultNamespace, String location, Evidence evidence)
   at System.Xml.Serialization.XmlSerializer.FromMappings(XmlMapping[] mappings, Evidence evidence)
   at System.Web.Services.Protocols.XmlReturn.GetInitializers(LogicalMethodInfo[] methodInfos)
   at System.Web.Services.Protocols.XmlReturnWriter.GetInitializers(LogicalMethodInfo[] methodInfos)
   at System.Web.Services.Protocols.MimeFormatter.GetInitializers(Type type, LogicalMethodInfo[] methodInfos)
   at System.Web.Services.Protocols.HttpServerType..ctor(Type type)
   at System.Web.Services.Protocols.HttpServerProtocol.Initialize()
   at System.Web.Services.Protocols.ServerProtocol.SetContext(Type type, HttpContext context, HttpRequest request, HttpResponse response)
   at System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean& abortProc

Now this tells me it need reference to data.entity so i added still thinking to myself this is weird never had to do this and i am not return entity object i am returning the created onces but i did it. 现在,这告诉我它需要引用data.entity,因此我仍然对自己添加了一个思考,那就是从来不需要这样做,而且我没有返回实体对象,而是返回了创建过的一次对象,但是我做到了。 still the same error 仍然是同样的错误

then i saw that no matter what webmethod i select it does this i commented the webmethod out and made a helloworld and it worked. 然后我看到,无论我选择了什么网络方法,我都会对webmethod进行评论并制作一个helloworld并且它有效。

I looked on google some people suggestthat you add 我在谷歌看了一些人建议你添加

<add assembly="System.Data.Entity, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>

to the webconfig i did and still same error. 我做的webconfig仍然是同样的错误。 i am dumbfounded i do this sort of thing like daily and i never got a error like this. 我傻眼了,我做了像日常这样的事情,我从来没有像这样的错误。 And the EF structure is nothing special 5 tables with foreignkeys. 并且EF结构没有什么特别的5个表与外键。

i even deleted the WS/CL project and recreated it. 我甚至删除了WS / CL项目并重新创建它。

pls help 请帮助

Found the problem... 发现了问题...

I have in each POCO class this 我在每个POCO课上都有这个

namespace CL
    {
        public class Item
        {
            public static implicit operator Item(DB.Item db)
            {
                return new Item
                {
                    Created = db.Created,
                    Id = db.ItemId
                };
            }
        }
    }

that basically converts the DB item into a POCO item. 基本上将DB项目转换为POCO项目。

So if i do this instead 所以如果我这样做

namespace CL.DB
{
    public partial class Item
        {
            public static implicit operator CL.Item(Item db)
            {

                return new CL.Item
                {
                    Created = db.Created,
                    Id = db.ItemId
                };
            }
        }
}

it works fine. 它工作正常。 WTF WTF

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM