我已经使用Code First方法在两个实体之间设置了多对多关系。不幸的是,我无法创建下拉列表以选择不同的值并将其添加到对象中。
您能在我的代码中指出错误或缺少的部分吗?
public class Vacancy : IAuditable
{
public Vacancy(){}
/// <summary>
/// General properties
/// </summary>
public int Id { get; set; }
{removing code for simplicity}
/// <summary>
/// A vacancy could have many HBO-I Skills.
/// The ICollection contains a list of HBO-I Skills which consist the HBO-I Skills for the particular vacancy.
/// </summary>
public virtual ICollection<VacancyHBOISkill> VacancyHBOISkills { get; set; }
}
public class HBOISkill : IAuditable
{
public HBOISkill(){}
/// <summary>
/// General properties
/// </summary>
public int Id { get; set; }
{removing code for simplicity}
/// <summary>
/// Many vacancies provides 1 or more HBO-I Skills.
/// </summary>
public virtual ICollection<VacancyHBOISkill> VacancyHBOISkills { get; set; }
}
空缺与HBOISkill之间的交集表
public class VacancyHBOISkill
{
public int VacancyId { get; set; }
public Vacancy Vacancy { get; set; }
public int HBOISkillId { get; set; }
public HBOISkill HBOISkill { get; set; }
}
VacancyHBOISkill类的配置
public void Configure(EntityTypeBuilder<VacancyHBOISkill> entityBuilder)
{
entityBuilder
.HasKey(vh => new { vh.VacancyId, vh.HBOISkillId });
}
DbContext类
{removing code for simplicity}
/// <summary>
/// Defining the database tables within our application
/// </summary
public DbSet<Vacancy> Vacancy { get; set; }
public DbSet<HBOISkill> HBOISkill { get; set; }
public DbSet<VacancyHBOISkill> VacancyHBOISkill { get; set; }
// Adding the Shadow Properties to database schemas when we creating the DbModel
protected override void OnModelCreating(ModelBuilder builder)
{
{removing code for simplicity}
/// <summary>
/// Loading the configuration files for the entities
/// </summary
builder.ApplyConfiguration(new VacancyConfiguration());
builder.ApplyConfiguration(new HBOISkillConfiguration());
builder.ApplyConfiguration(new VacancyHBOISkillConfiguration());
}
我的剃刀页面代码:
public IActionResult OnGet()
{
ViewData["TypeId"] = new SelectList(_context.VacancyType, "Id", "Name");
ViewData["CategoryId"] = new SelectList(_context.Category, "Id", "Desc");
//ViewData["VacancyHBOSKillId"] = new SelectList(_context.HBOISkill, "Id", "Name");
return Page();
}
[BindProperty]
public Vacancy Vacancy { get; set; }
public IEnumerable<HBOISkill> HBOISkillList { get; set; }
public IEnumerable<SelectListItem> GetHBOISkillSelectList()
{
return HBOISkillList
.Select((hs, i) => new SelectListItem
{
Text = hs.Name.ToString(),
Value = hs.Id.ToString()
//Selected = hs.Id == Vacancy.VacancyHBOISkills.Add(Vacancy.Id, hs.Id)
});
}
下面您将找到我的数据库(表)的图片(屏幕快照)。
Database tables
Vacancy class
HBOISkill
VacancyHBOISkill
先谢谢了,
真诚的
Halil
P.S。我将类名从复数更改为单数,这些更改在(旧的)屏幕截图中不可见。