Using Entity Framework to insert many rows into a table

c# entity-framework linq-to-entities

Question

I'm enjoying EF, but I've run into trouble.

In the beginning, I used the following line of code, which basically inserts some data into a table using ordinary linq.

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 that I've attempted to recreate the same, or comparable, code using the EF, I've completely failed.

I tried using the list and failed. AddTotblManagePreferences, but instead get an error message saying that the method is "oeDeprecated". Use the if you can. In its place, add the corresponding ObjectSet property's method.

Although I quickly examined ObjectSet, I'm still unsure on how to modify 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();

I would be really thankful if someone has experience with anything similar or could put me in the proper path.

Despite my enthusiasm, I can't help but feel like pig poop right now.

1
3
7/12/2010 10:23:23 AM

Accepted Answer

You must have rebuilt your model using Entity Framework designer, right? This ought to have produced a class particular to your model that derives from ObjectContext. This often finishes with "Entities."

Check out yourManagePreferencesEntities instance—or whatever you want to name yours. It ought to have a property that matches the table for your entities.tblManagePreferences maybe. That's theObjectSet for that particular table, and it has anAddObject a way through which you may add entities to that table.

Instead of the final five or so lines of code, try this:

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

List addition is not supported by ObjectSet by default, however you can easily write 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);
         }
     }
}
9
7/12/2010 10:38:38 AM

Popular Answer

I now use entity framework 5.
When utilizing:

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

then the database will only have the latest of your objects.

However, if you'll use:

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

All of your items will then be added.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow