我有一个Account
包含列表的类Payments
,其中包含一个列表Products
那是Pending
, Sold
or Refunded
.
当选择Products
要付款,我可以将它们添加到Payment
对象,然后将整个对象保存到Account
.
但是,当我在Pay
行动,Account.Payments
一片空白。
这里是AddProduct
action:
public ActionResult AddProduct(List<Product> products)
{
//var acc = Account.FetchAccountFromIdentity(User.Identity.GetUserName());
var username = User.Identity.GetUserName();
var acc = db.Accounts.First(u => u.EmailAddress == username);
var payment = new Payment();
foreach (var product in products)
{
if (product.IsSelected)
{
var tempProduct = db.Products.Find(product.ProductID);
payment.Products.Add(tempProduct);
payment.PaymentStatus = enPaymentStatus.Pending;
payment.Gross += tempProduct.Price;
payment.Vat += tempProduct.Price * 0.2;
payment.Net += payment.Gross - payment.Vat;
}
}
payment.AccountID = acc;
if (acc.Payments == null) acc.Payments = new List<Payment>();
acc.Payments.Add(payment);
db.SaveChanges();
return RedirectToAction("Pay", "Products", new {id = acc.AccountID} );
}
acc.Payments
此时已正确填充任何相关付款。
然后是Pay
action:
public ActionResult Pay(int? id)
{
var acc = db.Accounts.Find(id);
var data = new AccountPaymentView
{
Account = acc,
Payment = acc.Payments.First(p => p.PaymentStatus == enPaymentStatus.Pending)
};
return View(data);
}
acc.Payments
现在为空!?
知道我一定做错了什么吗?
Edit现在帐户类别:
public class Account
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int AccountID { get; set; }
[Display(Name = "Title")]
public string Salutation { get; set; }
[Required]
[Display(Name = "First Name")]
public string FirstName { get; set; }
[Required]
[Display(Name = "Last Name")]
public string LastName { get; set; }
[Required]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[Required]
[DataType(DataType.Password)]
[Compare("Password", ErrorMessage = "Passwords do not match. Please try again.")]
[Display(Name = "Confirm Password")]
public string ConfirmPass { get; set; }
[Required]
[DataType(DataType.EmailAddress)]
[Display(Name = "Email Address")]
public string EmailAddress { get; set; }
public virtual ICollection<Address> Addresses { get; set; }
public virtual ICollection<Payment> Payments { get; set; }
public DateTime CreatedOn { get; set; }
public virtual ICollection<DateTime> VistsList { get; set; }
public bool IsDeleted { get; set; }
public enAccountType AccountType { get; set; }
}
Edit 2这是 DBContext 上的 OnModelCreating 方法(删除了不相关的部分)。我是否遗漏了一些明显的东西?
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Properties<DateTime>()
.Configure(c => c.HasColumnType("datetime2"));
modelBuilder.Entity<Account>().HasOptional(x => x.Addresses).WithMany();
modelBuilder.Entity<Account>().HasOptional(x => x.Payments).WithMany();
modelBuilder.Entity<Payment>().HasOptional(x => x.Products).WithMany();
modelBuilder.Entity<Payment>().HasOptional(x => x.AccountID);
modelBuilder.Entity<Payment>().HasKey(x => x.PaymentID);
}
编辑3
检查了 Account.FetchAccountFromIdentity() ,它确实打开了一个新的上下文。所以现在它已被删除,我现在在 AddProduct 方法中使用上下文(上面更新了代码)。
现在保存时出现以下错误!
An object of type 'System.Collections.Generic.List`1[[PPPv2.Models.Payment, PPPv2,
Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]'
cannot be set or removed from the Value property of an EntityReference of type 'PPPv2.Models.Payment'.
我尝试在 OnModelCreating 方法中强制建立 PK 关系,但似乎没有帮助。
为了清楚起见,这里是 Payment 类:
public class Payment
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int PaymentID { get; set; }
public virtual Account AccountID { get; set; }
public virtual ICollection<Product> Products { get; set; }
public double Gross { get; set; }
public double Net { get; set; }
public double Vat { get; set; }
public string TransactionCode { get; set; }
public enPaymentStatus PaymentStatus { get; set; }
public bool IsDeleted { get; set; }
public DateTime PaidOn { get; set; }
public Payment()
{
Products = new List<Product>();
Gross = 0.0;
Net = 0.0;
Vat = 0.0;
IsDeleted = false;
PaidOn = new DateTime(2000, 1, 1, 0, 0,0);
}
}
编辑4
根据要求,以下是产品型号:
public class Product
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ProductID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
[DataType(DataType.Currency)]
[DisplayFormat(DataFormatString = "£{0:#,###0.00}")]
public double Price { get; set; }
[DataType(DataType.Currency)]
[Display(Name = "Total Estimated Legal Cost")]
[DisplayFormat(DataFormatString = "£{0:#,###0.00}")]
public double EstimatedTotalLegalCost { get; set; }
public virtual ICollection<Overview> Overviews { get; set; }
public virtual ICollection<Process> Processes { get; set; }
public DateTime CreatedOn { get; set; }
public DateTime LastUpdated { get; set; }
public bool IsSelected { get; set; }
public bool IsDeleted { get; set; }
}