Open rir3 opened 1 year ago
@vijayrkn is this a scaffolding issue? I'm a bit unclear about this one.
This is not specific to scaffolding. Scaffolding will honor the EF conventions or explicit Foreign key setup.
@rir3 - Doesn't the setup as shown in this doc work for you? https://www.learnentityframeworkcore.com/configuration/data-annotation-attributes/foreignkey-attribute
So the scaffolding generates CRUD Razor pages using the Id for the dropdown instead of the Name property. The only way I was able to make this work was by using [Required] attribute on the Name property. This makes me believe there is an issue because the [Required] attribute should not have this effect.
This is specific for when I try scaffolding Razor Pages using Entity Framework(CRUD).
I copied the Doc exactly:
using System.ComponentModel.DataAnnotations;
namespace WebApplication13.Models
{
public class Author
{
public int AuthorId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public ICollection<Book> Books { get; set; }
}
}
using System.ComponentModel.DataAnnotations.Schema;
namespace WebApplication13.Models
{
public class Book
{
public int BookId { get; set; }
public string Title { get; set; }
public Author Author { get; set; }
[ForeignKey("Author")]
public int AuthorFK { get; set; }
}
}
I used scaffolding using the model books to create CRUD Razor Pages using Entity Framework. And in Create.cshtml.cs Line 24:
ViewData["AuthorFK"] = new SelectList(_context.Set<Author>(), "AuthorId", "AuthorId"); //AuthorId is used for dropdown
The AuthorId is used for dropdown. But, when I add the [Required] attribute. As shown in the new Author Class.
using System.ComponentModel.DataAnnotations;
namespace WebApplication13.Models
{
public class Author
{
public int AuthorId { get; set; }
[Required] //This Line is what I am talking about.
public string FirstName { get; set; }
public string LastName { get; set; }
public ICollection<Book> Books { get; set; }
}
}
In Create.cshtml.cs Line 24:
ViewData["AuthorFK"] = new SelectList(_context.Set
This FirstName is used as I wanted in the dropdown. Is this how it is supposed to work? After reading many blogs, and documentation and watching a ton of videos I did not see one person use the [Required] attribute for choosing what will be used for the foreign key dropdown.
The version of Visual Studio I am using is: Microsoft Visual Studio Community 2022 (64-bit) - Current Version 17.3.3
Microsoft Visual Studio Community 2022 Version 17.3.3 VisualStudio.17.Release/17.3.3+32825.248 Microsoft .NET Framework Version 4.8.03752
Installed Version: Community
Visual C++ 2022 00482-90000-00000-AA736 Microsoft Visual C++ 2022
ASP.NET and Web Tools 17.3.376.3011 ASP.NET and Web Tools
C# Tools 4.3.0-3.22423.10+b35bb0baca1071d50cc4f8cf5a1a89e7cc112461 C# components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.
NuGet Package Manager 6.3.0 NuGet Package Manager in Visual Studio. For more information about NuGet, visit https://docs.nuget.org/
Razor (ASP.NET Core) 17.0.0.2232702+e1d654e792aa2fe6646a6935bcca80ff0aff4387 Provides languages services for ASP.NET Core Razor.
SQL Server Data Tools 17.0.62207.04100 Microsoft SQL Server Data Tools
Sorry I Closed by Accident
We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.
Scaffolding Razor Pages using Entity Framework(CRUD) v1.0.0.0
I have tried hundreds of different configurations to get scaffolding to use Name instead of Id for a foreign key dropdown. The only thing that worked was giving the name property a [Required] attribute. Once It had a Required attribute it worked! So, I am thinking this might be an issue because this attribute should not affect scaffolding in this manner.