模型是 成员有很多玩家(一对多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}");
}
}
保存条目并调用SaveChangesAsync时出现错误
内部异常: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]在<908a85714c18433696ab2103bcabebcc>:0中 Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync (Microsoft.EntityFrameworkCore.Storage.RelationalCommandParameterObject parameterObject,System.Threading.CancellationToken cancelledToken) [0x004c9]在<908a85714c18433696ab2103bcabebcc>:0中 Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync (Microsoft.EntityFrameworkCore.Storage.RelationalCommandParameterObject parameterObject,System.Threading.CancellationToken cancelledToken) [0x0057b]在<908a85714c18433696ab2103bcabebcc>:0中 Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.ExecuteAsync (Microsoft.EntityFrameworkCore.Storage.IRelationalConnection 连接,System.Threading.CancellationToken取消令牌) <908a85714c18433696ab2103bcabebcc>:0
中的[0x000cc]