Insert using Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)
realnizar opened this issue · comments
Hello,
I am trying to unit test my code which has a class decorated with the Key, DatabaseGenerated(DatabaseGeneratedOption.Identity) for the primary key. When I add an entity using Context.MainEntities.AddAsync(new MainEntity() { X= ""}); and doing a Context.SaveAsync(); the key is being inserted with 0 and if I have a loop I get the error of Duplicate Keys.
I also tried adding the MainEntity to a list and doing a Mock.CreateDbSetMock(x => x.MainEntities, initialEntities); and the primary key is not incrementing.
Your page said that auto increment is supported so was wondering if I am not setting things correctly?
Thanks,
Are you using attribute configuration or entitytypeconfiguration classes?
I am using an entity type configuration but not for that specific entity. The entity I am having issues with is the LoggingRepository entity. Here is the Context code:
public class TelemetryContext: DbContext
{
private DbContextOptions Options { get; set; }
public TelemetryContext() { }
public TelemetryContext(DbContextOptions<TelemetryContext> options) : base(options)
{
Options = options;
}
public virtual DbSet<LoggingType> LoggingTypes { get; set; }
public virtual DbSet<LoggingRepository> LoggingRepositories { get; set; }
public virtual DbSet<LoggingEntry> LoggingEntries { get; set; }
public virtual DbSet<LoggingSetting> LoggingSettings { get; set; }
public virtual DbSet<LoggingSettingEmailList> LoggingSettingEmailLists { get; set; }
public virtual DbSet<LoggingSettingEmailSetup> LoggingSettingEmailSetups { get; set; }
public virtual DbQuery<Views.ApplicationInstance> ApplicationInstances { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseLazyLoadingProxies();
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
// Set default Schema
modelBuilder.HasDefaultSchema("Telemetry");
// Entity Configurations
modelBuilder.ApplyConfiguration(new LoggingSettingConfiguration());
modelBuilder.ApplyConfiguration(new LoggingSettingEmailSetupConfiguration());
modelBuilder.ApplyConfiguration(new LoggingSettingEmailListConfiguration());
modelBuilder.ApplyConfiguration(new LoggingEntryConfiguration());
//View Configuration
modelBuilder.Query<Views.ApplicationInstance>().ToView("vw_ApplicationInstance");
// Seed Database
new Seed.LoggingTypeSeed(modelBuilder);
}
}
The logging Repository code is:
[Table("LoggingRepository")]
public class LoggingRepository
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int LoggingRepositoryId { get; set; }
/// <summary>
/// The link to the application entry
/// </summary>
[Required]
public Guid LoggingEntryId { get; set; }
[ForeignKey(nameof(LoggingEntryId))]
public virtual LoggingEntry LoggingEntry { get; set; }
/// <summary>
/// The date when the log was captured
/// </summary>
[Required]
public DateTimeOffset CreatedOn { get; set; }
/// <summary>
/// The user / system who performed the action that led to the log
/// </summary>
public string CreatedBy { get; set; }
/// <summary>
/// The short message that is logged
/// </summary>
[Required]
[MaxLength(1000)]
public string ShortMessage { get; set; }
/// <summary>
/// The full message being logged
/// </summary>
public string FullMessage { get; set; }
/// <summary>
/// This will be the class type of the serialized message
/// </summary>
[MaxLength(200)]
public string Category { get; set; }
/// <summary>
/// This will be the URL where the log occured if known
/// </summary>
public string Url { get; set; }
}
Thank you for raising this issue.
Should have been fixed in version 1.0.0.26.
Can you try and report back please?