Можно ли выполнить команду Scaffold-DbContext
без ее генерации связей в моделях? Причина, по которой я спрашиваю, заключается в том, что во многих таблицах в моей базе данных отсутствует ключ, и я предпочел бы не go просматривать каждую из них, выяснить, какие модели не имеют ключа, а затем удалить их из определений модели и контекста. .
Или существует ли альтернативный способ поддержания отношений между моделями, когда у одной из моделей нет ключа, а есть только внешний ключ?
Это моя проблема, я получаю это исключение при первом использовании после запуска скаффолда:
InvalidOperationException: The navigation '' cannot be added because it targets the keyless entity type 'AgntJobSetting'. Navigations can only target entity types with keys.
DbContext.cs:
modelBuilder.Entity<AgntJobSetting>(entity =>
{
entity.HasNoKey();
entity.HasOne(d => d.Job)
.WithMany(p => p.AgntJobSetting)
.HasForeignKey(d => d.JobId)
.HasConstraintName("FK_agnt_job_setting_agnt_job");
});
AgntJobSetting.cs:
[Table("agnt_job_setting")]
public partial class AgntJobSetting
{
[Required]
[Column("name")]
[StringLength(100)]
public string Name { get; set; }
[Column("job_id")]
public int JobId { get; set; }
[Column("value")]
[StringLength(1000)]
public string Value { get; set; }
[Column("organization_id")]
public int OrganizationId { get; set; }
[ForeignKey(nameof(JobId))]
[InverseProperty(nameof(AgntJob.AgntJobSetting))]
public virtual AgntJob Job { get; set; }
}
AgntJob.cs:
[Table("agnt_job")]
public partial class AgntJob
{
public AgntJob()
{
AgntJobHistory = new HashSet<AgntJobHistory>();
AgntJobNotification = new HashSet<AgntJobNotification>();
AgntJobSetting = new HashSet<AgntJobSetting>();
}
[Key]
[Column("job_id")]
public int JobId { get; set; }
[Column("guid")]
public Guid Guid { get; set; }
[Required]
[Column("job_name")]
[StringLength(50)]
public string JobName { get; set; }
[Required]
[Column("class_name")]
[StringLength(150)]
public string ClassName { get; set; }
[Required]
[Column("assembly_name")]
[StringLength(150)]
public string AssemblyName { get; set; }
[Column("schedule_cron")]
[StringLength(150)]
public string ScheduleCron { get; set; }
[Column("is_running")]
public bool? IsRunning { get; set; }
[Column("organization_id")]
public int OrganizationId { get; set; }
[Column("created_by")]
[StringLength(50)]
public string CreatedBy { get; set; }
[Column("date_created", TypeName = "datetime")]
public DateTime? DateCreated { get; set; }
[Column("modified_by")]
[StringLength(50)]
public string ModifiedBy { get; set; }
[Column("date_modified", TypeName = "datetime")]
public DateTime? DateModified { get; set; }
[InverseProperty("Job")]
public virtual ICollection<AgntJobHistory> AgntJobHistory { get; set; }
[InverseProperty("Job")]
public virtual ICollection<AgntJobNotification> AgntJobNotification { get; set; }
[InverseProperty("Job")]
public virtual ICollection<AgntJobSetting> AgntJobSetting { get; set; }
}
Трассировка стека исключительной ситуации указывает на то, что в конечном итоге он взорвался в этой строке в моем файле DbContext:
entity.HasOne(d => d.Job)