0
votes

I have a model in this way :

Users => Profiles => modules => treatments

( a user has on profile, a profile has many modules , a module has many treatement )

this model is Reverse engineeed by Entity Framwork code first

when I'm using this, in a simple asmx file, to ask for a user, I Got this Error :

A circular reference was detected while serializing an object of type 'CDU.Entities.Models.User'.

this is because the profile entity contrains a list of users that have their pofiles ... etc.

In My data context , I disabled the lazy loading , It Seems Ok for authentication, in my retrieved User's Profile, I have the profile Id , but the profile Entity is not loaded.

            this.Configuration.LazyLoadingEnabled = false;

Great!

once authenticated, I have to build the users Menu , based on modules and treatements in his profile.

so i asked the object to load the profile :

        User user = new User();
        using (cduContext db = new cduContext())
        {
            string encryptedPassword = Encryption.Encrypt(password);
            user = (from u in db.Users
                    where u.UserName.Equals(login) &&
                          u.Password.Equals(encryptedPassword)
                    select u).FirstOrDefault();
            // Including the user's Profile
            user = db.Users.Include("Profile").FirstOrDefault();
        }
        return user;

the profile is loaded , but his sub entities too, for instance the users list of this profile !

and then , I'm enjoying the same error :

A circular reference was detected while serializing an object of type 'CDU.Entities.Models.User'.

why the profile is "lazy loading" ?

I need to include these entities on demand ... How can I please ?

1

1 Answers

0
votes

Try getting the profile instead.

  profile = (from p in db.Profiles 
                where p.User.UserName.Equals(login) &&
                      p.User.Password.Equals(encryptedPassword)
                select p).FirstOrDefault();