AspNetCore.Docs icon indicating copy to clipboard operation
AspNetCore.Docs copied to clipboard

Container issue for EF/MVC

Open AucT opened this issue 5 years ago • 7 comments

This is the container issue for this doc. Reopen each issue you are working on so the owner is notified.

Before any of these issues are fixed, we'll do #26505, which will resolve many of the following issues.

  • This: asp.net core 3.1 fails to create migration - The entity type 'Instructor' cannot be mapped to a table because it is derived from 'Person'. Only base entity types can be mapped to a table.
  • Manual migration code is wrong for resulting model #21860
  • Adding Person breaks unless you also comment out or delete sections of the schoolcontext.cs #20671
  • Getting type cannot be instantiated error for abstract base class #21430
  • Home Controller About Custom Code is wrong #21861
  • Using .net5, the FromSQL produces an error #21783
  • SingleOrDefaultAsync throws if multiple rows returned, not null #20754
  • Examine SQL queries... not working #21862
  • ASP.NET MVC with EF Core Tutorial: Implement inheritance #22047
  • Incomplete code changes. #22828

Відомості про документ

Не змінюйте цей розділ. Він необхідний для зв’язування питань на платформах docs.microsoft.com ➟ GitHub.

AucT avatar Mar 26 '20 14:03 AucT

Update edit for container

Rick-Anderson avatar Mar 26 '20 18:03 Rick-Anderson

I was also getting the same error when I tried to create migration. I removed modelBuilder.Entity<Instructor>().ToTable("Instructor"); and modelBuilder.Entity<Student>().ToTable("Student"); from OnModelCreating method in context class and now it is working. I found the solution her https://docs.microsoft.com/en-us/ef/core/what-is-new/ef-core-3.0/breaking-changes#totable-on-a-derived-type-throws-an-exception.

mima89 avatar Mar 26 '20 19:03 mima89

Hi, My application also didn't run when I completed the inheritance part. I've changed the table name to Person in SchoolContext.cs

modelBuilder.Entity<Student>().ToTable("Person"); // <-- modelBuilder.Entity<Student>().ToTable("Student"); 

// and

modelBuilder.Entity<Instructor>().ToTable("Person"); // <-- modelBuilder.Entity<Instructor>().ToTable("Instructor");

We can see the same code in the completed application on GitHub. This issue is reported in #22828, #22047, #20671

Funix565 avatar Jul 07 '22 19:07 Funix565

As for the #21783, FromSql should be FromSqlRaw in DepartmentsController.cs

var department = await _context.Departments
                .FromSqlRaw(query, id)
                .Include(d = > d.Administrator)
                .AsNoTracking()
                .FirstOrDefaultAsync();

The merge to the docs was approved in #23398. The changes are described in this article https://docs.microsoft.com/en-us/ef/core/what-is-new/ef-core-3.x/breaking-changes#fromsql. Tracking issue https://github.com/dotnet/efcore/issues/10996

Funix565 avatar Jul 21 '22 18:07 Funix565

Dear @Rick-Anderson ,

Could you please explain to me the purpose of this container issue? As I see, they are all closed with almost no solution or explanation provided.

The latest commit to the EF Core with MVC repository was done on Jun 24, 2022. So I think it would be still relevant to adjust some code snippets which misled me and may confuse other users too.

Now there are 4 directories related to this tutorial. It would be great to know what they are for so I could make the necessary edits.

  • 5cu-snap
  • 5cu
  • cu-final
  • cu

Here is my plan:

  • [ ] https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/advanced?view=aspnetcore-6.0#call-a-query-to-return-entities Here we have FromSql which was renamed to FromRawSql. According to the md file of this page ( https://github.com/dotnet/AspNetCore.Docs/blob/main/aspnetcore/data/ef-mvc/advanced.md?plain=1#L51 ), I should edit intro/samples/cu/Controllers/DepartmentsController.cs?name=snippet_RawSQL&highlight=8,9,10 This line: https://github.com/dotnet/AspNetCore.Docs/blob/main/aspnetcore/data/ef-mvc/intro/samples/cu/Controllers/DepartmentsController.cs#L64

  • [ ] https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/advanced?view=aspnetcore-6.0#call-an-update-query Here we have ExecuteSqlCommandAsync which was renamed to ExecuteSqlRawAsync. According to https://github.com/dotnet/AspNetCore.Docs/blob/main/aspnetcore/data/ef-mvc/advanced.md?plain=1#L83, I should edit intro/samples/cu/Controllers/CoursesController.cs?name=snippet_UpdatePost This line: https://github.com/dotnet/AspNetCore.Docs/blob/main/aspnetcore/data/ef-mvc/intro/samples/cu/Controllers/CoursesController.cs#L211

  • [ ] https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/inheritance?view=aspnetcore-6.0#add-person-to-the-model Here we still have <Student> and <Instructor> mapped to their tables. But they don't exist in the database, because we joined these two tables into one table Person. According to the md file of this page ( https://github.com/dotnet/AspNetCore.Docs/blob/main/aspnetcore/data/ef-mvc/inheritance.md?plain=1#L86 ), I should edit intro/samples/cu/Data/SchoolContext.cs?name=snippet_AfterInheritance&highlight=19,30 According to the final version, we should have

modelBuilder.Entity<Student>().ToTable("Person");
modelBuilder.Entity<Instructor>().ToTable("Person");

Or should we remove these lines at all? https://github.com/dotnet/AspNetCore.Docs/blob/main/aspnetcore/data/ef-mvc/intro/samples/cu/Data/SchoolContext.cs#L113 https://github.com/dotnet/AspNetCore.Docs/blob/main/aspnetcore/data/ef-mvc/intro/samples/cu/Data/SchoolContext.cs#L115

Then we should also rephrase the action phrase to something like that: https://github.com/dotnet/AspNetCore.Docs/blob/main/aspnetcore/data/ef-mvc/inheritance.md?plain=1#L84

Add the Person entity type to SchoolContext.cs and specify the new names for Student and Instructor tables since they are now joined into one table Person. The new lines are highlighted.

OR

Add the Person entity type to SchoolContext.cs and remove the unnecessary table name mappings for the Student and Instructor since these tables are now joined into one table Person.

What do you think?

Funix565 avatar Jul 21 '22 20:07 Funix565

@Funix565

Could you please explain to me the purpose of this container issue?

We don't have the resources to working on issues for this document, so we are keeping one container issue to track all the issues we'd like to fix.

The entire doc needs to be updated using much of the code from Razor Pages with Entity Framework Core

Rick-Anderson avatar Jul 21 '22 21:07 Rick-Anderson

See #26505

Rick-Anderson avatar Jul 21 '22 22:07 Rick-Anderson