Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
302 views
in Technique[技术] by (71.8m points)

C# .NET core 3 MVC Scaffolding with EntityFrameworkCore

I have to following many-to-many model structure:

 public class User
    {    
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public Guid UserId { get; set; }

        [Required]
        [MaxLength(50)]
        public string Name{ get; set; }

        [Required]
        [MaxLength(50)]
        public string Email { get; set; }

        
        public virtual ICollection<UserInRole> UserInRoles { get; set; }
   }

 public class Role
    {
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int RoleId { get; set; }
        public string RoleName { get; set; }
                
        public virtual ICollection<UserInRole> UserInRoles { get; set; }
    }


 public class UserInRole
    {
        [Required]        
        public Guid UserId { get; set; }
    
        [Required]       
        public int RoleId { get; set; }
    
    
        public virtual User User { get; set; }
        public virtual Role Role { get; set; }
    }

ApplicationDbContext:

 protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            

            // UserInRole: set both fields as primary
            modelBuilder.Entity<UserInRole>().HasKey(table => new{ table.RoleId, table.UserId});


            modelBuilder.Entity<User>()
                .HasMany(c => c.UserInRoles);


            modelBuilder.Entity<Role>()
                .HasMany(c => c.UserInRoles);

The database is successfully created with the right relationships (many to many) but when I create a new Controller & View using scaffolding - the system generates the CRUD pages without the option to select the role for each user. is it normal? do I need to do this part by myself?

question from:https://stackoverflow.com/questions/65860406/c-sharp-net-core-3-mvc-scaffolding-with-entityframeworkcore

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Yes, as far as I remember you should render one-to-many relations in your views manually.

One of the easiest way I know:

<div class="form-group">
            <label asp-for="User" class="control-label"></label>
            <select asp-for="UserId"
                    class="form-control"
                    asp-items="@(new SelectList(ViewBag.Users, "UserId", "Name"))">
            </select>
        </div>
        <div class="form-group">
            <label asp-for="Role"></label>
            <select asp-for="RoleId"
                    class="form-control"
                    asp-items="@(new SelectList(ViewBag.Roles, "RoleId", "RoleName"))">
            </select>
        </div>

In this example, you send UsersList and RolesList to the View by ViewBag as a SelectList and then render them on View. So as far as I know you need to do it manually to render one-to-many or many-to-many relations because the list needs to be converted to the SelectList for the view to know how to work with it.

You can also take a look at here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...