Adding a new table or a field to existing table n NopCommerce 4.3

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
Il y a 3 ans
The customer attributes such as FirstName, LastName are stored in the dbo.GenericAttributes table under the KeyGroup = Customer

I think The CustomerAttributeValue table is more for the UI side, and contains the 'valid values' - e.g. the drop-down or radio-button items that the customer sees/selects when you configure a CustomerAttribute to be of those types.


asenol70 wrote:
When I look into original source code downloaded from nopcommerce version 4.3
There is a file NopCustomerDefaults.cs. In the file FirstName and LastName are defined as generic attributes.
But they are not custom attributes (they are not defined by person using nopcommerce admin panel), and also they are not fields (columns) of Customer table.
Then where on earth they are tied to database? Where do the following code get data from (a code line of payment plugin)?

var customerName = _genericAttributeService.GetAttribute<string>(customer, NopCustomerDefaults.FirstNameAttribute);



nopc43\Libraries\Nop.Core\Domain\Customers\NopCustomerDefaults.cs

    #region Customer attributes

        /// <summary>
        /// Gets a name of generic attribute to store the value of 'FirstName'
        /// </summary>
        public static string FirstNameAttribute => "FirstName";

        /// <summary>
        /// Gets a name of generic attribute to store the value of 'LastName'
        /// </summary>
        public static string LastNameAttribute => "LastName";

        /// <summary>
        /// Gets a name of generic attribute to store the value of 'Gender'
        /// </summary>
        public static string GenderAttribute => "Gender";



When I look at http://localhost:15536/Admin/Setting/CustomerUser in admin panel of nopcommerce, there is no custom attribute defined for customer.
Also in SSMS, no rows returned from the query below
SELECT [Id] ,[Name],[IsRequired] ,[AttributeControlTypeId] ,[DisplayOrder]
  FROM [nopcom].[dbo].[CustomerAttribute]

  
What is the purpose of  NopCustomerDefaults.cs file?
Il y a 3 ans
You can use

_genericAttributeService.GetAttribute<string>(customer, NopCustomerDefaults.CustomCustomerAttributes);  
Il y a 3 ans
Could any one let me know how to add new table in existing plugin without touching existing table?
Il y a 1 an
asenol70 wrote:

It would be better if it were done by code-first and migration approach.


Hi asenol70, I have the same query. I am using nopCommerce 4.50.3 and I also want to add some new field into the table like Category, Product etc. using migration without touching the database so that the fields are automatically create to the existing database and respective tables. I have tried the below code.

var categoryTableName = NameCompatibilityManager.GetTableName(typeof(Category));

            if (!Schema.Table(categoryTableName).Column(nameof(Category.MyProperty)).Exists())
                Alter.Table(categoryTableName)
                    .AddColumn(nameof(Category.MyProperty)).AsAnsiString();


But I am getting an sql inner exception : invalid column name 'MyProperty' when running the project using existing database. But if I create a new database by Database installation then the columns are created perfectly. By I don't want to create a new Database.

How can I achieve that? or do I need to add the column manually to the table within the existing database? It seems like there are no other options left.
Il y a 1 an
abhijit22 wrote:
invalid column name 'MyProperty'

Probably just means the Column is not created

Did you need to set a default value or make the field nullable

    var categoryTableName = NameCompatibilityManager.GetTableName(typeof(Category));
    if (!Schema.Table(categoryTableName).Column(nameof(Category.MyProperty)).Exists())
        Alter.Table(categoryTableName)
            .AddColumn(nameof(Category.MyProperty)).AsAnsiString().Nullable();


Also Google  search  has AsAnsiString(Int32, String) so maybe it needs another parameter
See https://fluentmigrator.github.io/api/v3.x/FluentMigrator.Builders.IColumnTypeSyntax-1.html#FluentMigrator_Builders_IColumnTypeSyntax_1_AsAnsiString_System_Int32_System_String_
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.