开发者

EF and Webservice Error "System.InvalidOperationException"

开发者 https://www.devze.com 2023-01-29 16:49 出处:网络
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 entitythat i can expose in WS. I ref the CL in a windows test

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. 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.

so my namspaces looks like this

[projectName].CL.Item - created object [projectName].CL.DB.Item - Ef Item [projectName].WS - Webservice namespace

So i ran the ws and tested it. 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. 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.

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. 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.

i even deleted the WS/CL project and recreated it.

pls help


Found the problem...

I have in each POCO class this

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.

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

0

精彩评论

暂无评论...
验证码 换一张
取 消