title: orchard IRepository.Create error
body: <p>I'm working on
  <a href="http://www.ideliverable.com/blog/writing-an-orchard-webshop-module-from-scratch-part-9" rel="nofollow noreferrer">http://www.ideliverable.com/blog/writing-an-orchard-webshop-module-from-scratch-part-9</a> </p>
  
  <p>my orchard version is 1.9</p>
  
  <p>OrderRecord.cs</p>
  
  <pre><code>public class OrderRecord
      {
          public virtual int Id { get; set; }
          public virtual int CustomerId { get; set; }
          public virtual DateTime CreatedAt { get; set; }
          public virtual decimal SubTotal { get; set; }
          public virtual decimal Vat { get; set; }
          public virtual OrderStatus Status { get; set; }
          public virtual IList&lt;OrderDetailRecord&gt; Details { get; private set; }
          public virtual string PaymentServiceProviderResponse { get; set; }
          public virtual string PaymentReference { get; set; }
          public virtual DateTime? PaidAt { get; set; }
          public virtual DateTime? CompletedAt { get; set; }
          public virtual DateTime? CancelledAt { get; set; }
  
          public virtual decimal Total
          {
              get { return SubTotal + Vat; }
              private set { }
          }
  
          public virtual string Number
          {
              get { return (Id + 1000).ToString(CultureInfo.InvariantCulture); }
              private set { }
          }
  
          public OrderRecord()
          {
              Details = new List&lt;OrderDetailRecord&gt;();
          }
  
          public virtual void UpdateTotals()
          {
              var subTotal = 0m;
              var vat = 0m;
  
              foreach (var detail in Details)
              {
                  subTotal += detail.SubTotal;
                  vat += detail.Vat;
              }
  
              SubTotal = subTotal;
              Vat = vat;
          } 
  </code></pre>
  
  <p>No additional content part class because i need to use id and methods are defined in the same class. Field setters that not necessary to create table are defined as private. <code>Total</code> and <code>Number</code></p>
  
  <p>Migration.cs</p>
  
  <pre><code> SchemaBuilder.CreateTable("OrderRecord", t =&gt; t
                      .Column&lt;int&gt;("Id", c =&gt; c.PrimaryKey().Identity())
                      .Column&lt;int&gt;("CustomerId", c =&gt; c.NotNull())
                      .Column&lt;DateTime&gt;("CreatedAt", c =&gt; c.NotNull())
                      .Column&lt;decimal&gt;("SubTotal", c =&gt; c.NotNull())
                      .Column&lt;decimal&gt;("Vat", c =&gt; c.NotNull())
                      .Column&lt;string&gt;("Status", c =&gt; c.WithLength(50).NotNull())
                      .Column&lt;string&gt;("PaymentServiceProviderResponse", c =&gt; c.WithLength(null))
                      .Column&lt;string&gt;("PaymentReference", c =&gt; c.WithLength(50))
                      .Column&lt;DateTime&gt;("PaidAt", c =&gt; c.Nullable())
                      .Column&lt;DateTime&gt;("CompletedAt", c =&gt; c.Nullable())
                      .Column&lt;DateTime&gt;("CancelledAt", c =&gt; c.Nullable())
  
                      );
  </code></pre>
  
  <p>Table created correctly with these fields but when i want to create an OrderRecord in OrderService.cs with IRepository.Create (T Entity) method, it sends fields not binded to the table. <code>Total</code> and <code>Number</code>.</p>
  
  <p>OrderService.cs</p>
  
  <pre><code>public OrderRecord CreateOrder(int customerId, IEnumerable&lt;ShoppingCartItem&gt; items)
  {
  
      if (items == null)
          throw new ArgumentNullException("items");
  
      // Convert to an array to avoid re-running the enumerable
      var itemsArray = items.ToArray();
  
      if (!itemsArray.Any())
          throw new ArgumentException("Creating an order with 0 items is not supported", "items");
  
      var order = new OrderRecord
      {
          CreatedAt = _dateTimeService.Now,
          CustomerId = customerId,
          Status = OrderStatus.New
      };
  
      _orderRepository.Create(order);
  </code></pre>
  
  <p>Last line throws error 
  here is error log:</p>
  
  <blockquote>
    <p>2018-04-20 18:37:15,871 [16] Orchard.Exceptions.DefaultExceptionPolicy
    - Default - An unexpected exception was caught [<a href="http://localhost:64607/Orchard.WebShop/Order/Create]" rel="nofollow noreferrer">http://localhost:64607/Orchard.WebShop/Order/Create]</a>
    NHibernate.Exceptions.GenericADOException: could not insert:
    [Orchard.Webshop.Models.OrderRecord][SQL: INSERT INTO
    Orchard_Webshop_OrderRecord (CustomerId, CreatedAt, SubTotal, Vat,
    Status, PaymentServiceProviderResponse, PaymentReference, PaidAt,
    CompletedAt, CancelledAt, Total, Number) VALUES (?, ?, ?, ?, ?, ?, ?,
    ?, ?, ?, ?, ?); select SCOPE_IDENTITY()] --->
    System.Data.SqlClient.SqlException: Invalid column name 'Total'.
    Invalid column name 'Number'. Statement(s) could not be prepared.<br>
    at System.Data.SqlClient.SqlConnection.OnError(SqlException exception,
    Boolean breakConnection, Action<code>1 wrapCloseInAction)    at
    System.Data.SqlClient.SqlInternalConnection.OnError(SqlException
    exception, Boolean breakConnection, Action</code>1 wrapCloseInAction)    at
    System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject
    stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)    at
    System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior,
    SqlCommand cmdHandler, SqlDataReader dataStream,
    BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject
    stateObj, Boolean&amp; dataReady)    at
    System.Data.SqlClient.SqlDataReader.TryConsumeMetaData()    at
    System.Data.SqlClient.SqlDataReader.get_MetaData()    at
    System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds,
    RunBehavior runBehavior, String resetOptionsString, Boolean
    isInternal, Boolean forDescribeParameterEncryption)    at
    System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior
    cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean
    async, Int32 timeout, Task&amp; task, Boolean asyncWrite, Boolean inRetry,
    SqlDataReader ds, Boolean describeParameterEncryptionRequest)    at
    System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior
    cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String
    method, TaskCompletionSource<code>1 completion, Int32 timeout, Task&amp; task,
    Boolean&amp; usedCache, Boolean asyncWrite, Boolean inRetry)    at
    System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior
    cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String
    method)    at
    System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior
    behavior, String method)    at
    System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior
    behavior)    at
    System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader()<br>
    at NHibernate.AdoNet.AbstractBatcher.ExecuteReader(IDbCommand cmd)<br>
    at
    NHibernate.Id.IdentityGenerator.InsertSelectDelegate.ExecuteAndExtract(IDbCommand
    insert, ISessionImplementor session)    at
    NHibernate.Id.Insert.AbstractReturningDelegate.PerformInsert(SqlCommandInfo
    insertSQL, ISessionImplementor session, IBinder binder)    --- End of
    inner exception stack trace ---    at
    NHibernate.Id.Insert.AbstractReturningDelegate.PerformInsert(SqlCommandInfo
    insertSQL, ISessionImplementor session, IBinder binder)    at
    NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object[]
    fields, Boolean[] notNull, SqlCommandInfo sql, Object obj,
    ISessionImplementor session)    at
    NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object[]
    fields, Object obj, ISessionImplementor session)    at
    NHibernate.Action.EntityIdentityInsertAction.Execute()    at
    NHibernate.Engine.ActionQueue.Execute(IExecutable executable)    at
    NHibernate.Event.Default.AbstractSaveEventListener.PerformSaveOrReplicate(Object
    entity, EntityKey key, IEntityPersister persister, Boolean
    useIdentityColumn, Object anything, IEventSource source, Boolean
    requiresImmediateIdAccess)    at
    NHibernate.Event.Default.AbstractSaveEventListener.PerformSave(Object
    entity, Object id, IEntityPersister persister, Boolean
    useIdentityColumn, Object anything, IEventSource source, Boolean
    requiresImmediateIdAccess)    at
    NHibernate.Event.Default.AbstractSaveEventListener.SaveWithGeneratedId(Object
    entity, String entityName, Object anything, IEventSource source,
    Boolean requiresImmediateIdAccess)    at
    NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.SaveWithGeneratedOrRequestedId(SaveOrUpdateEvent
    event)    at
    NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.EntityIsTransient(SaveOrUpdateEvent
    event)    at
    NHibernate.Event.Default.DefaultSaveEventListener.PerformSaveOrUpdate(SaveOrUpdateEvent
    event)    at
    NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.OnSaveOrUpdate(SaveOrUpdateEvent
    event)    at NHibernate.Impl.SessionImpl.FireSave(SaveOrUpdateEvent
    event)    at NHibernate.Impl.SessionImpl.Save(Object obj)    at
    Orchard.Data.Repository</code>1.Create(T entity) in
    C:\Users\Selman\Orchard-dev\src\Orchard\Data\Repository.cs:line 93<br>
    at Orchard.Data.Repository<code>1.Orchard.Data.IRepository&lt;T&gt;.Create(T
    entity) in
    C:\Users\Selman\Orchard-dev\src\Orchard\Data\Repository.cs:line 33<br>
    at Orchard.Webshop.Services.OrderService.CreateOrder(Int32 customerId,
    IEnumerable</code>1 items) in
    C:\Users\Selman\Orchard-dev\src\Orchard.Web\Modules\Orchard.Webshop\Services\OrderService.cs:line
    46    at Orchard.Webshop.Controllers.OrderController.Create() in
    C:\Users\Selman\Orchard-dev\src\Orchard.Web\Modules\Orchard.Webshop\Controllers\OrderController.cs:line
    48    at lambda_method(Closure , ControllerBase , Object[] )    at
    System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase
    controller, Object[] parameters)    at
    System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext
    controllerContext, IDictionary<code>2 parameters)    at
    System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext
    controllerContext, ActionDescriptor actionDescriptor, IDictionary</code>2
    parameters)    at
    System.Web.Mvc.Async.AsyncControllerActionInvoker.b__39(IAsyncResult
    asyncResult, ActionInvocation innerInvokeState)    at
    System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult<code>2.CallEndDelegate(IAsyncResult
    asyncResult)    at
    System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase</code>1.End()
    at
    System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult
    asyncResult)    at
    System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.b__3d()
    at
    System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.&lt;>c__DisplayClass46.b__3f()
    at
    System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.&lt;>c__DisplayClass46.b__3f()
    at
    System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.&lt;>c__DisplayClass46.b__3f()</p>
  </blockquote>
  
  <p><strong>Solution</strong></p>
  
  <p>I found a solution temporarily. I turned <code>Total</code> and <code>Number</code> fields into to methods <code>public virtual decimal Total()</code> and <code>public virtual string Number()</code>. It works right now.</p>