Updating an existing entity. How to add a new property

10 months ago
https://docs.nopcommerce.com/en/developer/tutorials/update-existing-entity.html

Hi I'm reproducing the steps from the documentation. I successfully added a new property the other day but I got this error today.
Can somebody help? below is the error displayed.

SqlException: Invalid column name 'SomeNewProperty2'.
Microsoft.Data.SqlClient.SqlCommand+<>c.<ExecuteDbDataReaderAsync>b__208_0(Task<SqlDataReader> result)
System.Threading.Tasks.ContinuationResultTaskFromResultTask<TAntecedentResult, TResult>.InnerInvoke()
System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, object state)
System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, object state)
System.Threading.Tasks.Task.ExecuteWithThreadLocal(ref Task currentTaskSlot, Thread threadPoolThread)
LinqToDB.Data.DataConnection.ExecuteReaderAsync(CommandBehavior commandBehavior, CancellationToken cancellationToken)
LinqToDB.Data.DataConnection.ExecuteDataReaderAsync(CommandBehavior commandBehavior, CancellationToken cancellationToken)
LinqToDB.Data.DataConnection.ExecuteDataReaderAsync(CommandBehavior commandBehavior, CancellationToken cancellationToken)
LinqToDB.Data.DataConnection+QueryRunner.ExecuteReaderAsync(CancellationToken cancellationToken)
LinqToDB.Linq.QueryRunner.ExecuteQueryAsync<T>(Query query, IDataContext dataContext, Mapper<T> mapper, Expression expression, object[] ps, object[] preambles, int queryNumber, Func<T, bool> func, TakeSkipDelegate skipAction, TakeSkipDelegate takeAction, CancellationToken cancellationToken)
LinqToDB.Linq.QueryRunner.ExecuteQueryAsync<T>(Query query, IDataContext dataContext, Mapper<T> mapper, Expression expression, object[] ps, object[] preambles, int queryNumber, Func<T, bool> func, TakeSkipDelegate skipAction, TakeSkipDelegate takeAction, CancellationToken cancellationToken)
LinqToDB.Linq.ExpressionQuery<T>.GetForEachAsync(Action<T> action, CancellationToken cancellationToken)
LinqToDB.AsyncExtensions.ToListAsync<TSource>(IQueryable<TSource> source, CancellationToken token)
Nop.Data.EntityRepository<TEntity>+<>c__DisplayClass11_0+<<GetAllAsync>g__getAllAsync|0>d.MoveNext() in EntityRepository.cs
+
                return await query.ToListAsync();
Nop.Core.Caching.MemoryCacheManager.GetAsync<T>(CacheKey key, Func<Task<T>> acquire) in MemoryCacheManager.cs
+
            result = await acquire();
Nop.Data.EntityRepository<TEntity>.GetEntitiesAsync(Func<Task<IList<TEntity>>> getAllAsync, Func<IStaticCacheManager, CacheKey> getCacheKey) in EntityRepository.cs
10 months ago
What version of nopCommerce?
10 months ago
New York wrote:
What version of nopCommerce?

4.6 the latest.
10 months ago
So you made changes and the migration updated the table
Then today you get an error - did you make an changes in between ?
Have you checked the Database Table to see if the SomeNewProperty2 field is still in the table ?
10 months ago
Don't just copy the sample code exactly.  The migration date should use 'today's date", not:

[NopMigration("2022/01/01 12:00:00:2551770", ...

Then after you run the app, migrations should apply, so query the MigrationVersionInfo table to check it was.
10 months ago
Yidna wrote:
So you made changes and the migration updated the table
Then today you get an error - did you make an changes in between ?
Have you checked the Database Table to see if the SomeNewProperty2 field is still in the table ?


No, I didn't make any change. I tried add SomeNewProperty 2wks ago and it worked.
But adding SomeNewProperty2 was not successful. I don't see SomeNewProperty2 field in DB.

---------------------------------------------------------------------------------
Add the following property to the Category class.

public string SomeNewProperty { get; set; }
Add the new class Nop.Data.Migrations.AddSomeNewProperty with following code:

using FluentMigrator;
using Nop.Core.Domain.Catalog;

namespace Nop.Data.Migrations
{
    [NopMigration("2022/01/01 12:00:00:2551770", "Category. Add some new property", UpdateMigrationType.Data, MigrationProcessType.Update)]
    public class AddSomeNewProperty: AutoReversingMigration
    {
        /// <summary>Collect the UP migration expressions</summary>
        public override void Up()
        {
            Create.Column(nameof(Category.SomeNewProperty))
            .OnTable(nameof(Category))
            .AsString(255)
            .Nullable();
        }
    }
}
------------------------------------------------------------------------------------------

Technically, if I follow these two steps and run the program. I should able to see DB has added new field SomeNewProperty2 right? but I get an error when I run.
10 months ago
New York wrote:
Don't just copy the sample code exactly.  The migration date should use 'today's date", not:

[NopMigration("2022/01/01 12:00:00:2551770", ...

Then after you run the app, migrations should apply, so query the MigrationVersionInfo table to check it was.


Oh! it works! thank you :)