开发者

Entity Framework 4, dynamic proxies with AsyncController in ASPNET MVC 3

I'm using the following method in my application which uses AsyncController, which retrieves (in the using a dynamic proxy if I don't use the foreach and use something like newMessageList = myMessages.ToList();.

I need to get in newMessageList a List not of dynamic proxy, because if not when the View tries to parse the result it throws an HTTP Error 500 (Internal Server Error). Is there any way I can avoid the use of the foreach to iterate over each property of myMessage and just avoid to creation of the Dynamic Proxies for it? (but just here, not for the entire application).

   private class MyAsyncResult : IAsyncResult
    {
        private static int mCount = 0;
        private readonly CheckForNewMessagesResponse mCheckForNewMessagesResponse;
        private readonly Thread mThread;
        private readonly AutoResetEvent mWait;

        public MyAsyncResult(CheckForNewMessagesResponse checkForNewMessagesResponse)
        {
            List<Message> newMessageList = null; 
            using (var ctx = new Server.Model.ServerPOCOs())
            {
                mCheckForNewmyMessagesResponse = checkForNewmyMessagesResponse;
                newmyMessageList = new List<myMessage>();

                var myMessages = from myMessage in ctx.myMessages
                                  where !myMessage.read
                                  orderby myMessage.ReceptionDate
                                  select myMessage;


                foreach (myMessage message in myMessages)
                {
                    myMessage um = new myMessage();
                    um.concept1 = message.concept1;
                    um.concept2 = message.concept2;

                    newmyMessageList.Add(um); 
                }
            }

            mWait = new AutoResetEve开发者_开发技巧nt(false);

            mThread = new Thread(new ThreadStart(() =>
            {
                Thread.Sleep(TimeSpan.FromMilliseconds(5000));

                checkForNewmyMessagesResponse(newmyMessageList);

                mWait.Set();
            }));

            mThread.Start();
        }

Thanks in advance. Guillermo.


This should work:

using (var ctx = new Server.Model.ServerPOCOs())
{
    ctx.ContextOptions.ProxyCreationEnabled = false;

    newMessages = (from myMessage in ctx.myMessages
                   where !myMessage.read
                   orderby myMessage.ReceptionDate
                   select myMessage).ToList();
}

Edit: Sample modified for ObjectContext API.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