New table in database

8 years ago
I have a problem. I must add new table to database, this table is related with Order.
Something like as table Notes.
Where I must add new classes ?
Please help me :)
8 years ago
Before adding “stuff” into nopCommerce, a good understanding of “How things work” is a must!

As a recent user of nop, I’ve discovered that the following approach works best for me. Perhaps there is a better way or even another way but for now…this should help you out!

If I understand correctly, you need a new TABLE that will have a foreign key to the Order TABLE.

Assuming this is correct, let’s call our new TABLE “Gazou”

In Nop.Core:
-Create a new folder called “Gazou” under the existing “Domain” folder.
-In that newly created folder, create a class called “Gazou.cs”. Look at other classes inside the Domain folder to see how they work…how they have properties and how they implement BaseEntity etc…
-Once you’re done, build the Nop.Core only project to make sure everything works.

In Nop.Data:
-Create a new folder called “Gazou” under the existing “Mapping” folder.
-In that newly created folder, create a new class called “GazouMap.cs”. It is in that class that you’ll define all the fields and foreign key your new TABLE Gazou will have. Look at other mapping classes to see how they work, for example, how to create a foreign key, how it implements EntityTypeConfiguration<> etc…
-Once you’re done, build the Nop.Data only project to make sure everything works.

In Nop.Service:
-If your functionality requires you to create a new non-existing service, then do the following:
-Create a new folder “Gazou” and inside that folder, create a new “GazouService.cs” class. Look around existing services to see what is needed, what they implement etc…
-If your functionality relies on existing services but you only need to “add” methods, then find the appropriate service and “add” your new method there. Note that if you do so, you’ll be modifying the existing core and you may (or may not) have to deal with merges if and when you decide to upgrade nopCommerce to another version. This is a trade-off you’ll have to make.

--------------

That’s basically how we do it.
One important thing you’ll need to understand is that if you press F5 in Visual Studio, although this is code-first, your new TABLE will not, I repeat, will not be created. In order to do so, you have two options:

1- Either you manually create your new Gazou TABLE and then press F5 in VS.
or
2- Delete your existing database and re-launch your installation so that it creates the database again, only this time, it will automatically create your Gazou TABLE.

As for the UI, well…considering I have no idea what your plan is, I really don’t know.
In addition, please remember that what I’m writing here is if and only if you don’t mind modifying some core code. If you do not want to touch the core files, you may have to extend it.

In the end, nopCommerce is not meant to be upgrade proof but upgrade resistant so sooner or later, you might have to modify core files.

Hope this helps!
8 years ago
Thanks :)
7 years ago
This was very helpful for me and well written. Thanks!

Vlince wrote:
Before adding “stuff” into nopCommerce, a good understanding of “How things work” is a must!

As a recent user of nop, I’ve discovered that the following approach works best for me. Perhaps there is a better way or even another way but for now…this should help you out!

If I understand correctly, you need a new TABLE that will have a foreign key to the Order TABLE.

Assuming this is correct, let’s call our new TABLE “Gazou”

In Nop.Core:
-Create a new folder called “Gazou” under the existing “Domain” folder.
-In that newly created folder, create a class called “Gazou.cs”. Look at other classes inside the Domain folder to see how they work…how they have properties and how they implement BaseEntity etc…
-Once you’re done, build the Nop.Core only project to make sure everything works.

In Nop.Data:
-Create a new folder called “Gazou” under the existing “Mapping” folder.
-In that newly created folder, create a new class called “GazouMap.cs”. It is in that class that you’ll define all the fields and foreign key your new TABLE Gazou will have. Look at other mapping classes to see how they work, for example, how to create a foreign key, how it implements EntityTypeConfiguration<> etc…
-Once you’re done, build the Nop.Data only project to make sure everything works.

In Nop.Service:
-If your functionality requires you to create a new non-existing service, then do the following:
-Create a new folder “Gazou” and inside that folder, create a new “GazouService.cs” class. Look around existing services to see what is needed, what they implement etc…
-If your functionality relies on existing services but you only need to “add” methods, then find the appropriate service and “add” your new method there. Note that if you do so, you’ll be modifying the existing core and you may (or may not) have to deal with merges if and when you decide to upgrade nopCommerce to another version. This is a trade-off you’ll have to make.

--------------

That’s basically how we do it.
One important thing you’ll need to understand is that if you press F5 in Visual Studio, although this is code-first, your new TABLE will not, I repeat, will not be created. In order to do so, you have two options:

1- Either you manually create your new Gazou TABLE and then press F5 in VS.
or
2- Delete your existing database and re-launch your installation so that it creates the database again, only this time, it will automatically create your Gazou TABLE.

