SATıŞ: 1-800-867-1389

 none
Problems with Entity Framework when Session state provider is used as Azure Appfabric cache service

    Soru

  • Hi Team,

    We are trying to migrate our on-premise SAAS applicaiton to windows azure, and we have used lot of Entity Framework for a module specifically. As a migration part we are trying to migrate and test the application in phases.  We are using Azure appfabric cache service as a session state provider. 

    When I try to configure the session state to appfabric cache we are running in to these issues related to Entity framework.  First we got this error to fix this issue we have the class as [DataContract] and the issue is resolved.

    System.Runtime.Serialization.InvalidDataContractException: Type 'XX.XX.XXX' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute.  If the type is a collection, consider marking it with the CollectionDataContractAttribute.  See the Microsoft .NET Framework documentation for other supported types.

    The property 'AdXXXXXX' on type 'CaXXXXX_9FDDBE5B2C99F9C5DE2E0DCD2E8CC77D2CB59220D1A3413E91AF5A2B6A23026D' cannot be set because the collection is already set to an EntityCollection.

    We are completely lost of how to fix these issues, and when googled a couple of threads tells we need to remove the virtual access modifier for the properties.  But the same code works with out any issues like this on-premise, and the code is generated automatically by the EF on each update of the model.  We are also have other option of going to SQLAzure as session state provider in the second chance, but we are hoping  there are also the same problems may exists as the data need to be serialized  when stored in the session.

    http://social.msdn.microsoft.com/forums/en/adonetefx/thread/99d8d4a1-5ab1-42dc-b9db-5087be02162d
    http://ianfnelson.com/archives/2011/03/09/entityframeworkadventure3/

    The following link shows closely describes the same problem we are facing, and we are not using lazy loading as described in this post, but did not find the solution. see the last comments made by Rui in the below link

    http://stackoverflow.com/questions/8980542/ef-dynamic-proxies-in-session-azure-cache

    Thanks in advance.


    • Düzenleyen chennas 12 Nisan 2012 Perşembe 15:34 To update the post
    12 Nisan 2012 Perşembe 15:33

Tüm Yanıtlar

  • Hi,

    Where do you get that error message? Is it increate new instance of entity class? And do you ever use virtual key word when you create entity classes? If so, try to remove them and retry.

    Also, you've mentioned that the application enable Azure Caching service already, if you access the data in cache, why does the application call the data from Entity Framework. As far as I know, Azure Caching service is used to cache data and does not need to access the data source again.

    Hope this helps.


    Please mark the replies as answers if they help or unmark if not. If you have any feedback about my replies, please contact msdnmg@microsoft.com Microsoft One Code Framework

    13 Nisan 2012 Cuma 08:18
  • Hi

    Please find the responses below for your points

    1.We are not creating the entity class manually, we are updating the model from the database and using it, all the entity related classes are genereated automatically by the EF.

    2.  We enabled the caching service, we when are trying to store the data [entity related objects] in the Session , we are getting the error.

    The property 'AdXXXXXX' on type 'CaXXXXX_9FDDBE5B2C99F9C5DE2E0DCD2E8CC77D2CB59220D1A3413E91AF5A2B6A23026D' cannot be set because the collection is already set to an EntityCollection.

    Hope I asked the questions correctly with out any confuison.

    13 Nisan 2012 Cuma 09:57
  • Hi,

    Do you mean the "Session[“YourSession”]=EntityObject;" caused the exception? If so what happens if he uses in-memory session instead of Caching Service on the cloud?

    I hope you can provide some code snippets and stack message about the exception, if so, i think it's can help us understand your problem.


    Please mark the replies as answers if they help or unmark if not. If you have any feedback about my replies, please contact msdnmg@microsoft.com Microsoft One Code Framework

    16 Nisan 2012 Pazartesi 08:22
  •                  

    Yes you are correct.

    We are getting the below mentioned error, when we tried as you mentioned above.

    The property 'AdXXXXXX' on type 'CaXXXXX_9FDDBE5B2C99F9C5DE2E0DCD2E8CC77D2CB59220D1A3413E91AF5A2B6A23026D' cannot be set because the collection is already set to an EntityCollection.

    Basically we have our own custom object, where in we are it holds some entity objects.

    Here is the update from our side.  Initially we got the above mentioned error.  Here is the process we followed to resolve this error after googling.

    After doing further research, by disabling the default proxy generation for the Entity Framework classes, we fixed the issue and we started getting another issue similar issue what we got when trying to add the classes used in our project and we applied similar fix by adding DataContract attribute for the classes and DataMember attribute for the members (Properties & Methods). Even after applying the changes we still not able to fix the error.

     Following is an example class, where we are suspecting the error is coming

    DataContract]

        public partial class User

        {

            #region Primitive Properties



            [DataMember]

            public virtual int Id

            {

                get;

                set;

            }



            [DataMember]

            public virtual string FullName

            {

                get;

                set;

            }



            #endregion



    #region Navigation Properties



            [DataMember]

            public virtual Account Account

            {

                get { return _account; }

                set

                {

                    if (!ReferenceEquals(_account, value))

                    {

                        var previousValue = _account;

                        _account = value;

                        FixupAccount(previousValue);

                    }

                }

            }

            private Account _account;



            [DataMember]

            public virtual ICollection<Client> Clients

            {

                get

                {

                    if (_clients == null)

                    {

                        var newCollection = new FixupCollection<Client>();

                        newCollection.CollectionChanged += FixupClients;

                        _clients = newCollection;

                    }

                    return _clients;

                }

                set

                {

                    if (!ReferenceEquals(_clients, value))

                    {

                        var previousValue = _clients as FixupCollection<Client>;

                        if (previousValue != null)

                        {

                            previousValue.CollectionChanged -= FixupClients;

                        }

                        _clients = value;

                        var newValue = value as FixupCollection<Client>;

                        if (newValue != null)

                        {

                            newValue.CollectionChanged += FixupClients;

                        }

                    }

                }

            }

            private ICollection<Client> _clients;



            #endregion

       }



    In the above class User has two members Account and Clients. Account is a direct property and the associated Clients for the user are represented as collection in the property Clients. It seems when a property returns a collection, it seems the User object is not able to serialize and getting errors related to serialization.

    Hope this has enough information.

    16 Nisan 2012 Pazartesi 17:25