How to add new table in Nop 4.1. (Auto Migration or Manual)

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
5 years ago
I have to use City as drop down after state selection for this I have to create new table City. Can anyone please suggest what to do I have added new domain entity City in Core project and it's mapping in Data project and all place I have used now CityId instead of City as text input. After successful build no changes reflect in database.


I have also tried ef migration but getting this error.

PM> dotnet ef migrations add InitialCreate
Unable to create an object of type 'NopObjectContext'. Add an implementation of 'IDesignTimeDbContextFactory<NopObjectContext>' to the project, or see https://go.microsoft.com/fwlink/?linkid=851728 for additional patterns supported at design time.
PM> dotnet ef migrations add AddCity
Unable to create an object of type 'NopObjectContext'. Add an implementation of 'IDesignTimeDbContextFactory<NopObjectContext>' to the project, or see https://go.microsoft.com/fwlink/?linkid=851728 for additional patterns supported at design time.


We are doing customization for new eCommerce site using nopCommerce latest version 4.1.
5 years ago
Hello,

Follow vendor module.

you should follow core,data and objectcontext thoroughly.

in 4.1 team did changes in context file.

After delete installedplugin.json and datasetting.json file and run project.

If you want with plugin then you should follow Manual (Fixed or By Weight and By Total)  this plugin.

Hope you will understand..!!
5 years ago
Hello sk5202,

Thanks for your reply.

I have used all module's properly.

First thing is that my application is live and I do not want to create Database again on live basically re-installation.

My requirement is to generate and column or new table when I run the project or through EF Core Migration.

Both are not working.

Kind reply in that perceptive.

Thank you
Ankur
5 years ago
Hi

I got the same issue while updating the existing table in Nop 4.1

Did you get any solution for this? If yes please share.

Thanks in advance
5 years ago
u need to create the database table yourself.
use sql manager and create table script to create the table.
5 years ago
Hi

This is what I am getting when i tried to update the existing table in Nop Commerce 4.1 using code first approach

PM> Add-Migration InitialCreate

Unable to create an object of type 'NopObjectContext'. Add an implementation of 'IDesignTimeDbContextFactory<NopObjectContext>' to the project, or see https://go.microsoft.com/fwlink/?linkid=851728 for additional patterns supported at design time.
5 years ago
use ms sql management studio
use the create table script

u can always run the project installation locally,  it will create the new table, then copy the script for your server project
5 years ago
Hello Rejeesher,

Yes, I'm doing manually in SQL database changes. Accordingly i do change in Core & Data project.

As of now .Net Entity framework Core do not support auto migration in Code first.
Either you have to use do Migration each time or manage Manually. I prefer manually as i know what script i have to run on my production DB.

@Hezy has said it rightly that we need to do manually. :)

Kind Regards,
Ankur Shrivastava
5 years ago
Hi

If you change NopObjectContext file as below, then you can easily run the add-migration command. Consider if you already have a database and now you want to add migration for the first time, after running the add-migration command you must delete all the codes from Up and Down methods. These codes are generated automatically in the migration class because EF core doesn't support -IngoreChanges option now.


    public partial class NopObjectContext : DbContext, IDbContext, IDesignTimeDbContextFactory<NopObjectContext>
    {
        #region Ctor

        public NopObjectContext(DbContextOptions<NopObjectContext> options) : base(options)
        {

        }

        public NopObjectContext()
        {
        }

        public NopObjectContext CreateDbContext(string[] args)
        {
            var optionsBuilder = new DbContextOptionsBuilder<NopObjectContext>();

            dynamic dataSettings;
            using (StreamReader reader = new StreamReader(NopDataSettingsDefaults.FilePath.Replace("~/", string.Empty).TrimStart('/').Replace('/', '\\')))
            {
                string json = reader.ReadToEnd();
                dataSettings = JsonConvert.DeserializeObject(json);
            }

            optionsBuilder.UseSqlServer((string)dataSettings.DataConnectionString);

            return new NopObjectContext(optionsBuilder.Options);
        }

        #endregion


Best Regards
5 years ago
Hi,

I Actually have the code below. might be useful to others.

 
public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory<NopObjectContext>
    {
        public NopObjectContext CreateDbContext(string[] args)
        {
            IConfigurationRoot configuration = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile(@"App_Data\dataSettings.json")
                .Build();

            var builder = new DbContextOptionsBuilder<NopObjectContext>();


            var connectionString = configuration.GetSection("DataConnectionString").Value; //configuration.GetConnectionString("DataConnectionString");

            builder.UseSqlServer(connectionString);

            return new NopObjectContext(builder.Options);
        }
    }



Regards,
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.