Connector.cs
public virtual DbSet<Scheduler> Scheduler { get; set; }
public virtual DbSet<Warn> Warn { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseLazyLoadingProxies().UseMySql("Url");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Warn>(entity => {
entity.HasKey(e => new {e.Gid, e.UserId})
.HasName("PRIMARY");
entity.Property(e => e.Gid)
.HasColumnName("gid")
.HasColumnType("bigint(20) unsigned");
entity.Property(e => e.UserId)
.HasColumnName("userId")
.HasColumnType("bigint(20) unsigned");
});
modelBuilder.Entity<Scheduler>(entity => {
entity.HasKey(e => e.Gid);
entity.Property(e => e.Gid)
.HasColumnName("gid")
.HasColumnType("bigint(20) unsigned");
entity.Property(e => e.UserId)
.HasColumnName("userId")
.HasColumnType("bigint(20) unsigned");
entity.HasOne(d => d.Warn)
.WithMany(p => p.Scheduler)
.HasForeignKey(d => d.Gid)
.HasPrincipalKey(p => p.Gid)
.HasConstraintName("scheduler_ibfk_1");
});
}
Test.cs
var warns = ctx.Warn;
foreach(var id in Ids) {
ctx.DetachLocal<Warn>(_ => _.Gid == Guild.Id && _.UserId == id);
var userWarn = await warns.SingleAsync(e => e.Gid == Guild.Id && e.UserId == id);
ctx.Scheduler.Add(new Scheduler {
Gid = Guild.Id, UserId = id, StartTime = DateTime.Now, EndTime = type.Time,
IsUnlimited = !type.Time.HasValue, WarnCount = type.WarnCount,
Handlername = Author.FullUserName()
});
}
await ctx.SaveChangesAsync();
我将数据添加而不是删除,但是由于某些原因,旧数据被删除,新数据被添加。
该代码使用回收连接器,所以我分离了数据,这有关系吗?