It could very well be trivial, but I can't see what's missing in the following snippet:
using System; using System.ComponentModel.DataAnnotations; namespace Intro.Models { public class Todo { [Key, Required] public Int32 Id; public String Name; public String Note; public DateTime Created; public Boolean Completed; } }
I've also tried
Int32 ID;, but they all amount to the same error:
The entity type 'Todo' requires a primary key to be defined. at Microsoft.VisualStudio.Web.CodeGeneration.ActionInvoker.<BuildCommandLine>b__6_0() at Microsoft.Extensions.CommandLineUtils.CommandLineApplication.Execute(String[] args) at Microsoft.VisualStudio.Web.CodeGeneration.ActionInvoker.Execute(String[] args) at Microsoft.VisualStudio.Web.CodeGeneration.CodeGenCommand.Execute(String[] args)
I am trying to follow an outdated 1.1 tutorial on Microsoft Docs, and was trying to scaffold some views/controllers with:
dotnet aspnet-codegenerator controller -name TodoController-m Todo-dc TodoContext--relativeFolderPath Controllers--useDefaultLayout --referenceScriptLibraries
My
DbContextfor reference:
using System; using Microsoft.EntityFrameworkCore; namespace Intro.Models{publicclassTodoContext:DbContext{publicTodoContext(DbContextOptions<TodoContext> options):base(options){}publicDbSet<Todo>Todos{ get;set;}//protected override void OnModelCreating(ModelBuilder modelBuilder) {// modelBuilder.Entity<Todo>().HasKey(todo => todo.Id);//}}}
And my
Startup:
using Microsoft.EntityFrameworkCore; using Intro.Models; namespace Intro{publicclassStartup{publicIConfigurationConfiguration{ get;}publicStartup(IConfiguration configuration){Configuration= configuration;}publicvoidConfigureServices(IServiceCollection services){// MVC stuff deducted services.AddDbContext<TodoContext>(options => options.UseSqlServer("Server=127.0.0.1;Database=LearnCSharp.Intro.Test1;User Id=SA;Password=smallC!0ud;"));}// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.publicvoidConfigure(IApplicationBuilder app,IHostingEnvironment env){// MVC stuff deducted...// Ensure createdvar context =newTodoContext(app.ApplicationServices.GetRequiredService<DbContextOptions<TodoContext>>()); context.Database.EnsureCreated();}}}
Finally my
BuildWebHost():
returnWebHost.CreateDefaultBuilder(args).UseApplicationInsights().UseStartup<Startup>().UseDefaultServiceProvider(options => options.ValidateScopes=false).Build();
I read