Asp.net mvc 如何在MVC模型上获取新生成的id
我有一个有多个实体的模型。例如,我有一个Asp.net mvc 如何在MVC模型上获取新生成的id,asp.net-mvc,entity-framework,Asp.net Mvc,Entity Framework,我有一个有多个实体的模型。例如,我有一个订单,它将客户ID转换为外键。在模型中,我有一些来自订单的字段和一些来自客户实体的字段。当我保存新记录时,我想先保存customer,获取新生成的标识值并将其放入order表中。如何做到这一点?我可以通过从customer表中获取MAX(ID)来做到这一点,但是我非常确信有更好的方法来处理这个问题 这是我的控制器方法: public ActionResult Create(OrderModels model, FormCollection form) {
订单
,它将客户ID
转换为外键。在模型中,我有一些来自订单
的字段和一些来自客户
实体的字段。当我保存新记录时,我想先保存customer
,获取新生成的标识值并将其放入order
表中。如何做到这一点?我可以通过从customer
表中获取MAX(ID)
来做到这一点,但是我非常确信有更好的方法来处理这个问题
这是我的控制器方法:
public ActionResult Create(OrderModels model, FormCollection form)
{
try
{
Customer customer = new Customer() { FirstName = model.FirstName, MiddleName = model.MiddleName, SecondName = model.SecondName, Email = model.Email, PhoneNbr = model.PhoneNbr };
int orderSource = Int32.Parse(form["OrderSourceList"]);
int paymentType = Int32.Parse(form["PaymentTypeList"]);
string warehouseGuid = form["Warehouses"];
ProductLogic productLogic = new ProductLogic();
Product product = productLogic.GetProductByArticle(model.Article);
DateTime now = DateTime.Now;
SalesOrder salesOrder = new SalesOrder()
{
OrderNbr = model.OrderNbr,
OrderSourceId = orderSource,
NpWarehouseRef = new Guid(warehouseGuid),
TTN = model.TTN,
OrderStatusId = 1,
PaymentTypeId = paymentType,
OrderDate = DateTime.Now
};
using (AccountingRepository repository = new AccountingRepository())
{
repository.AddOrUpdate<Customer>(customer);
repository.AddOrUpdate<SalesOrder>(salesOrder);
}
return RedirectToAction("Index");
}
catch (Exception e)
{
return View();
}
}
我的订单
类别:
using System.ComponentModel.DataAnnotations;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.ModelConfiguration;
namespace Accounting.Entity
{
[Table("Customer", Schema = "dbo")]
public class Customer
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int? Id { get; set; }
public string FirstName { get; set; }
public string MiddleName { get; set; }
public string SecondName { get; set; }
public string PhoneNbr { get; set; }
public string Email { get; set; }
public virtual HashSet<SalesOrder> SalesOrder { get; set; }
internal class Config : EntityTypeConfiguration<Customer>
{
public Config()
{
}
}
}
}
使用制度;
使用System.ComponentModel.DataAnnotations;
使用System.Collections.Generic;
使用System.ComponentModel.DataAnnotations.Schema;
使用System.Data.Entity.ModelConfiguration
namespace Accounting.Entity
{
[Table("SalesOrder", Schema = "dbo")]
public class SalesOrder
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int? Id { get; set; }
...
public int? CustomerId { get; set; }
...
public virtual Customer Customer { get; set; }
internal class Config : EntityTypeConfiguration<SalesOrder>
{
public Config()
{
HasRequired(r => r.Customer)
.WithMany(r => r.SalesOrder)
.HasForeignKey(r => r.CustomerId);
}
}
}
}
两种方式:
db.Customers.Add(customer);
db.SaveChanges();
order.CustomerId = customer.Id; // has a value now
order.Customer = customer;
db.Orders.Add(order);
db.SaveChanges(); // customer is saved first and the id is set for order.CustomerId automatically
db.Customers.Add(customer);
db.SaveChanges();
order.CustomerId = customer.Id; // has a value now
order.Customer = customer;
db.Orders.Add(order);
db.SaveChanges(); // customer is saved first and the id is set for order.CustomerId automatically
db.Customers.Add(customer);
db.SaveChanges();
order.CustomerId = customer.Id; // has a value now
order.Customer = customer;
db.Orders.Add(order);
db.SaveChanges(); // customer is saved first and the id is set for order.CustomerId automatically
db.Customers.Add(customer);
db.SaveChanges();
order.CustomerId = customer.Id; // has a value now
order.Customer = customer;
db.Orders.Add(order);
db.SaveChanges(); // customer is saved first and the id is set for order.CustomerId automatically
如果您使用EF,它将为您解决问题,如果您的模型上有导航属性。实际上,您使用EF为您处理关系,不需要获取最后记录或scopeidentity…我修改了问题并添加了控制器和实体类。您只需将客户实体附加到订单实体。应该为您处理FK客户id。如果您使用EF,它会为您处理它,如果您的模型上有导航属性。实际上,您使用EF来处理关系,不需要获取最后的记录或scopeidentity…我修改了问题并添加了控制器和实体类。您只需要将客户实体附加到订单实体。应该为您处理FK客户id。如果您使用EF,它会为您处理它,如果您的模型上有导航属性。实际上,您使用EF来处理关系,不需要获取最后的记录或scopeidentity…我修改了问题并添加了控制器和实体类。您只需要将客户实体附加到订单实体。应该为您处理FK客户id。如果您使用EF,它会为您处理它,如果您的模型上有导航属性。实际上,您使用EF来处理关系,不需要获取最后的记录或scopeidentity…我修改了问题并添加了控制器和实体类。您只需要将客户实体附加到订单实体。应该为您处理FK客户id。您能举个例子吗?我试着做:AccountingRepository=新建AccountingRepository();repository.AddOrUpdate(客户);Save();repository.AddOrUpdate(salesOrder);Save();repository.Dispose()//这并没有帮助我们看到上面的更新。如果这不适用于您的存储库方法,则表示存储库中的某些设置不正确。请给出一个示例?我试着做:AccountingRepository=新建AccountingRepository();repository.AddOrUpdate(客户);Save();repository.AddOrUpdate(salesOrder);Save();repository.Dispose()//这并没有帮助我们看到上面的更新。如果这不适用于您的存储库方法,则表示存储库中的某些设置不正确。请给出一个示例?我试着做:AccountingRepository=新建AccountingRepository();repository.AddOrUpdate(客户);Save();repository.AddOrUpdate(salesOrder);Save();repository.Dispose()//这并没有帮助我们看到上面的更新。如果这不适用于您的存储库方法,则表示存储库中的某些设置不正确。请给出一个示例?我试着做:AccountingRepository=新建AccountingRepository();repository.AddOrUpdate(客户);Save();repository.AddOrUpdate(salesOrder);Save();repository.Dispose()//这并没有帮助我们看到上面的更新。如果这对你的睡眠不起作用