EF6 - Error inserting 2 related objects in a single SaveChanges
Posted by: Samuel Sippe
Date: March 02, 2014 10:06PM

*** Also posted to stackoverflow which is a bit easier to read for the formatting
http://stackoverflow.com/q/22138579/176868
***

Hi,

I am attepting to insert two related objects Client and Quote (where Quote has a Client) in a single call to save changes. Code is:

                      
            var client = new Client();
            db.Client.Add(client);            
            var quote = new Quote();
            quote.Client = client;
            db.Quote.Add(quote);

            db.SaveChanges();
 
Exception is:

Additional information: An error occurred while updating the entries. See the inner exception for details.

   at System.Data.Entity.Internal.InternalContext.SaveChanges()
   at System.Data.Entity.Internal.LazyInternalContext.SaveChanges()
   at System.Data.Entity.DbContext.SaveChanges()
  at MyTestClass... 

Inner Exception is:
{"The specified value is not an instance of a valid constant type.\r\nParameter name: type"}

   at System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.Update()
   at System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.<Update>b__2(UpdateTranslator ut)
   at System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.Update[T](T noChangesResult, Func`2 updateFunction, Boolean throwOnClosedConnection)
   at System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.Update(Boolean throwOnClosedConnection)
   at System.Data.Entity.Core.Objects.ObjectContext.<SaveChangesToStore>b__33()
   at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
   at System.Data.Entity.Core.Objects.ObjectContext.SaveChangesToStore(SaveOptions options, IDbExecutionStrategy executionStrategy)
   at System.Data.Entity.Core.Objects.ObjectContext.<>c__DisplayClass28.<SaveChanges>b__25()
   at System.Data.Entity.Infrastructure.DefaultExecutionStrategy.Execute[TResult](Func`1 operation)
   at System.Data.Entity.Core.Objects.ObjectContext.SaveChanges(SaveOptions options)
   at System.Data.Entity.Internal.InternalContext.SaveChanges()



Entity classes:

    public class Client
    {
        [Key]
        public long Id { get; set; }
        ....
    }

    public class Quote
    {
    
        [Column("id")]
        [Key]
        public long Id { get; set; }
   
        [Column("client")]
        [ForeignKey("Client")]
        public long ClientId { get; set; }

        public virtual Client Client { get; set; }
        ...
   }


Libraries:
EF6
MySql.Data.dll v6.8.3
MySql.Data.Entity.EF6.dll v6.8.3

If I add a db.SaveChanges() after the db.Client.Add(client) line, it will work ok but I want to use the unit of work pattern on group database calls where possible. Any ideas what could be going wrong?



Edited 2 time(s). Last edit at 03/04/2014 04:33PM by Samuel Sippe.

Options: ReplyQuote




Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.