Quantcast
Channel: MySQL Forums - Connector/NET and C#, Mono, .Net
Viewing all articles
Browse latest Browse all 1451

EF6 - Error inserting 2 related objects in a single SaveChanges (no replies)

$
0
0
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?

Viewing all articles
Browse latest Browse all 1451

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>