Why can't I insert a record with a foreign key into a single server request?

I am trying to make a simple insert with a foreign key, but it seems to me that I need to use db.SaveChanges()for each record. How can I 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 records. See InnerException for more details. The specified value is not an instance of a valid constant type. Parameter name: value.

EDIT: Changed the sample code and added the returned exception.

+7
source share
2 answers

UNSIGNED BIGINT . SIGNED BIGINT, , .

+15

" ":

alt text

, :

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;
      }
   }
}

, .SaveChanges() - , .

- EF.....

+2

All Articles