Why can't I insert record with foreign key in a single server request?
Asked Answered
A

3

7

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.

Altis answered 24/5, 2010 at 8:6 Comment(4)
Show more details how you get "file" variable?Novelty
@Andrew: Added files variable source.Altis
It seems to me you have things a bit backward - you add an entry to DirectorySet with a reference to the file - shouldn't it be the other way around?? A file is located inside a directory - so the file should reference the directory - you'd have one Directory entry and any number of Files referencing that??Ripply
This is just a quick & dirty example of a principle. You're right, the names file & directory does not make any sense, but this is not my problem.Altis
A
16

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

Altis answered 1/6, 2010 at 14:31 Comment(2)
I had the same problem with UNSIGNED INTS using entity with a MySQL backend. What was your database?Dogcart
Me too with MySQL + UNSIGNED BIGINT.Octane
R
2

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.....

Ripply answered 24/5, 2010 at 9:25 Comment(2)
OK, I'll check it out and get back to you with the results. Thanks.Altis
I've tried it, but once again an exception was thrown (see updated question).Altis
B
0

it might be related with the implementation of AddToSalesFeedSet etc.. there is chance that you are doing commit inside ?

any way, my point is that i encountered very close problem, was tring to add relation to new entity with existed entity that been queried earlier - that has unsigned key and got the same exception;

the solution was to call Db.collection.Attach(previouslyQueriedEntityInstance);

Bedtime answered 8/12, 2019 at 10:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.