title: Empty content item after create/edit in Orchard
body: <p>I'm using the guide to creating n-to-n relations in Orchard (ocs.orchardproject.net/Documentation/Creating-1-n-and-n-n-relations) with some slight modifications. While the sample code works well my own content part is always blank after I create or edit the item. I can't figure it out, because I <em>swear</em> my code is almost identical to theirs (with the exception of content parts having more/less unrelated fields).</p>
  
  <p>I suspect it might have to do with the Prefix in the Driver. I don't really know what the prefix is supposed to do, but setting it to one value produces a runtime error on create/edit, other values just produce result with all fields blank.</p>
  
  <p>The original sample works fine, so it has to be something I did or didn't do, but I just can't figure out what it is.</p>
  
  <p>Some relevant classes:</p>
  
  <pre><code>using System.Linq;
  using JetBrains.Annotations;
  using Orchard.ContentManagement;
  using Orchard.ContentManagement.Drivers;
  using ArealAds.Models;
  using ArealAds.Services;
  using ArealAds.ViewModels;
  
  namespace ArealAds.Drivers {
      [UsedImplicitly]
      public class StreetPartDriver : ContentPartDriver&lt;StreetPart&gt; {
          private readonly IStreetService _streetService;
  
          private const string TemplateName = "Parts/Street";
  
          public StreetPartDriver(IStreetService streetService) {
              _streetService = streetService;
          }
  
          // this one gives a runtime error with blank description,
          // other values produce result with all fields blank
          protected override string Prefix {
              get { return "Area"; }
          }
  
          protected override DriverResult Display(StreetPart part, string displayType, dynamic shapeHelper) {
              return ContentShape("Parts_Street",
                              () =&gt; shapeHelper.Parts_Street(
                                  ContentPart: part,
                                  Name: part.Name,
                                  Areas: part.Areas,
                                  Districts: part.Districts));
  
          }
  
          protected override DriverResult Editor(StreetPart part, dynamic shapeHelper) {
              return ContentShape("Parts_Street_Edit",
                      () =&gt; shapeHelper.EditorTemplate(
                          TemplateName: TemplateName,
                          Model: BuildEditorViewModel(part),
                          Prefix: Prefix));
          }
  
          protected override DriverResult Editor(StreetPart part, IUpdateModel updater, dynamic shapeHelper) {
              var model = new EditStreetViewModel();
              updater.TryUpdateModel(model, Prefix, null, null);
  
              if (part.ContentItem.Id != 0) {
                  _streetService.UpdateAreasForContentItem(part.ContentItem, model.Areas);
              }
  
              return Editor(part, shapeHelper);
          }
  
          private EditStreetViewModel BuildEditorViewModel(StreetPart part) {
              var itemAreas = part.Areas.ToLookup(r =&gt; r.Id);
              return new EditStreetViewModel {
                  Areas = _streetService.GetAreas().Select(r =&gt; new AreaEntry {
                      Area = r,
                      IsChecked = itemAreas.Contains(r.Id)
                  }).ToList()
              };
          }
      }
  }
  
  
  using System.Collections.Generic;
  using System.Linq;
  using Orchard;
  using Orchard.ContentManagement;
  using Orchard.Data;
  using ArealAds.Models;
  using ArealAds.ViewModels;
  
  namespace ArealAds.Services {
      public interface IStreetService : IDependency {
          void UpdateAreasForContentItem(ContentItem item, IEnumerable&lt;AreaEntry&gt; areas);
          IEnumerable&lt;AreaRecord&gt; GetAreas();
      }
  
      public class StreetService : IStreetService {
          private readonly IRepository&lt;AreaRecord&gt; _areaRepository;
          private readonly IRepository&lt;StreetAreaRecord&gt; _streetAreaRepository;
  
          public StreetService(
              IRepository&lt;AreaRecord&gt; areaRepository,
              IRepository&lt;StreetAreaRecord&gt; streetAreaRepository) {
  
              _areaRepository = areaRepository;
              _streetAreaRepository = streetAreaRepository;
          }
  
          public void UpdateAreasForContentItem(ContentItem item, IEnumerable&lt;AreaEntry&gt; areas) {
              var record = item.As&lt;StreetPart&gt;().Record;
              var oldAreas = _streetAreaRepository.Fetch(
                  r =&gt; r.StreetRecord == record);
              var lookupNew = areas
                  .Where(e =&gt; e.IsChecked)
                  .Select(e =&gt; e.Area)
                  .ToDictionary(r =&gt; r, r =&gt; false);
              // Delete the areas that are no longer there and mark the ones that should stay
              foreach(var streetAreaRecord in oldAreas) {
                  if (lookupNew.ContainsKey(streetAreaRecord.AreaRecord)) {
                      lookupNew[streetAreaRecord.AreaRecord] = true;
                  }
                  else {
                      _streetAreaRepository.Delete(streetAreaRecord);
                  }
              }
              // Add the new areas
              foreach(var area in lookupNew.Where(kvp =&gt; !kvp.Value).Select(kvp =&gt; kvp.Key)) {
                  _streetAreaRepository.Create(new StreetAreaRecord {
                      StreetRecord = record,
                      AreaRecord = area
                  });
              }
          }
  
          public IEnumerable&lt;AreaRecord&gt; GetAreas() {
              return _areaRepository.Table.ToList();
          }
      }
  }
  
  
  using System.ComponentModel.DataAnnotations;
  using Orchard.ContentManagement;
  using Orchard.ContentManagement.Records;
  
  namespace ArealAds.Models {
      public class StreetAreaRecord : ContentPartRecord {
          public virtual StreetRecord StreetRecord { get; set; }
          public virtual AreaRecord AreaRecord { get; set; }
      }
  }
  
  using System;
  using System.Collections.Generic;
  using System.Data;
  using Orchard.ContentManagement.Drivers;
  using Orchard.ContentManagement.MetaData;
  using Orchard.ContentManagement.MetaData.Builders;
  using Orchard.Core.Contents.Extensions;
  using Orchard.Data.Migration;
  using ArealAds.Models;
  
  namespace ArealAds {
      public class Migrations : DataMigrationImpl {
  
          public int Create() {
              //
              // Street-Area-District
              //
  
              SchemaBuilder.CreateTable("DistrictRecord", table =&gt; table
                  .ContentPartRecord()
                  .Column&lt;string&gt;("Name")
              );
  
              ContentDefinitionManager.AlterPartDefinition(
                  typeof(DistrictPart).Name, cfg =&gt; cfg.Attachable());
  
              ContentDefinitionManager.AlterTypeDefinition(
                  "District", cfg =&gt; cfg
                  .WithPart("CommonPart")
                  .WithPart("DistrictPart")
                  .Creatable()
              );
  
              SchemaBuilder.CreateTable("AreaRecord", table =&gt; table
                  .ContentPartRecord()
                  .Column&lt;string&gt;("Name")
                  .Column&lt;int&gt;("DistrictRecord_Id")
              );
  
              ContentDefinitionManager.AlterPartDefinition(
                  typeof(AreaPart).Name, cfg =&gt; cfg.Attachable());
  
              ContentDefinitionManager.AlterTypeDefinition(
                  "Area", cfg =&gt; cfg
                  .WithPart("CommonPart")
                  .WithPart("AreaPart")
                  .Creatable()
              );
  
              SchemaBuilder.CreateTable("StreetRecord", table =&gt; table
                  .ContentPartRecord()
                  .Column&lt;string&gt;("Name")
              );
  
              ContentDefinitionManager.AlterPartDefinition(
                  typeof(StreetPart).Name, cfg =&gt; cfg.Attachable());
  
              ContentDefinitionManager.AlterTypeDefinition(
                  "Street", cfg =&gt; cfg
                  .WithPart("CommonPart")
                  .WithPart("StreetPart")
                  .Creatable()
              );
  
              SchemaBuilder.CreateTable("StreetAreaRecord", table =&gt; table
                  .Column&lt;int&gt;("Id", column =&gt; column.PrimaryKey().Identity())
                  .Column&lt;int&gt;("StreetRecord_Id")
                  .Column&lt;int&gt;("AreaRecord_Id")
              );
  
  
              //
              // Address-Ad
              //
  
              SchemaBuilder.CreateTable("AddressRecord", table =&gt; table
                  .ContentPartRecord()
                  .Column&lt;int&gt;("StreetRecord_Id")
                  .Column&lt;int&gt;("Building")
                  .Column&lt;int&gt;("Kor")
                  .Column&lt;int&gt;("Str")
                  .Column&lt;int&gt;("Vl")
                  .Column&lt;string&gt;("Note")
                  .Column&lt;int&gt;("AreaRecord_Id")
                  .Column&lt;int&gt;("DistrictRecord_Id")
                  .Column&lt;string&gt;("Phone1")
                  .Column&lt;string&gt;("Phone2")
                  .Column&lt;string&gt;("Phone3")
              );
  
              ContentDefinitionManager.AlterPartDefinition(
                  typeof(AddressPart).Name, cfg =&gt; cfg.Attachable());
  
  
              return 1;
          }
      }
  }
  
  @model ArealAds.ViewModels.EditStreetViewModel
  &lt;fieldset&gt;
      &lt;legend&gt;Улица&lt;/legend&gt;
  
      &lt;div class="editor-label"&gt;
          @Html.LabelFor(model =&gt; model.Street.Name)
      &lt;/div&gt;
      &lt;div class="editor-field"&gt;
          @Html.TextBoxFor(model =&gt; model.Street.Name)
          @Html.ValidationMessageFor(model =&gt; model.Street.Name)
      &lt;/div&gt;
  &lt;ul&gt;
  
  @for (int i = 0; i &lt; Model.Areas.Count; i++) {
      &lt;li&gt;
          &lt;input type="hidden" value="@Model.Areas[i].Area.Id"
                 name="@Html.FieldNameFor(m =&gt; m.Areas[i].Area.Id)"/&gt;
  
          &lt;label for="@Html.FieldNameFor(m =&gt; m.Areas[i].IsChecked)"&gt;
  
              &lt;input type="checkbox" value="true"
                  name="@Html.FieldNameFor(m =&gt; m.Areas[i].IsChecked)"
                  id="@Html.FieldNameFor(m =&gt; m.Areas[i].IsChecked)"
                  @if (Model.Areas[i].IsChecked) {&lt;text&gt;checked="checked"&lt;/text&gt;}/&gt;
  
              @Model.Areas[i].Area.Name
          &lt;/label&gt;
      &lt;/li&gt;
  }
  &lt;/ul&gt;
  &lt;/fieldset&gt;
  </code></pre>
  
  <p>I had been beating my head against the wall on this for days, please make any suggestions you feel might theoretically help 'cos I'm desperate :(</p>
  
  <p>UPD: The StreetHandler class:</p>
  
  <pre><code>using ArealAds.Models;
  using Orchard.ContentManagement.Handlers;
  using Orchard.Data;
  
  namespace ArealAds.Handlers {
      public class StreetHandler : ContentHandler {
          public StreetHandler(IRepository&lt;StreetRecord&gt; repository) {
              Filters.Add(StorageFilter.For(repository));
          }
      }
  }
  </code></pre>
  
  <p>There is an exception on the log:</p>
  
  <pre><code>2012-04-10 00:07:58,515 [7] Orchard.ContentManagement.Drivers.Coordinators.ContentPartDriverCoordinator - IdentifierGenerationException thrown from IContentPartDriver by ArealAds.Drivers.StreetPartDriver
  NHibernate.Id.IdentifierGenerationException: attempted to assign id from null one-to-one property: ContentItemRecord
     â NHibernate.Id.ForeignGenerator.Generate(ISessionImplementor sessionImplementor, Object obj)
     â NHibernate.Event.Default.AbstractSaveEventListener.SaveWithGeneratedId(Object entity, String entityName, Object anything, IEventSource source, Boolean requiresImmediateIdAccess)
     â NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.SaveWithGeneratedOrRequestedId(SaveOrUpdateEvent event)
     â NHibernate.Event.Default.DefaultSaveEventListener.SaveWithGeneratedOrRequestedId(SaveOrUpdateEvent event)
     â NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.EntityIsTransient(SaveOrUpdateEvent event)
     â NHibernate.Event.Default.DefaultSaveEventListener.PerformSaveOrUpdate(SaveOrUpdateEvent event)
     â NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.OnSaveOrUpdate(SaveOrUpdateEvent event)
     â NHibernate.Impl.SessionImpl.FireSave(SaveOrUpdateEvent event)
     â NHibernate.Impl.SessionImpl.Save(Object obj)
     â Orchard.Data.Repository`1.Create(T entity) â d:\TeamCity\Projects\Orchard-Default\src\Orchard\Data\Repository.cs:ñòðîêà 96
     â Orchard.Data.Repository`1.Orchard.Data.IRepository&lt;T&gt;.Create(T entity) â d:\TeamCity\Projects\Orchard-Default\src\Orchard\Data\Repository.cs:ñòðîêà 36
     â ArealAds.Services.StreetService.UpdateAreasForContentItem(ContentItem item, IEnumerable`1 areas) â c:\Users\Mom\Teritoriya\Modules\ArealAds\Services\Street.cs:ñòðîêà 46
     â ArealAds.Drivers.StreetPartDriver.Editor(StreetPart part, IUpdateModel updater, Object shapeHelper) â c:\Users\Mom\Teritoriya\Modules\ArealAds\Controllers\Street.cs:ñòðîêà 47
     â System.Dynamic.UpdateDelegates.UpdateAndExecute4[T0,T1,T2,T3,TRet](CallSite site, T0 arg0, T1 arg1, T2 arg2, T3 arg3)
     â Orchard.ContentManagement.Drivers.ContentPartDriver`1.Orchard.ContentManagement.Drivers.IContentPartDriver.UpdateEditor(UpdateEditorContext context) â d:\TeamCity\Projects\Orchard-Default\src\Orchard\ContentManagement\Drivers\ContentPartDriver.cs:ñòðîêà 30
     â Orchard.ContentManagement.Drivers.Coordinators.ContentPartDriverCoordinator.&lt;&gt;c__DisplayClass10.&lt;UpdateEditor&gt;b__f(IContentPartDriver driver) â d:\TeamCity\Projects\Orchard-Default\src\Orchard\ContentManagement\Drivers\Coordinators\ContentPartDriverCoordinator.cs:ñòðîêà 61
     â Orchard.InvokeExtensions.Invoke[TEvents](IEnumerable`1 events, Action`1 dispatch, ILogger logger) â d:\TeamCity\Projects\Orchard-Default\src\Orchard\InvokeExtensions.cs:ñòðîêà 19
  </code></pre>
  
  <p>EDIT: some model classes:</p>
  
  <pre><code>using System.ComponentModel.DataAnnotations;
  using Orchard.ContentManagement;
  using Orchard.ContentManagement.Records;
  
  namespace ArealAds.Models {
      public class AreaRecord : ContentPartRecord {
          public virtual string Name { get; set; }
          public virtual DistrictRecord DistrictRecord { get; set; }
      }
  
      public class AreaPart : ContentPart&lt;AreaRecord&gt; {
          [Required]
          public string Name {
              get { return Record.Name; }
              set { Record.Name = value; }
          }
  
          [Required]
          public DistrictRecord DistrictRecord {
              get { return Record.DistrictRecord; }
              set { Record.DistrictRecord = value; }
          }
      }
  }
  
  using System.Collections.Generic;
  using System.Linq;
  using System.ComponentModel.DataAnnotations;
  using Orchard.ContentManagement;
  using Orchard.ContentManagement.Records;
  
  namespace ArealAds.Models {
      public class StreetRecord : ContentPartRecord {
          public virtual string Name { get; set; }
          public virtual IList&lt;StreetAreaRecord&gt; Areas { get; set; }
  
          public StreetRecord() {
              Areas = new List&lt;StreetAreaRecord&gt;();
          }
      }
  
      public class StreetPart : ContentPart&lt;StreetRecord&gt; {
          [Required]
          public string Name {
              get { return Record.Name; }
              set { Record.Name = value; }
          }
  
          public IEnumerable&lt;AreaRecord&gt; Areas {
              get {
                  return Record.Areas.Select (r =&gt; r.AreaRecord);
              }
          }
  
          public IEnumerable&lt;DistrictRecord&gt; Districts {
              get {
                  return Record.Areas.Select (r =&gt; r.AreaRecord.DistrictRecord).Distinct();
              }
          }
      }
  }
  
  using System.ComponentModel.DataAnnotations;
  using Orchard.ContentManagement;
  using Orchard.ContentManagement.Records;
  
  namespace ArealAds.Models {
      public class StreetAreaRecord : ContentPartRecord {
          public virtual StreetRecord StreetRecord { get; set; }
          public virtual AreaRecord AreaRecord { get; set; }
      }
  }
  
  using ArealAds.Models;
  using Orchard.ContentManagement.Handlers;
  using Orchard.Data;
  
  namespace ArealAds.Handlers {
      public class AreaHandler : ContentHandler {
          public AreaHandler(IRepository&lt;AreaRecord&gt; repository) {
              Filters.Add(StorageFilter.For(repository));
          }
      }
  }
  
  using ArealAds.Models;
  using Orchard.ContentManagement.Handlers;
  using Orchard.Data;
  
  namespace ArealAds.Handlers {
      public class StreetHandler : ContentHandler {
          public StreetHandler(IRepository&lt;StreetRecord&gt; repository) {
              Filters.Add(StorageFilter.For(repository));
          }
      }
  }
  </code></pre>