问题描述
模型是 成员有很多玩家(一对多Relationshiphip) 玩家有很多运动 体育有很多球员 PlayerSports与玩家和体育之间的多对多关系
public class Member
{
public int MemberId { get; set; }
public string Code { get; set; }
public double Payment { get; set; }
public virtual List<Player> Players { get; set; }
}
public class Player
{
public int PlayerId { get; set; }
public string Name { get; set; }
public double Payment { get; set; }
public virtual ICollection<PlayerSport> PlayerSports { get; set; }
public int MemberId { get; set; }
public virtual Member Member { get; set; }
}
public class Sport
{
public int SportId { get; set; }
public string Name { get; set; }
public string Type { get; set; }
public double Price { get; set; }
public virtual ICollection<PlayerSport> PlayerSports { get; set; }
}
public class PlayerSport
{
public int PlayerId { get; set; }
public int SportId { get; set; }
public virtual Player Player { get; set; }
public virtual Sport Sport { get; set; }
}
当成员被删除时,删除子级球员而不删除运动 删除播放器后,其父级成员和体育项目仍然相同
public class AppContext : DbContext
{
public DbSet<Member> Members { get; set; }
public DbSet<Player> Players { get; set; }
public DbSet<Sport> Sports { get; set; }
public DbSet<PlayerSport> PlayeRSSports { get; set; }
public AppContext()
{
sqlitePCL.Batteries_V2.Init();
this.Database.EnsureCreated();
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Member>()
.HasMany<Player>(m => m.Players)
.WithOne(p => p.Member)
.HasForeignKey(p => p.MemberId)
.OnDelete(DeleteBehavior.Cascade);
modelBuilder.Entity<PlayerSport>()
.HasKey(t => new { t.PlayerId,t.SportId });
modelBuilder.Entity<PlayerSport>()
.HasOne(ps => ps.Player)
.WithMany(p => p.PlayerSports)
.HasForeignKey(ps => ps.PlayerId);
modelBuilder.Entity<PlayerSport>()
.HasOne(ps => ps.Sport)
.WithMany(s => s.PlayerSports)
.HasForeignKey(ps => ps.SportId);
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
string dbPath = Path.Combine(FileSystem.AppDataDirectory,"members.db3");
optionsBuilder
.Usesqlite($"Filename={dbPath}");
}
}
内部异常:Microsoft.Data.sqlite.sqliteException(0x80004005): sqlite错误19:“ FOREIGN KEY约束失败”。
FullError:
内部异常:Microsoft.Data.sqlite.sqliteException(0x80004005): sqlite错误19:“ FOREIGN KEY约束失败”。在 Microsoft.Data.sqlite.sqliteException.ThrowExceptionForRC (system.int32 rc,sqlitePCL.sqlite3 db)[0x0006d]在 :0处 Microsoft.Data.sqlite.sqliteDataReader.NextResult()[0x0017b]在 :0处 Microsoft.Data.sqlite.sqliteCommand.ExecuteReader (System.Data.CommandBehavior行为)在[0x000d4]中 :0处 Microsoft.Data.sqlite.sqliteCommand.ExecuteReaderAsync (System.Data.CommandBehavior行为, System.Threading.CancellationToken cancellingToken)[0x00007]在 :0处 Microsoft.Data.sqlite.sqliteCommand.ExecuteDbDataReaderAsync (System.Data.CommandBehavior行为, System.Threading.CancellationToken cancellingToken)[0x00011]在 :0处 Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync (Microsoft.EntityFrameworkCore.Storage.RelationalCommandParameterObject parameterObject,System.Threading.CancellationToken cancelledToken) [0x00278]在:0中 Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync (Microsoft.EntityFrameworkCore.Storage.RelationalCommandParameterObject parameterObject,System.Threading.CancellationToken cancelledToken) [0x004c9]在:0中 Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync (Microsoft.EntityFrameworkCore.Storage.RelationalCommandParameterObject parameterObject,System.Threading.CancellationToken cancelledToken) [0x0057b]在:0中 Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.ExecuteAsync (Microsoft.EntityFrameworkCore.Storage.IRelationalConnection 连接,System.Threading.CancellationToken取消令牌) :0
中的[0x000cc]
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)