entity-framework-core - 实体框架首先在多对多关系数据库中生成错误查询
问题描述
我有 6 个表的关系:关联模型:
public class Product
{
public int? GameId { get; set; }
public virtual Game Game { get; set; }
public int? PlatformId { get; set; }
public virtual Platform Platform { get; set; }
public int? ManufactureId { get; set; }
public virtual Manufacture Manufacture { get; set; }
public int? LocalizationId { get; set; }
public virtual Localization Localization { get; set; }
public int? GenreId { get; set; }
public virtual Genre Genre { get; set; }
public int? RegionrestrictId { get; set; }
public virtual Country RegionRestrict { get; set; }
}
主要相关表:
public class Game
{
public int Id { get; set; }
public string Titleofgame { get; set; }
public string Description { get; set; }
public DateTime DateRelese { get; set; }
public virtual List<Manufacture> Manufactures { get; set; } = new List<Manufacture>();
public List<Product> Products { get; set; } = new List<Product>();
public int? Idseriesofgame { get; set; }
public Serie Series { get; set; }
public int? Idpublication { get; set; }
public Publication Publication { get; set; }
public virtual List<Platform> Platforms { get; set; } = new List<Platform>();
public virtual List<Localization> Localizations { get; set; } = new List<Localization>();
public virtual List<Genre> Genres { get; set; } = new List<Genre>();
public virtual List<Country> RegionRestricts { get; set; } = new List<Country>();
}
以及与“游戏”相关的表之一(其余表相同)
public class Localization
{
public int Id { get; set; }
[Display(Name = "Язык")]
public string Titleoflocalization { get; set; }
public virtual List<Game> Games { get; set; } = new List<Game>();
public List<Product> Products { get; set; } = new List<Product>();
}
流利的 Api 配置:
modelBuilder
.Entity<Game>()
.HasMany(c => c.Localizations)
.WithMany(s => s.Games)
.UsingEntity<Product>(
j =>
j.HasOne(pt => pt.Localization)
.WithMany(t => t.Products)
.HasForeignKey(pt => pt.LocalizationId),
j =>
j.HasOne(pt => pt.Game)
.WithMany(p => p.Products)
.HasForeignKey(pt => pt.GameId),
j =>
{
j.HasKey(t => new { t.GameId, t.ManufactureId, t.GenreId, t.PlatformId, t.RegionrestrictId, t.LocalizationId });
j.ToTable("Products");
}
);
modelBuilder
.Entity<Game>()
.HasMany(c => c.Platforms)
.WithMany(s => s.Games)
.UsingEntity<Product>(
j =>
j.HasOne(pt => pt.Platform)
.WithMany(t => t.Products)
.HasForeignKey(pt => pt.PlatformId),
j =>
j.HasOne(pt => pt.Game)
.WithMany(p => p.Products)
.HasForeignKey(pt => pt.GameId),
j =>
{
j.HasKey(t => new { t.GameId, t.ManufactureId, t.GenreId, t.PlatformId, t.RegionrestrictId, t.LocalizationId });
j.ToTable("Products");
}
);
modelBuilder
.Entity<Game>()
.HasMany(c => c.Genres)
.WithMany(s => s.Games)
.UsingEntity<Product>(
j =>
j.HasOne(pt => pt.Genre)
.WithMany(t => t.Products)
.HasForeignKey(pt => pt.GenreId),
j =>
j.HasOne(pt => pt.Game)
.WithMany(p => p.Products)
.HasForeignKey(pt => pt.GameId),
j =>
{
j.HasKey(t => new { t.GameId, t.ManufactureId, t.GenreId, t.PlatformId, t.RegionrestrictId, t.LocalizationId });
j.ToTable("Products");
}
);
modelBuilder
.Entity<Game>()
.HasMany(c => c.RegionRestricts)
.WithMany(s => s.Games)
.UsingEntity<Product>(
j =>
j.HasOne(pt => pt.RegionRestrict)
.WithMany(t => t.Products)
.HasForeignKey(pt => pt.RegionrestrictId),
j =>
j.HasOne(pt => pt.Game)
.WithMany(p => p.Products)
.HasForeignKey(pt => pt.GameId),
j =>
{
j.HasKey(t => new { t.GameId, t.ManufactureId, t.GenreId, t.PlatformId, t.RegionrestrictId , t.LocalizationId});
j.ToTable("Products");
}
);
modelBuilder.Entity<Game>()
.HasOne(a => a.Series)
.WithMany(a => a.Games)
.HasForeignKey(c => c.Idseriesofgame);
modelBuilder
.Entity<Game>()
.HasMany(c => c.Manufactures)
.WithMany(s => s.Games)
.UsingEntity<Product>(
j =>
j.HasOne(pt => pt.Manufacture)
.WithMany(t => t.Products)
.HasForeignKey(pt => pt.ManufactureId),
j =>
j.HasOne(pt => pt.Game)
.WithMany(p => p.Products)
.HasForeignKey(pt => pt.GameId).OnDelete(DeleteBehavior.Cascade),
j =>
{
j.HasKey(t => new { t.GameId, t.ManufactureId, t.GenreId, t.PlatformId, t.RegionrestrictId });
j.ToTable("Products");
}
);
modelBuilder.Entity<Game>().HasMany(d => d.Products).WithOne(d => d.Game).HasForeignKey(d => d.GameId).OnDelete(DeleteBehavior.Cascade);
modelBuilder.Entity<Product>()
.HasOne(bc => bc.Game)
.WithMany(b => b.Products)
.HasForeignKey(bc => bc.GameId);
modelBuilder.Entity<Product>()
.HasOne(bc => bc.RegionRestrict)
.WithMany(b => b.Products)
.HasForeignKey(bc => bc.RegionrestrictId);
modelBuilder.Entity<Product>()
.HasOne(bc => bc.Genre)
.WithMany(b => b.Products)
.HasForeignKey(bc => bc.GenreId);
modelBuilder.Entity<Product>()
.HasOne(bc => bc.Localization)
.WithMany(b => b.Products)
.HasForeignKey(bc => bc.LocalizationId);
modelBuilder.Entity<Product>()
.HasOne(bc => bc.Manufacture)
.WithMany(b => b.Products)
.HasForeignKey(bc => bc.ManufactureId);
modelBuilder.Entity<Product>()
.HasOne(bc => bc.Platform)
.WithMany(b => b.Products)
.HasForeignKey(bc => bc.PlatformId);
modelBuilder.Entity<Product>()
.HasKey(bc => bc.GameId );
modelBuilder.Entity<Game>().HasKey(p => p.Id);
modelBuilder.Entity<Genre>().HasKey(p => p.Id);
modelBuilder.Entity<Localization>().HasKey(p => p.Id);
modelBuilder.Entity<Manufacture>().HasKey(p => p.Id);
modelBuilder.Entity<Platform>().HasKey(p => p.Id);
当我尝试按条件删除例如流派时:
Product product = new Product() { GameId = 1, GenreId =1};
_context.Entry(product).State = EntityState.Deleted;
var game = publication.Game;
game.Products.Remove(product);
_context.SaveChanges();;
(genreid==1 和 gameid==1 的产品只有一个):
bug: 20.08.2021 00:25:51.250 RelationalEventId.CommandExecuting[20100] (Microsoft.EntityFrameworkCore.Database.Command)
Executing DbCommand [Parameters=[@p0='1'], CommandType='Text', CommandTimeout='30']
DELETE FROM `Products`
WHERE `gameid` = @p0;
SELECT ROW_COUNT();
info: 20.08.2021 00:25:51.253 RelationalEventId.CommandExecuted[20101] (Microsoft.EntityFrameworkCore.Database.Command)
Executed DbCommand (2ms) [Parameters=[@p0='1'], CommandType='Text', CommandTimeout='30']
DELETE FROM `Products`
WHERE `gameid` = @p0;
SELECT ROW_COUNT();
顺便说一句,当我尝试添加时,一切都很好。我不知道我做错了什么。请帮我。
解决方案
推荐阅读
- asp.net - ASP.net MVC 表单发布安全
- visual-studio - 有什么方法可以保存在 Visual Basic 6.0 中制作的程序的复选框状态
- extjs - `exclude -namespace=Ext.ux.ajax` 不适用于 sencha cmd 7.0
- arrays - 如何在visual foxpro中比较两个数组及其元素?
- java - Integer.valueOf 针对一个字符返回 ASCII
- reactjs - TSX 类组件中的 Material UI makeStyles
- ubuntu - MSB4184,VS 代码[Codium],Ubuntu 18.04.3 LTS,ASP.NET Core 项目g
- javascript - javascript:如果外部 .php 文件具有 .. 内联样式,则隐藏 div 永远不会改变
- android - 如果我的推文长度超过推特推文限制,如何分部分发布推文?
- javascript - 使用 Node.js fast-csv 库读取和写入 csv 文件