3

I’m having fun with EF and have come unstuck.

Originally I used the following bit of code using standard linq that essentially enters some data into a table.

ManagePreferencesDataContext manpref = new ManagePreferencesDataContext();

                tblManagePreference prefMemberID = new tblManagePreference();
                {
                    prefMemberID.Username = CreateUserWizard1.UserName;
                    prefMemberID.MemberID = tbxMemberID.Text.ToString();
                    prefMemberID.LocationID = tbxLocationID.Text.ToString();
                    prefMemberID.Preference = "MemberID";
                }

                tblManagePreference prefLocationID = new tblManagePreference();
                {
                    prefLocationID.Username = CreateUserWizard1.UserName;
                    prefLocationID.MemberID = tbxMemberID.Text.ToString();
                    prefLocationID.LocationID = tbxLocationID.Text.ToString();
                    prefLocationID.Preference = "LocationID";
                }


                List<tblManagePreference> ie = new List<tblManagePreference>();
                ie.Add(prefMemberID);
                ie.Add(prefLocationID);

                manpref.tblManagePreferences.InsertAllOnSubmit(ie);
                manpref.SubmitChanges();

Now, I’ve tried to replicate the same, or similar code using the EF and have totally come unstuck.

I tried to use the list and .AddTotblManagePreferences but receive a “Deprecated Method for adding a new object to the tblManagePreferences EntitySet. Consider using the .Add method of the associated ObjectSet property instead.

I’ve had a look at ObjectSet briefly but I’m not really sure how to amend the code.

    VDSORDAL.PDC_VDSOREntities manpref = new PDC_VDSOREntities();
        tblUserPreference prefMemberID = new tblUserPreference();

        {
            prefMemberID.Username = CreateUserWizard1.UserName;
            prefMemberID.MemberID = tbxMemberID.Text.ToString();
            prefMemberID.LocationID = tbxLocationID.Text.ToString();
            prefMemberID.ColumnName = "MemberID";


        }

        tblUserPreference prefLocationID = new tblUserPreference();
        {
            prefLocationID.Username = CreateUserWizard1.UserName;
            prefLocationID.MemberID = tbxMemberID.Text.ToString();
            prefLocationID.LocationID = tbxLocationID.Text.ToString();
            prefLocationID.ColumnName = "LocationID";
        }

    List<tblUserPreference> ie = new List<tblUserPreference>();
        ie.Add(prefMemberID);
        ie.Add(prefLocationID);


        manpref.AddObject(PDC_VDSOREntities,ie);
        manpref.SaveChanges();

If anyone has used something along these lines before or could point me in the right direction, I’d be most grateful.

Although enthusiastic, I can't help but feeling thick as pig poo at the moment.

1
  • Yes there is a property called tblUserPreferences (Sorry - I tried adding it to our comment thread but I wasn't allowed : S ) Commented Jul 12, 2010 at 10:33

3 Answers 3

9

I presume you have recreated your model in the Entity Framework designer? This should have created a class deriving from ObjectContext specific to your model. By convention, this ends in "Entities".

Have a look at your ManagePreferencesEntities instance - or whatever yours is called. It should have a property on it corresponding to the table for your entities, tblManagePreferences perhaps. This is the ObjectSet instance for that specific table, and it has an AddObject method that you can use to add entities into that table.

Try this instead of the last 5 or so lines of code:

manpref.tblUserPreferences.AddObject(prefMemberId);
manpref.tblUserPreferences.AddObject(prefLocationId);
manpref.SaveChanges();

By default ObjectSet doesn't support adding lists of things, but it's easy to create your own extension method:

public static class ObjectSetExtensions
{
     public static void AddObjects<T>(this ObjectSet<T> objectSet, IEnumerable<T> objects)
     {
         foreach (var item in objects)
         {
            objectSet.AddObject(item);
         }
     }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Hello Samuel, thanks for looking at this for me. I have attempted the following (see edit above) but without success. Apologies for being thick - very new to all of this and qute out of my depth!
Can you confirm that there is a property called tblUserPreferences on PDC_VDSOREntities?
Brilliant stuff - thank you very much Sam. very useful indeed. I owe you a pint.
1

Try the following code:


manPref.tblManagePreference.Add(prefMemberID);
manPref.tblManagePreference.Add(prefLocationID);
manPref.SaveChanges();

Comments

1

I'm using entity framework 5 now.
When you use:

objectcontext.Add(yourobject1);
objectcontext.Add(yourobject2);
objectcontext.SaveChanges();

then only last of your objects will be inserted into database.

But if you will use:

objectcontext.Entry(yourobject1).State = System.Data.EntityState.Added;
objectcontext.Entry(yourobject2).State = System.Data.EntityState.Added;
objectcontext.SaveChanges();

Then all of your objects will be added.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.