尝试在MySQL数据库中插入数据时获取DbUpdateException。
已使用Pomelo.EntityFrameworkCore.MySql包从数据库中搭建模型,
该项目是在.net core 2.1中创建的
堆栈跟踪:
异常消息:无法添加或更新子行:外键约束失败(HMEBooking
。Invoice
,CONSTRAINT Invoice_ibfk_7
外键(BookingId
)参考Booking
(Id
)ON删除不执行任何操作更新无操作)
堆栈跟踪:位于C:\ projects \ mysqlconnector \ src \ MySqlConnector \ MySql.Data.MySqlClient \ MySqlDataReader.cs:line 93中的MySql.Data.MySqlClient.MySqlDataReader.ActivateResultSet(ResultSet resultSet)
在C:\ projects \ mysqlconnector \ src \ MySqlConnector \ MySql.Data.MySqlClient \ MySqlDataReader.cs:line 328处的MySql.Data.MySqlClient.MySqlDataReader.ReadFirstResultSetAsync(IOBehavior ioBehavior)
在MySql.Data.MySqlClient.MySqlDataReader.CreateAsync(MySqlCommand命令,CommandBehavior行为,IOBehavior ioBehavior)在C:\ projects \ mysqlconnector \ src \ MySqlConnector \ MySql.Data.MySqlClient \ MySqlDataReader.cs:line 313
在MySqlConnector.Core.TextCommandExecutor.ExecuteReaderAsync(String commandText,MySqlParameterCollection parameterCollection,CommandBehavior behavior,IOBehavior ioBehavior,CancellationToken cancellingToken)在C:\ projects \ mysqlconnector \ src \ MySqlConnector \ Core \ TextCommandExecutor.cs:line 73
在C:\ projects \ mysqlconnector \ src \ MySqlConnector \ MySql.Data.MySqlClient \ MySqlCommand.cs:line 168处的MySql.Data.MySqlClient.MySqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
在Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.Execute(IRelationalConnection连接,DbCommandMethod executeMethod,IReadOnlyDictionary 2 parameterValues) at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.ExecuteReader(IRelationalConnection connection, IReadOnlyDictionary
2 parameterValues)
在Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.Execute(IRelationalConnection连接)
有预订,预订详细信息和发票模型。
BookingDetail与预订有关系,
发票与Booking和BookingDetail有关系。
在向数据库添加条目时出现错误无法添加或更新子行:外键约束失败。
但是,当我在非.net核心应用程序中使用相同的模型时,它可以工作
请让我知道我是否在某处出错。
感谢您的帮助。
以下是使用的模型
public partial class Booking
{
public Booking()
{
BookingDetail = new HashSet<BookingDetail>();
Invoice = new HashSet<Invoice>();
}
[Column(TypeName = "bigint(20)")]
public long Id { get; set; }
[Required]
[Column(TypeName = "varchar(50)")]
public string TempId { get; set; }
[InverseProperty("Booking")]
public ICollection<BookingDetail> BookingDetail { get; set; }
[InverseProperty("Booking")]
public ICollection<Invoice> Invoice { get; set; }
}
public partial class BookingDetail
{
public BookingDetail()
{
Invoice = new HashSet<Invoice>();
}
[Column(TypeName = "bigint(20)")]
public long Id { get; set; }
[Required]
[Column(TypeName = "varchar(50)")]
public string CreatedBy { get; set; }
[Column(TypeName = "datetime")]
public DateTime CreatedOn { get; set; }
[Required]
[Column(TypeName = "varchar(50)")]
public string ModifiedBy { get; set; }
[Column(TypeName = "datetime")]
public DateTime ModifiedOn { get; set; }
[Column(TypeName = "bigint(20)")]
public long BookingId { get; set; }
[ForeignKey("BookingId")]
[InverseProperty("BookingDetail")]
public Booking Booking { get; set; }
[InverseProperty("BookingDetail")]
public ICollection<Invoice> Invoice { get; set; }
}
public partial class Invoice
{
[Column(TypeName = "bigint(20)")]
public long Id { get; set; }
[Column(TypeName = "bigint(20)")]
public long BookingId { get; set; }
[Column(TypeName = "bigint(20)")]
public long BookingDetailId { get; set; }
[Column(TypeName = "datetime")]
public DateTime CreatedOn { get; set; }
[Column(TypeName = "datetime")]
public DateTime CreatedBy { get; set; }
[Column(TypeName = "datetime")]
public DateTime ModifiedOn { get; set; }
[Required]
[Column(TypeName = "varchar(50)")]
public string ModifiedBy { get; set; }
[ForeignKey("BookingId")]
[InverseProperty("Invoice")]
public Booking Booking { get; set; }
[ForeignKey("BookingDetailId")]
[InverseProperty("Invoice")]
public BookingDetail BookingDetail { get; set; }
}
public class Class1
{
HMEBookingContext context = new HMEBookingContext();
#region declarations
public long BookingId { get; set; }
public string CustomerEmailId { get; set; } = "[email protected]";
#endregion
Random rnd = new Random();
public void insert()
{
try
{
Booking booking = new Booking();
booking.TempBookingId = BookingRefNumber.ToString();
var bookingDetail = new List<BookingDetail>();
BookingDetail detail = new BookingDetail();
detail.CreatedBy = CustomerEmailId;
detail.CreatedOn = DateTime.Now;
detail.ModifiedBy = "1";
detail.ModifiedOn = DateTime.UtcNow;
detail.BookingStatusId = 1;
var invoices = new List<Invoice>();
Invoice invoice = new Invoice();
invoices.Add(invoice);
detail.Invoice = invoices;
bookingDetail.Add(detail);
booking.BookingDetail = bookingDetail;
context.Add(booking);
context.SaveChanges(true);
BookingId=booking.Id
}
catch(DbUpdateException upd)
{
}
catch(Exception ex)
{
}
}
}
预订-预订详细信息,预订-发票,预订详细信息-发票之间具有一对多关系
EF核心版本:2.1
数据库提供程序:Pomelo.EntityFrameworkCore.MySql
IDE:Visual Studio 2017
最佳答案
错误是Invoice
记录的Invoice.BookingId
值无效。
查看您的测试代码,您将创建一个Invoice
对象invoice
;您永远不会分配invoice.Booking
属性,也不会将invoice
添加到booking.Invoices
集合中,因此两者之间没有关系b / t,因此使用Invoice.BookingId
的默认值,并且PK值表Booking
中不存在。
将booking
分配给invoice.Booking
或将invoice
添加到booking.Invoices
,该错误应消失。
但是,此解决方案不能纠正设计缺陷-Invoice
引用Booking
和BookingDetail
的情况,然后BookingDetail
可以引用不同的Booking
记录,从而导致数据不一致。 Invoice
(Invoice -> BookingDetail
或Invoice -> Booking
)应该存在单个功能依赖性,不能同时存在。
关于mysql - 无法添加或更新子行:外键约束失败MYSQL:EF核心,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52365037/