Extend Nopcommerce Db with new tables

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
11 年 前
Hi all,

i have created a plugin that contains the following class

namespace Nop.Plugins.Misc.PrenotazioneServizi.Domain
{

    public partial class Prenotazione : BaseEntity
    {

        private ICollection<Prenotazioni_Risorse> _risorsePrenotazione;

        public virtual int IdOrdine { get; set; }
        public virtual int IdServizio { get; set; }
        public virtual Servizio Servizio { get; set; }
        public virtual DateTime GiornoOraDa { get; set; }
        public virtual DateTime GiornoOraA { get; set; }
        public virtual DateTime CreatedOnUtc { get; set; }


        public virtual Order Ordine { get; set; }

        public virtual Customer Cliente { get; set; }

        public virtual ICollection<Prenotazioni_Risorse> Risorse
        {
            get { return _risorsePrenotazione ?? (_risorsePrenotazione = new List<Prenotazioni_Risorse>()); }
            set { _risorsePrenotazione = value; }
        }
    }

}

On modelcreating of objectcontext I receive the error: Table Address already exists.

This is my objectcontext:

public class PrenotazioneServiziObjectContext : DbContext, IDbContext
    {
        public PrenotazioneServiziObjectContext(string nameOrConnectionString)
            : base(nameOrConnectionString)
        {
            //((IObjectContextAdapter) this).ObjectContext.ContextOptions.LazyLoadingEnabled = true;
        }


        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Configurations.Add(new TipoRisorsaMap());
            modelBuilder.Configurations.Add(new ServizioMap());
            modelBuilder.Configurations.Add(new Servizi_IntervalliMap());
            modelBuilder.Configurations.Add(new RisorsaMap());
            modelBuilder.Configurations.Add(new PrenotazioneMap());
            modelBuilder.Configurations.Add(new Servizi_RisorseMap());
            modelBuilder.Configurations.Add(new Prenotazioni_RisorseMap());

            modelBuilder.Entity<RewardPointsHistory>().HasRequired(p => p.UsedWithOrder).WithOptional();
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
            //modelBuilder.Entity<Prenotazione>().Ignore(c => c.Ordine);
            //modelBuilder.Entity<Prenotazione>().Ignore(c => c.Cliente);

            base.OnModelCreating(modelBuilder);
        }


        public string CreateDatabaseScript()
        {
            return ((IObjectContextAdapter)this).ObjectContext.CreateDatabaseScript();
        }

        public new IDbSet<TEntity> Set<TEntity>() where TEntity : BaseEntity
        {
            return base.Set<TEntity>();
        }

        /// <summary>
        /// Install
        /// </summary>
        public void Install()
        {
            //It's required to set initializer to null (for SQL Server Compact).
            //otherwise, you'll get something like "The model backing the 'your context name' context has changed since the database was created. Consider using Code First Migrations to update the database"
            Database.SetInitializer<PrenotazioneServiziObjectContext>(null);

            //create the table
            var dbScript = CreateDatabaseScript();
            Database.ExecuteSqlCommand(dbScript);
            SaveChanges();
        }

        /// <summary>
        /// Uninstall
        /// </summary>
        public void Uninstall()
        {
            //drop the table
            try
            {
                string dbQuery;


                dbQuery = "DROP TABLE PWeb_Servizi_Intervalli";
                Database.ExecuteSqlCommand(dbQuery);

                dbQuery = "DROP TABLE PWeb_Servizi_Risorse";
                Database.ExecuteSqlCommand(dbQuery);

                dbQuery = "DROP TABLE PWeb_Prenotazioni_Risorse";
                Database.ExecuteSqlCommand(dbQuery);

                dbQuery = "DROP TABLE PWeb_Risorse";
                Database.ExecuteSqlCommand(dbQuery);

                dbQuery = "DROP TABLE PWeb_Prenotazioni";
                Database.ExecuteSqlCommand(dbQuery);

                dbQuery = "DROP TABLE PWeb_Servizi";
                Database.ExecuteSqlCommand(dbQuery);

                dbQuery = "DROP TABLE PWeb_TipiRisorse";
                Database.ExecuteSqlCommand(dbQuery);


                SaveChanges();
            }
            catch
            {
            }
        }


        public IList<TEntity> ExecuteStoredProcedureList<TEntity>(string commandText, params object[] parameters) where TEntity : BaseEntity, new()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Creates a raw SQL query that will return elements of the given generic type.  The type can be any type that has properties that match the names of the columns returned from the query, or can be a simple primitive type. The type does not have to be an entity type. The results of this query are never tracked by the context even if the type of object returned is an entity type.
        /// </summary>
        /// <typeparam name="TElement">The type of object returned by the query.</typeparam>
        /// <param name="sql">The SQL query string.</param>
        /// <param name="parameters">The parameters to apply to the SQL query string.</param>
        /// <returns>Result</returns>
        public IEnumerable<TElement> SqlQuery<TElement>(string sql, params object[] parameters)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Executes the given DDL/DML command against the database.
        /// </summary>
        /// <param name="sql">The command string</param>
        /// <param name="timeout">Timeout value, in seconds. A null value indicates that the default value of the underlying provider will be used</param>
        /// <param name="parameters">The parameters to apply to the command string.</param>
        /// <returns>The result returned by the database after executing the command.</returns>
        public int ExecuteSqlCommand(string sql, int? timeout = null, params object[] parameters)
        {
            throw new NotImplementedException();
        }
    }
}

I need to join my new tables with nopcommerce order and customer table. How can I do this?

Please help me.
Thanks
11 年 前
koreano wrote:
Hi all,

i have created a plugin that contains the following class

namespace Nop.Plugins.Misc.PrenotazioneServizi.Domain
{

    public partial class Prenotazione : BaseEntity
    {

