AspNetCore.Docs
AspNetCore.Docs copied to clipboard
Container issue for EF/MVC
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.
- ID: 55345ff6-3c81-969e-3c92-29c24d78d2ca
- Version Independent ID: aa826512-74a5-752a-0ad0-0f8133f6c39e
- Content: Tutorial: Implement inheritance - ASP.NET MVC with EF Core
- Content Source: aspnetcore/data/ef-mvc/inheritance.md
- Product: aspnet-core
- Technology: aspnetcore-data
- GitHub Login: @Rick-Anderson
- Microsoft Alias: riande
Update edit for container
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.
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
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
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
FromSqlwhich was renamed toFromRawSql. 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
ExecuteSqlCommandAsyncwhich was renamed toExecuteSqlRawAsync. 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 tablePerson. 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
Personentity type toSchoolContext.csand specify the new names forStudentandInstructortables since they are now joined into one tablePerson. The new lines are highlighted.
OR
Add the
Personentity type toSchoolContext.csand remove the unnecessary table name mappings for theStudentandInstructorsince these tables are now joined into one tablePerson.
What do you think?
@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
See #26505