NET Core 3.1 – First Basic Project Started-1 (Simple Steps to Start any Project)

.NET Core is a general-purpose framework that may be used to build software applications for Windows, Linux, and MacOS. Unlike other software frameworks, 

1-

2-

Startup.cs

public void ConfigureServices(IServiceCollection services)
         {       
 services.AddRazorPages();
        services.AddDbContext<ApplicationDbContext>
            (options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
        services.AddControllersWithViews(); //json

        services.AddSwaggerGen(c =>
        {
            c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });
        });

    }

appsettings.json

{

  "ConnectionStrings": {
    "DefaultConnection": "Server=DESKTOP-H9FJ8PG\\MESUT;Database=BookListRazor;Trusted_Connection=True;"
  },

  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*"
}

Book.js

public class Book
{
    [Key]
    public int Id { get; set; }
    [Required]
    public String Name { get; set; }
    public String Author { get; set; }
    public String ISBN { get; set; }

}

ApplicationDbContext.cs

namespace BlazorBooks.Model
{
    public class ApplicationDbContext : DbContext
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
        {

        }

        public DbSet<Book> Book { get; set; }
    }
}

codebased migration in code first
Add-Migration
Update-Database

https://www.entityframeworktutorial.net/code-first/code-based-migration-in-code-first.aspx

Source Code: https://github.com/bhrugen/BookListRazor

Bir cevap yazın

Your email address will not be published. Please enter your name, email and a comment.