我正在使用EF6和MySql,并在一次调用中插入两个新的相关对象客户端和报价(其中报价具有客户端)以保存更改。这应该映射到客户端表的插入和报价表的插入。代码是:
var client = new Client();
db.Client.Add(client);
var quote = new Quote();
quote.Client = client;
db.Quote.Add(quote);
db.SaveChanges();
例外情况是:
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...
内部例外是:
{"The specified value is not an instance of a valid constant type.rnParameter 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()
实体类:
public class Client
{
//AUTO_INCREMENT
[Key]
public long Id { get; set; }
....
}
public class Quote
{
//AUTO_INCREMENT
[Column("id")]
[Key]
public long Id { get; set; }
[Column("client")]
[ForeignKey("Client")]
public long ClientId { get; set; }
public virtual Client Client { get; set; }
...
}
图书馆:
- EF6
- MySql.Data.dll v6.8.3
- MySql.Data.Entity.EF6.dll v6.8.3
如果我添加一个数据库。保存 db 之后的更改()。Client.Add(client) 行,它可以正常工作,但我想尽可能晚地进行 SavChanges 以对数据库调用进行分组。任何想法可能出错?
我无法重现这一点,有关我的测试用例的更多详细信息可在 http://forums.mysql.com/read.php?38,608603,608603#msg-608603 中找到(您也发布了问题)。
谢谢。
只需在客户端类中定义一个引号集合,例如
public IList<Quotes> quotes {get;set;}
并添加记录,例如
var client = new Client();
var quote = new Quote();
client.Quotes.Add(quote);
db.client.Add(client);
db.SaveChanges();
并尝试
问题是由于有符号/无符号的 bigint 造成的,如此处所述。