Use the Entity Framework (EF) code in quick custom NopCommerce for priority migration, nopperformanceef

Source: Internet
Author: User
Tags nopcommerce

Use the Entity Framework (EF) code in quick custom NopCommerce for priority migration, nopperformanceef

I have seen many users in the nopCommerce forum asking them how to use Entity Framework (EF) code to migrate the code first to customize nopCommerce and add new fields and entites cores. In fact, I used a lot of EF Migrations in the nopCommerce custom project. I must say it is very helpful in development.

Today, I will share with you how to do this in the nopCommerce project! I will use nopCommerce 3.20 as an example, but you can easily apply this concept to other vesions!

Link: http://www.nopcn.com/nopcommerce-blog-ef-migrations-88.html

Set EF migration in nopCommerce Solution

The first thing you need to do is enable migration in the nopCommerce solution. So start nopCommerce in Visual Studio, look at the Nop. Web project, and open Web. config. You need to add a connection string to the development database. Note that adding a connection string to Web. config does not affect the working principle of nopCommerce, because nopCommerce does not search for the connection string in Web. config.

Then, enable NopObjectContext. cs in Nop. Data. Add a new constructor pointing to the name of the connection string just added in the previous step.Note: replace Pro-nopCommerce with the name of the connection string.

The next step is to enable migration in the project. If you have not done so, open the Package Manager Console. In the "Default project" drop-down list, select Nop. Data as the project. Make sure that Nop. Web is selected as the startup project. Enter the "enable-migrations" command in the Package Manager Console and press Enter! Visual Studio generates a file named "deployments. cs". You can safely ignore it, but you need to keep it.

The last step of EF migration settings is to enter the command "add-migration InitialMigration-IgnoreChanges" in the Package Manager Console ". InitialMigration is the name you want to give the current migration. The IgnoreChanges handle tells EF Migrations that you want to ignore the current database: this means that you want EF to ignore all existing tables so that no script is generated for the existing tables.

Therefore, you will see the new. cs file generated by Visual Studio that corresponds to the Migration (InitialMigration) You just added. If you view the file, it is essentially a blank file, because we used the-IgnoreChanges handle in the previous step.

To save the migration to the database, run the "update-database" command in the Package Manager Console ". Your database can now perform the actual EF migration task!

Add database migration and updates

Now, let's assume that we want to link blogs and products. One product can have multiple blogposts to talk about the product itself. We need a one-to-many relationship between products and BlogPost.

Essentially, what we need is the new ProductId field in BlogPost and the ICollection <BlogPost> attribute in Product. The following summarizes the updates we should add to Product. cs and BlogPost. cs (in the Nop. Core Project.

Now that you have prepared attributes on the domain model, you also need to configure and inform Entity Framework how to format parameters, such as links, and whether fields are optional/required. In Nop. open BlogPostMap in Data. cs, enter the following line, and we will tell EF product (or ProductId) is an optional attribute (meaning it can have NULL as a value), a product can have many BlogPost, use ProductId as the foreign key between BlogPost and Product.

We now have enough information to instruct Entity Framework to generate a migration, so open the Package Manager Console again and enter the command "add-migration AddProductToBlogPost ".

This command causes Visual Studio to generate a. cs file similar to the following:

To generate an SQL script and update the database, run the "update-database" command again on the Package Manager Console ". To verify that the database is correctly updated, open the BlogPost table in the database and check whether new fields and Foreign keys have been added.

Conclusion

If you have a lot of custom nopCommerce entities, code migration can be a very convenient tool. In this way, when you want to add new fields and tables to the database, you do not need to touch the database. Everything can be done from the code!

Learn skills and have fun coding!

 

Link: http://www.nopcn.com/nopcommerce-blog-ef-migrations-88.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.