Problem: Hi guys. Im using EF core 2.1 and I need to have in the Table InstallationInformation the attributes LicenceKey and Importrun(FK) to a unique index(combination).
Added Info: If im right with the research that i have made. I cant use Annotation [index] in .net Core 2.1 cause it not supported. I tryed to upgrade but it needed VS 2019 which i dont have. And to use VS 2019 I need windows 10 which i also not using.
Methods that i have tryed. I tryed the Annotation thing. But unfortunately it was not working. I found out that i could try using Fluent API and found out that i could map the attribute.
modelBuilder.Entity<InstallationInformation>(entity =>
{
entity.HasIndex(i => new { i.LicenceKey, i.ImportRun }).IsUnique();
});
But as i was debbuging it had this error "InvalidOperationException: 'ImportRun' cannot be used as a property on entity type 'InstallationInformation' because it is configured as a navigation."
Code structure:
public class ImportRun
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ImportRunId { get; set; }
public DateTime ExtractedDate { get; set; }
public DateTime ProcessDate { get; set; }
public ResultCondition ProcessResult { get; set; }
public string FailureDescription { get; set; }
[Required]
public virtual ICollection<InstallationInformation> InstallationInformation { get; set; }
}
public class InstallationInformation
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int InstallationInformationID { get; set; }
[MaxLength(256)]
public string LicenceKey { get; set; }
[MaxLength(256)]
public string ProductVersion { get; set; }
[MaxLength(256)]
public string ProductName { get; set; }
[MaxLength(256)]
public string CompanyName { get; set; }
public DateTime Timestamp { get; set; }
[Required]
public ImportRun ImportRun { get; set; }
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<ImportRun>()
.HasMany(c => c.InstallationInformation)
.WithOne(e => e.ImportRun)
.IsRequired();
modelBuilder.Entity<InstallationInformation>(entity =>
{
entity.HasIndex(i => new { i.LicenceKey, i.ImportRun }).IsUnique();
});
}
Change:
modelBuilder.Entity<InstallationInformation>(entity =>
{
entity.HasIndex(i => new { i.LicenceKey, i.ImportRun }).IsUnique();
});
To:
modelBuilder.Entity<InstallationInformation>(entity =>
{
entity.HasIndex(i => new { i.LicenceKey, i.ImportRunId }).IsUnique();
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.