I have two models that relate many to one with each other.
public class AlertRule: Identity
{
public long RuleId { get; set; }
[ForeignKey(nameof(RuleId))]
public virtual SepRuleBase Rule { get; set; }
public long AlertId { get; set; }
[ForeignKey(nameof(AlertId))]
public virtual Alert Alert { get; set; }//new; it was commented; maybe cause error
}
public abstract class SepRuleBase : BaseEntity
{
public string Name { get; set; }
public int Score { get; set; }
public string Description { get; set; }
[JsonIgnore]
public bool IsDeleted { get; set; }
[JsonIgnore]
public bool IsActive { get; set; } = true;
[JsonIgnore]
public int Type { get; set; }
public virtual ICollection<AlertRule> AlertRules { get; set; }
public override string GetTableName()
{
return "SepRule";
}
}
and the configurations is like this:
public class AlertRuleConfiguration: IEntityTypeConfiguration<AlertRule>
{
public void Configure(EntityTypeBuilder<AlertRule> builder)
{
builder.ToTable("AlertRule".ConvertDbName())
.HasKey(x => x.Id)
;
builder.Property(x => x.Id)
.HasColumnName("ID".ConvertDbName())
.UseOracleIdentityColumn()
.IsRequired()
builder.Property(x => x.AlertId)
.HasColumnName("AlertId".ConvertDbName())
.IsRequired();
builder.Property(x => x.RuleId)
.HasColumnName("RuleId".ConvertDbName())
.IsRequired();
builder
.HasOne(p => p.Rule)
.WithMany(p => p.AlertRules)
.HasForeignKey(p => p.RuleId)
.HasConstraintName("FK_AlertRule_Rule".ConvertDbName())
.OnDelete(DeleteBehavior.Cascade)
;
builder
.HasOne(p => p.Alert)
.WithMany(p => p.AlertRules)
.HasForeignKey(p => p.AlertId)
.HasConstraintName("FK_AlertRule".ConvertDbName())//change here
.OnDelete(DeleteBehavior.Cascade)
;
builder.HasIndex(p => p.AlertId).HasName("IX_AlrtRl_AlrtId".ConvertDbName());
builder.HasIndex(p => p.RuleId).HasName("IX_AlrtRl_RlId".ConvertDbName());
}
}
public class SepRuleBaseConfiguration : IEntityTypeConfiguration<SepRuleBase>
{
public void Configure(EntityTypeBuilder<SepRuleBase> builder)
{
builder.HasKey(x => x.Id);
//.HasName("ID");
builder.Property(x => x.Id)
.HasColumnName("ID".ConvertDbName())
.UseSqlServerIdentityColumn()
.IsRequired()
builder.Property(x => x.Type)
.HasColumnName("Type".ConvertDbName())
.IsRequired();
builder.Property(x => x.Name)
.HasColumnName("Name".ConvertDbName())
.IsRequired();
builder.Property(x => x.Score)
.HasColumnName("Score".ConvertDbName())
.IsRequired();
builder.Property(x => x.Description)
.HasColumnName("Description".ConvertDbName())
.IsRequired(false);
builder.Property(x => x.LastUpdate)
.HasColumnName("LastUpdate".ConvertDbName())
.IsRequired();
builder.Property(x => x.LastUpdateBy)
.HasColumnName("LastUpdateBy".ConvertDbName())
.IsRequired();
builder.Property(x => x.IsDeleted)
.HasColumnName("IsDeleted".ConvertDbName())
.IsRequired();
builder.Property(x => x.IsActive)
.HasColumnName("IsActive".ConvertDbName())
.IsRequired();
builder.ToTable("SepRule".ConvertDbName());;
}
}
When I try to insert into AlertRule table,
_dbContext.AlertRules.Add(new AlertRule
{
AlertId = dbAlert.Id,
RuleId = long.Parse(rule)
});
_dbContext.SaveChanges();
When I use Logging for EntityFramework, the query that dbcontext try to run like this:
And made error: SqlException: Invalid column name 'RuleBaseId'.
But I dont have any RuleBaseId
column in my code!!
What is wrong with my code?