2011-08-29 12 views
8

Mam obecnie encję, którą próbuję edytować za pomocą mojej aplikacji internetowej MVC 3. Otrzymuję DbUpdateConcurrencyException, gdy próbuję wykonać klienta wygrywa podejście otrzymałem od MSDN's post Using DbContext in EF 4.1 Part 9: Optimistic Concurrency Patterns. Dziwne jest to, że dzieje się to tylko w tej konkretnej istocie i nie robię nic innego od drugiej. Ponadto dzieje się tak tylko przy aktualizacji wartości zerowej do wartości. Właściwości powodujące błąd podczas aktualizacji z wartości pustej do wartości DateTime to DispositionLetterDate i DateDisposition.Entity Framework 4.1 - Code First - Nie można zaktualizować wartości null Data, podczas rozwiązywania DbUpdateConcurrencyException

Klasa:

public class A22 
{ 
    public A22() 
    { 
     this.IsArchived = false; 
     this.A22StatusId = (int)AStatus.Open; 
    } 

    [Key] 
    public int Id { get; set; } 

    [Required] 
    [StringLength(100, ErrorMessage="A22 Number cannot exceed 100 characters")] 
    public string Number { get; set; } 

    [Display(Name="Manual")] 
    public int ManualId { get; set; } 

    [Display(Name="SGMLID")] 
    public string SGMLId { get; set; } 

    [Required] 
    [DataType(DataType.Date)] 
    [Display(Name="Date Received")] 
    public DateTime DateReceived { get; set; } 

    [Display(Name= "Status")] 
    [EnumDataType(typeof(A22Status))] 
    public int A22StatusId { get; set; } 

    [Display(Name="Priority")] 
    [EnumDataType(typeof(A22Priority))] 
    public int A22PriorityId { get; set; } 

    [Display(Name="Providing Disposition")] 
    public string ProvidingDisposition { get; set; } 

    [Display(Name="Final Disposition")] 
    public bool FinalDisposition { get; set; } 

    [Display(Name="Is Archived")] 
    public bool IsArchived { get; set; } 

    [Display(Name="Created By")] 
    public int CreatedById { get; set; } 

    [Display(Name="Date Created")] 
    public DateTime DateCreated { get; set; } 

    [ConcurrencyCheck] 
    [Display(Name="Date Modified")] 
    public DateTime DateModified { get; set; } 

    [Display(Name = "Disposition Date")] 
    public DateTime? DateDisposition { get; set; } 

    [Display(Name = "Date Disposition Letter Sent")] 
    public DateTime? DispositionLetterDate{ get; set; } 

    // Virtual Properties 

    [ForeignKey("CreatedById")] 
    public virtual User CreatedBy { get; set; } 

    [ForeignKey("ManualId")] 
    public virtual Manual Manual { get; set; } 

    public virtual ICollection<A22Manual> A22ManualsImpacted { get; set; } 

    public virtual ICollection<A22Task> A22TasksImpacted { get; set; } 

    public virtual ICollection<A22Comment> Comments { get; set; } 

    public virtual ICollection<A22HistoryLog> HistoryLogs { get; set; } 
} 

Kontroler:

[HttpPost] 
public ActionResult Edit(A22 a22) 
{ 
    var d = new A22Repository().Find(a22.Id); 
    var changes = TrackChanges(d, a22); 
    if (ModelState.IsValid) { 
     if (!string.IsNullOrEmpty(changes)) 
     { 
      repository.InsertOrUpdate(a22); 
      this.repository.AddHistory(a22, changes); 
      repository.Save(); 
     } 
     return RedirectToAction("Details", new { id = a22.Id }); 
    } else { 
     ViewBag.PossibleManuals = d.ManualId == default(int) ? manualRepo.GetManualList() : 
                   manualRepo.GetManualList(d.ManualId); 
     ViewBag.APriority = repository.GetAPriorityList(d.APriorityId); 
     ViewBag.AStatus = repository.GetAStatusList(d.APriorityId); 
     return View(); 
     } 
    } 
} 