        private ICollection<Prenotazioni_Risorse> _risorsePrenotazione;

        public virtual int IdOrdine { get; set; }
        public virtual int IdServizio { get; set; }
        public virtual Servizio Servizio { get; set; }
        public virtual DateTime GiornoOraDa { get; set; }
        public virtual DateTime GiornoOraA { get; set; }
        public virtual DateTime CreatedOnUtc { get; set; }


        public virtual Order Ordine { get; set; }

        public virtual Customer Cliente { get; set; }

        public virtual ICollection<Prenotazioni_Risorse> Risorse
        {
            get { return _risorsePrenotazione ?? (_risorsePrenotazione = new List<Prenotazioni_Risorse>()); }
            set { _risorsePrenotazione = value; }
        }
    }

}

On modelcreating of objectcontext I receive the error: Table Address already exists.

This is my objectcontext:

public class PrenotazioneServiziObjectContext : DbContext, IDbContext
    {
        public PrenotazioneServiziObjectContext(string nameOrConnectionString)
            : base(nameOrConnectionString)
        {
            //((IObjectContextAdapter) this).ObjectContext.ContextOptions.LazyLoadingEnabled = true;
        }


        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Configurations.Add(new TipoRisorsaMap());
            modelBuilder.Configurations.Add(new ServizioMap());
            modelBuilder.Configurations.Add(new Servizi_IntervalliMap());
            modelBuilder.Configurations.Add(new RisorsaMap());
            modelBuilder.Configurations.Add(new PrenotazioneMap());
            modelBuilder.Configurations.Add(new Servizi_RisorseMap());
            modelBuilder.Configurations.Add(new Prenotazioni_RisorseMap());

            modelBuilder.Entity<RewardPointsHistory>().HasRequired(p => p.UsedWithOrder).WithOptional();
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
            //modelBuilder.Entity<Prenotazione>().Ignore(c => c.Ordine);
            //modelBuilder.Entity<Prenotazione>().Ignore(c => c.Cliente);

            base.OnModelCreating(modelBuilder);
        }


        public string CreateDatabaseScript()
        {
            return ((IObjectContextAdapter)this).ObjectContext.CreateDatabaseScript();
        }

        public new IDbSet<TEntity> Set<TEntity>() where TEntity : BaseEntity
        {
            return base.Set<TEntity>();
        }

        /// <summary>
        /// Install
        /// </summary>
        public void Install()
        {
            //It's required to set initializer to null (for SQL Server Compact).
            //otherwise, you'll get something like "The model backing the 'your context name' context has changed since the database was created. Consider using Code First Migrations to update the database"
            Database.SetInitializer<PrenotazioneServiziObjectContext>(null);

            //create the table
            var dbScript = CreateDatabaseScript();
            Database.ExecuteSqlCommand(dbScript);
            SaveChanges();
        }

        /// <summary>
        /// Uninstall
        /// </summary>
        public void Uninstall()
        {
            //drop the table
            try
            {
                string dbQuery;


                dbQuery = "DROP TABLE PWeb_Servizi_Intervalli";
                Database.ExecuteSqlCommand(dbQuery);

                dbQuery = "DROP TABLE PWeb_Servizi_Risorse";
                Database.ExecuteSqlCommand(dbQuery);

                dbQuery = "DROP TABLE PWeb_Prenotazioni_Risorse";
                Database.ExecuteSqlCommand(dbQuery);

                dbQuery = "DROP TABLE PWeb_Risorse";
                Database.ExecuteSqlCommand(dbQuery);

                dbQuery = "DROP TABLE PWeb_Prenotazioni";
                Database.ExecuteSqlCommand(dbQuery);

                dbQuery = "DROP TABLE PWeb_Servizi";
                Database.ExecuteSqlCommand(dbQuery);

                dbQuery = "DROP TABLE PWeb_TipiRisorse";
                Database.ExecuteSqlCommand(dbQuery);


                SaveChanges();
            }
            catch
            {
            }
        }


        public IList<TEntity> ExecuteStoredProcedureList<TEntity>(string commandText, params object[] parameters) where TEntity : BaseEntity, new()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Creates a raw SQL query that will return elements of the given generic type.  The type can be any type that has properties that match the names of the columns returned from the query, or can be a simple primitive type. The type does not have to be an entity type. The results of this query are never tracked by the context even if the type of object returned is an entity type.
        /// </summary>
        /// <typeparam name="TElement">The type of object returned by the query.</typeparam>
        /// <param name="sql">The SQL query string.</param>
        /// <param name="parameters">The parameters to apply to the SQL query string.</param>
        /// <returns>Result</returns>
        public IEnumerable<TElement> SqlQuery<TElement>(string sql, params object[] parameters)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Executes the given DDL/DML command against the database.
        /// </summary>
        /// <param name="sql">The command string</param>
        /// <param name="timeout">Timeout value, in seconds. A null value indicates that the default value of the underlying provider will be used</param>
        /// <param name="parameters">The parameters to apply to the command string.</param>
        /// <returns>The result returned by the database after executing the command.</returns>
        public int ExecuteSqlCommand(string sql, int? timeout = null, params object[] parameters)
        {
            throw new NotImplementedException();
        }
    }
}

I need to join my new tables with nopcommerce order and customer table. How can I do this?

Please help me.
Thanks


Basically you can't because you are using 2 DbContext. :D
11 年 前
How can I use the same DbContext of Nopcommerce?
This is a strong limitation.

Is there a workaround?
11 年 前
koreano wrote:
How can I use the same DbContext of Nopcommerce?
This is a strong limitation.

Is there a workaround?


Not an expert with EF, perhaps some other people can help? :D

But logically I don't think you can use the same DbContext without modifying the core. :D
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.