0

我建立了一个简单的模型。StartDate 是 DateTime2 的原因是因为以前当我尝试使用 DateTime 时它给了我一个错误。

 public class Auction
{
    [Key]
    public string Id { get; set; }
    [Required]
    public string Title { get; set; }
    public string Description { get; set; }
    [Column(TypeName = "DateTime2")]
    [Display(Name = "StartTime")]
    public DateTime StartTime { get; set; }
    [Column(TypeName = "DateTime2")]

    public DateTime EndTime { get; set; }
    public int StartPrice { get; set; }
    public int CurrentPrice { get; set; }
    public string category { get; set; }

    public virtual Collection<Bid> Bids { get; private set; }

    public int BidCount
    {
        get { return Bids.Count; }
    }

    public Auction()
    {
        Bids = new Collection<Bid>();

    }

我在控制器中创建了一个 create() 函数。

     [HttpPost]
     public ActionResult create(Auction auction)
     {
         var categoryList = new SelectList(new[] { "auto", "elec", "games", "Home" });
         ViewBag.categoryList = categoryList;
         auction.StartTime= DateTime.Now;
         auction.EndTime = DateTime.Now.AddDays(7);

          if (ModelState.IsValid)
         {
             //Save the form
              var db = new AuctionsDataContext();
             db.Auctions.Add(auction);
             db.SaveChanges();

             return RedirectToAction("Index");

         }
         return View();

     }

当我尝试保存更改时出现此错误:

 Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.Entity.Validation.DbEntityValidationException: Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.
4

0 回答 0