Widok:

@model TPMVC.Web.Models.A22 

@{ 
    Layout = "~/Views/Shared/_BlendLayoutLeftOnly.cshtml"; 
    ViewBag.Title = "Edit"; 
} 

<h2>Edit A22# @Model.Number</h2> 

@using (Html.BeginForm()) 
{ 
    @Html.ValidationSummary(true) 
    @Html.HiddenFor(model => model.Id) 
    @Html.HiddenFor(model => model.CreatedById) 
    @Html.HiddenFor(model => model.DateCreated) 
    @Html.HiddenFor(model => model.DateModified) 
    @Html.Partial("_CreateOrEdit") 

    <div class="newItemLabel"> 
     <strong style="padding-right: 145px;">@Html.LabelFor(model => model.AStatusId)</strong> 
    @{ 
     Html.Telerik().DropDownList() 
      .Name("AStatusId") 
      .BindTo((IEnumerable<SelectListItem>)ViewBag.AStatus) 
      .HtmlAttributes(new { @style = "width: 200px;" }) 
      .Render(); 
    } 
     @Html.ValidationMessageFor(model => model.AStatusId) 
    </div> 

    <div class="newItemLabel"> 
     <strong style="padding-right: 77px;">@Html.LabelFor(model => model.FinalDisposition)</strong> 
     @Html.EditorFor(model => model.FinalDisposition) 
    </div> 

    <div class="newItemLabel"> 
     <strong style="padding-right: 44px;">@Html.LabelFor(model => model.DateDisposition)</strong> 
     @{ 
      Html.Telerik().DatePickerFor(model => model.DateDisposition) 
      .Render();  
     } 
    </div> 

    <div class="newItemLabel"> 
     <strong style="padding-right: 44px;">@Html.LabelFor(model => model.DispositionLetterDate)</strong> 
     @{ 
      Html.Telerik().DatePickerFor(model => model.DispositionLetterDate) 
      .Render();  
     } 
    </div> 

    <div class="newItemLabel"> 
     <strong style="padding-right: 110px;">@Html.LabelFor(model => model.IsArchived)  </strong> 
     @Html.EditorFor(model => model.IsArchived) 
    </div> 

    <p> 
     <input type="submit" value="Save" /> 
    </p> 

} 

<div> 
    @Html.ActionLink("Back to List", "Index") 
</div> 

myśląc, że to mogło być coś z adnotacji danych, zdecydowałem się definiować właściwości z ten problem można opcjonalnie użyć Fluid API.

modelBuilder.Entity<A22>().Property(a => a.DateDisposition).IsOptional(); 
modelBuilder.Entity<A22>().Property(a => a.DispositionLetterDate).IsOptional(); 

W zasadzie potrzebuję świeżej pary oczu, aby zobaczyć, czy czegoś brakuje. Czy istnieje inna właściwość, która sprawia, że ​​zachowuje się w ten sposób?

Z góry dziękuję.

+0

Na jakiej bazie danych mapowana jest konfiguracja EF? MS SQL Server, MySQL, coś jeszcze? – Regfor

+0

Występuje błąd, który ma związek z aktualizowaniem wartości null z wartości null. Jeśli opublikujesz kod do InsertOrUpdate, mogę powiedzieć, czy go uderzasz, czy nie. – cadrell0

Odpowiedz

1

Mapuję zerowalne właściwości DateTime, takie jak następujące bez metod IsOptional. Działa również dobrze z MS SQL i MySQL przeze mnie.

this.Property(t => t.CreatedDate).HasColumnName("CreatedDate"); 
this.Property(t => t.ModifiedDate).HasColumnName("ModifiedDate"); 

w klasie pochodzącej z EntityTypeConfiguration. Używam podejścia Code First.

Powiązane problemy