title: How to save custom data in Orchard CMS database
body: <p>I have created a module that i want to collect some simple data in Orchard's database, so i have created model, migration and handler for it:</p>
  
  <p><code>Models/StatePartRecord.cs</code> :</p>
  
  <pre><code> namespace Address.Models
      {
          public class StatePart : ContentPart&lt;StatePartRecord&gt;
          {
              public int Id
              {
                  get { return Record.Id; }
                  set { Record.Id = value; }
              }
              public string StateName
              {
                  get { return Record.StateName; }
                  set { Record.StateName = value; }
              }
          }
      }
  </code></pre>
  
  <p><code>Models/StatePartRecord.cs</code> :</p>
  
  <pre><code>namespace Address.Models
  {
      public class StatePartRecord : ContentPartRecord
      {
          public virtual int Id { get; set; }
          public virtual string StateName { get; set; }
      }
  }
  </code></pre>
  
  <p><code>Migrations.cs</code> :</p>
  
  <pre><code>namespace Address
  {
      public class Migrations : DataMigrationImpl
      {
          public int Create()
          {
              SchemaBuilder.CreateTable("StatePartRecord", table =&gt; table
                  .ContentPartRecord()
                  .Column&lt;string&gt;("StateName")
                  );
  
              return 1;
          }
          public int UpdateFrom1()
          {
              ContentDefinitionManager.AlterPartDefinition("State", part =&gt; part
                  .Attachable());
  
              return 2;
          }
  
      }
  }
  </code></pre>
  
  <p><code>Handlers/StatePartHandler.cs</code> :</p>
  
  <pre><code>namespace Address.Handlers
  {
      public class StatePartHandler : ContentHandler
      {
          public StatePartHandler(IRepository&lt;StatePartRecord&gt; repository)
          {
              Filters.Add(StorageFilter.For(repository));
          }
      }
  }
  </code></pre>
  
  <p>Services/MyService.cs :</p>
  
  <pre><code>namespace Address.Services
  {
      public class AddressService : IAddressService
      {
      ...
       public void InsertState(Models.StatePartRecord state)
       {
           _stateRepository.Create(state);
       }
      ...
  }
  </code></pre>
  
  <p>now in written service class for my module, when i try to create an item and save it in database it trows an exeption:</p>
  
  <pre><code>attempted to assign id from null one-to-one property: ContentItemRecord
  </code></pre>
  
  <p><strong>Note</strong> that <code>_stateRepository</code> is a <code>IRepository&lt;StatePartRecord&gt;</code> typed injected object.</p>
  
  <p>what is wong?</p>