Scaffolding Template on Asp. Net Core Razor Page, scaffoldingrazor

Source: Internet
Author: User

Scaffolding Template on Asp. Net Core Razor Page, scaffoldingrazor
Scaffolding Template Intro

We know that in Asp. net MVC, if you use EF DBContext, you can right-click solution-add controller containing view in, then vs will automatically generate the corresponding CURD controller and View based on the Model you selected, which is very convenient. This is called ASP. NET Scaffolding Template, which has previously been introduced by Xueyan. If you are interested, please refer to [portal]

Everyone knows about Asp recently. net Core2.0 was released, and Microsoft also released Razor Page as the default Asp. net Core Web project, but the Scaffolding Template (hereinafter referred to as ST) function was not provided at the beginning, so that we need to write 4-5 pages and PageModel for a Model each time, compared with Asp. net MVC is much more, but it is better that ST appears in time, but this time it is not integrated into VS, we need to add a build package and CMD command to complete, although it is complicated, but at least the code size is low. Let's do it!

Hello Scaffolding Razor PageStep 1: Create Razor Page project Step 2: create a Model. Here we create a Blog entity class:
public class Blog{    public int BlogId { get; set; }    public string Title { get; set; }    public string Author { get; set; }    public DateTime CreatedDate { get; set; }}
Step 3: Create DbContext:
public class BlogDbContext : DbContext{    public BlogDbContext(DbContextOptions<BlogDbContext> options) : base(options)    {    }    public DbSet<Blog> Blogs { get; set; }    protected override void OnModelCreating(ModelBuilder modelBuilder)    {        modelBuilder.Entity<Blog>().ToTable("Blog");    }}
Step 4: configure the connection string and register the DbContext to the project Container Using DI.
public void ConfigureServices(IServiceCollection services){    services.AddDbContext<BlogDbContext>(options =>        options.UseSqlServer(Configuration.GetConnectionString("BlogDbContext")));    services.AddMvc();}
Remember to add connectString to the configuration file (appsettings. json:
{  "ConnectionStrings": {    "MovieContext": "Server=(localdb)\\mssqllocaldb;Database=DemoDb;Trusted_Connection=True;MultipleActiveResultSets=true"  }}
Step 5: Install CodeGeneration package, tool-Nuget package Manager-package Management Console
Install-Package Microsoft.VisualStudio.Web.CodeGeneration.Design -Version 2.0.0
Step 6: add data migration-update to database:
Add-Migration InitialUpdate-Database

[Here, you must perform the next migration. Otherwise, the generated code will fail. I guess the generated code will read the database.]

Step 7: Open the terminal (CMD or Powershell)

First, go to the project directory (directory Program. cs and Startup. cs)

Mine is: E: \ project \ aspnet \ Demos \ WebApplication4 \ WebApplication4

Run the following command:

dotnet aspnet-codegenerator razorpage -m Blog -dc BlogDbContext  -udl -outDir Pages\Blogs –referenceScriptLibraries

OK. You will see the following prompt:

We can see that it automatically creates the corresponding View and PageModel.

Although it is not as convenient as MVC, it also saves us a lot of time.

The DbSet name of the DbContext automatically generated by Issues is incorrect.

Let's take a look at the Create. cshtml. cs code, one of which is:

public async Task<IActionResult> OnPostAsync(){    if (!ModelState.IsValid)    {        return Page();    }    _context.Blog.Add(Blog);    await _context.SaveChangesAsync();    return RedirectToPage("./Index");}


We can see that it usesBlogIt is the Set Name of the object class, but in Step 3, I use Blogs. This should be a bug and I want to submit it, but no corresponding project is found.

Conclusion

The day after tomorrow is the birthday of the great motherland. Everyone has a happy holiday! Hope to go to HK Disney tomorrow, hey.

I will summarize the problems encountered by Razor Page later. I am very optimistic about it and believe it will become increasingly popular.

PS: asp.net core QQ Study Group: 376248054 customs clearance password: cnblogs (no password will pass)

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.