As for the UI, well…considering I have no idea what your plan is, I really don’t know.
In addition, please remember that what I’m writing here is if and only if you don’t mind modifying some core code. If you do not want to touch the core files, you may have to extend it.

In the end, nopCommerce is not meant to be upgrade proof but upgrade resistant so sooner or later, you might have to modify core files.

Hope this helps!
7 years ago
Thanks Vlince,

That is very well written and has also helped me out a lot. Specially regarding Mapping!

Cheers,

Mike.
7 years ago
Hello Vlince and All,

I have followed all steps mentioned by you. But i got weird error. I have created table called "Prescription" and mapp accordingly. Script through i have created table. I got  below error (some part of exceptions).

System.Data.EntityCommandExecutionException: An error occurred while executing the command definition. See the inner exception for details. ---> System.Data.SqlClient.SqlException: Invalid object name 'dbo.Prescriptions'.     at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)     at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`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& 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)     at


If you notice "Invalid object name 'dbo.Prescriptions'"  it simply says some where in code or from entity framework called like this. Although i have search thorough site and database, no table name mentioned like  'dbo.Prescriptions' or 'Prescriptions' (Double checked).  I could not figure out how come this table name from ? Database having name with "Prescription" only. Even PrescriptionMap.cs having same table name.

The real issue is still to come ,When i have prepared build and uploaded "bin" folder to my staging server it works all fine but suddenly after some time like 24 hours or less that error mentioned above is comes up whenever i tries to access that page which select data from "Prescription" table. I have double checked this issue (client  had confirmed this). It is working today after upload and not working next day.

Even when i am testing on staging that is not working with that database , same i have connected with my current development code and debug in VS shockingly all is working fine :(

Please please guide me on this issue or have any clue what is happening regarding this or any suggestion about same. I am stuck at this point what to do?


Thanks
Jay
6 years ago
Vlince wrote:
Before adding “stuff” into nopCommerce, a good understanding of “How things work” is a must!


Excellent tutorial Vlince. Thanks

Do I need to put anything in the Mapping file?

I understand the "this.HasKey" for the Primary Key, but the Table I'm using has a two-field Primary Key :
 
       public MemberEventMap()
        {
            this.ToTable("MemberEvent");

            this.HasKey(pc => pc.?????);
            ...........

Here is the Table :

CREATE TABLE [MemberEvent](  [MemID] [int]   [EventID] [int]    [Active] [bit] ,

CONSTRAINT [PK_MemberEvent] PRIMARY KEY CLUSTERED
(
  [MemID] ASC,
  [EventID] ASC
)WITH  ON [PRIMARY]
) ON [PRIMARY]

ALTER TABLE [dbo].[MemberEvent]  WITH CHECK ADD  CONSTRAINT [FK_MemberEvent_Member] FOREIGN KEY([MemID])
REFERENCES [dbo].[Customer] ([Id])


Plus I have a foreign key going to the Customer table.
Not sure how to implement this.
6 years ago
Thank to all ,
But suppose I have lots of data into database and all data required , Now i want to add new table into database ,
I have created all the required Class Like below steps

1. Create The Entity Class (e.g Entity.cs)

    Path : Nop/Core/Domain/Entity.cs

2. Create The Mapping Class (e.g EntityMap.cs)

    Path : Nop/Data/Mapping/EntityMap.cs

3. Create a Model for MVC (e.g EntityModel.cs)

    Path : Nop/Admin/Models/EntityModel.cs OR Nop/Web/Models/EntityModel.cs

4. Create a validator for model (e.g EntityValidator.cs)

    Path : Nop/Admin/Validators/EntityValidator.cs OR Nop/Web/Validators/EntityValidator.cs

5. Create A Mapping Configuration On AutoMapperStartupTask.cs for Entity and Model

    Path : Nop/Admin/Infrastructure OR Nop/Web/Infrastructure

6. Apply Mapping between Model and Entity on MappingExtensions.cs

    Path : Nop/Admin OR Nop/Web

7. Create a service class and service interface (e.g EntityService.cs , IEntityService.cs)

    Path : Nop/Services/EntityService.cs AND Nop/Services/IEntityService.cs

8. Finally Create Controller and View for given model

But i am not able to create table automatically so i create table manually...
does any other way to create table automatically ? Because i am using nopCommerce 3.40 . it used code first , and concept of code first is automatically create table.. So please help me what i do here ?
4 years ago
Is there a way to do this from a plugin?
4 years ago
As seen here?

http://docs.nopcommerce.com/display/nc/Plugin+with+data+access