views:

49

answers:

2

I'm tryring to do a simple insert with foreign key, but it seems that I need to use db.SaveChanges() for every record insert. How can I manage to use only one db.SaveChanges() at the end of this program?

public static void Test()
{
    using (var entities = new DBEntities())
    {
        var sale =
            new SalesFeed
            {
                SaleName = "Stuff...",
            };
        entities.AddToSalesFeedSet(sale);

        var phone =
            new CustomerPhone
            {
                CreationDate = DateTime.UtcNow,
                sales_feeds = sale
            };
        entities.AddToCustomerPhoneSet(phone);

        entities.SaveChanges();
    }
}

After running the above code I get this exception:

System.Data.UpdateException: An error occurred while updating the entries. See the InnerException for details. The specified value is not an instance of a valid constant type Parameter name: value.

EDIT: Changed example code and added returned exception.

+2  A: 

I tried to do this "the right way":

alt text

And then I wrote this little test app to scan a directory, store the directory and all its files in two tables:

static void Main(string[] args)
{
   string directoryName = args[0];

   if(!Directory.Exists(directoryName))
   {
      Console.WriteLine("ERROR: Directory '{0}' does not exist!", directoryName);
      return;
   }

   using (testEntities entities = new testEntities())
   {
      StoredDir dir = new StoredDir{ DirName = directoryName };
      entities.AddToStoredDirSet(dir);

      foreach (string filename in Directory.GetFiles(directoryName))
      {
         StoredFile stFile = new StoredFile { FileName = Path.GetFileName(filename), Directory = dir };
         entities.AddToStoredFileSet(stFile);
      }

      try
      {
         entities.SaveChanges();
      }
      catch(Exception exc)
      {
         string message = exc.GetType().FullName + ": " + exc.Message;
      }
   }
}

As you can see, I only have a single call to .SaveChanges() at the very end - this works like a charm, everything's as expected.

Something about your approach must be screwing up the EF system.....

marc_s
OK, I'll check it out and get back to you with the results. Thanks.
Eran Betzalel
I've tried it, but once again an exception was thrown (see updated question).
Eran Betzalel
A: 

Apperantly using UNSIGNED BIGINT causes this problem. When I switched to SIGNED BIGINT everything worked as it supposed to.

Eran Betzalel