C# 具有标识增量值的事务
我需要将发票添加到数据库中。每张发票有几行 我的课程是:C# 具有标识增量值的事务,c#,entity-framework,C#,Entity Framework,我需要将发票添加到数据库中。每张发票有几行 我的课程是: // Mapped to Invoice DB table public class Invoice { // Mapped to IDENTITY(1,1) field public int InvoiceNumber { get; set; } public List<InvoiceLine> InvoiceLineList { get; set; } } // Mapped to Invoice
// Mapped to Invoice DB table
public class Invoice
{
// Mapped to IDENTITY(1,1) field
public int InvoiceNumber { get; set; }
public List<InvoiceLine> InvoiceLineList { get; set; }
}
// Mapped to InvoiceLines DB table
public class InvoiceLine
{
// Must have same value as Invoice.InvoiceNumber
public int InvoiceNumber { get; set; }
public string ProductName { get; set; }
}
//映射到发票数据库表
公共类发票
{
//映射到标识(1,1)字段
public int InvoiceNumber{get;set;}
公共列表InvoiceLineList{get;set;}
}
//映射到InvoiceLines数据库表
公共类发票行
{
//必须与Invoice.InvoiceNumber具有相同的值
public int InvoiceNumber{get;set;}
公共字符串ProductName{get;set;}
}
我需要一个事务来以以下方式在发票和发票行表中插入记录:
IDENTITY(1,1)
)系统事务
还是应该使用具有事务的SQL存储过程?如果是-如何将发票行列表传递给存储过程?当然。只需启动一个事务,添加发票,SaveChanges()。SaveChanges()之后,新发票将填充其InvoiceNumber。使用该选项添加发票行、再次保存更改()并提交事务 或者添加导航属性并以这种方式关联它们。如果这样做,您就不需要事务,因为您将使用一个SaveChanges(),它总是在事务中运行