我有一个Save对象,它有多个关联的集合。对象的总大小如下:
对象之间的关系可以从该映射中推断出来,并且在数据库中似乎得到了正确的表示。查询也很好。
modelBuilder.Entity<Save>().HasKey(c => c.SaveId).HasAnnotation("DatabaseGenerated",DatabaseGeneratedOption.Identity);
modelBuilder.Entity<Save>().HasMany(c => c.Families).WithOne(x => x.Save).HasForeignKey(x => x.SaveId);
modelBuilder.Entity<Save>().HasMany(c => c.Countries).WithOne(x => x.Save).HasForeignKey(x => x.SaveId);
modelBuilder.Entity<Save>().HasMany(c => c.Provinces).WithOne(x => x.Save).HasForeignKey(x => x.SaveId);
modelBuilder.Entity<Save>().HasMany(c => c.Pops).WithOne(x => x.Save).HasForeignKey(x => x.SaveId);
modelBuilder.Entity<Country>().HasOne(c => c.Save);
modelBuilder.Entity<Country>().HasMany(c => c.Technologies).WithOne(x => x.Country).HasForeignKey(x => new {x.SaveId, x.CountryId});
modelBuilder.Entity<Country>().HasMany(c => c.Players).WithOne(x => x.Country).HasForeignKey(x => new {x.SaveId, x.CountryId});
modelBuilder.Entity<Country>().HasMany(c => c.Families).WithOne(x => x.Country).HasForeignKey(x => new {x.SaveId, x.OwnerId});
modelBuilder.Entity<Country>().HasMany(c => c.Provinces).WithOne(x => x.Owner);
modelBuilder.Entity<Country>().HasKey(c => new { c.SaveId, c.CountryId });
modelBuilder.Entity<Family>().HasKey(c => new …Run Code Online (Sandbox Code Playgroud) c# entity-framework mariadb entity-framework-core ef-core-3.1
我正在制作一个复杂的应用程序(规划涉及:文章,销售,客户,制造,机器......),它使用ERP的SQL Server数据库提供的信息.
我使用了大约30个不同的相关对象,每个对象都将其信息存储在表/视图中.其中一些表有20k到100k的记录.
我需要将所有这些表转换为C#对象,以便将来无法在SQL中处理.我不需要所有的行,但是没有办法确定我需要哪些行,因为这将取决于运行时事件.
问题是关于这样做的最佳方式.我尝试了以下方法:
检索所有数据并使用a将其存储在DataSet中SqlDataAdapter,这在RAM中占用大约300mb.这里的第一个问题是:同步,但它是可以接受的,因为数据在执行期间不会发生太大变化.
然后我遍历每一行并将其转换为C#对象,存储在静态字典中以便通过密钥快速访问.问题在于创建如此多的对象(数百万)需要内存使用量高达1,4GB,这太过分了.除了内存,数据访问速度非常快.
因此,如果获取所有内存太多,我认为我需要某种laxy加载,所以我试过:
SqlDataReader我第一次需要的项目过滤来直接查询数据库,然后将它存储在静态字典中.这种方式的内存使用量是最小的,但这种方式很慢(分钟顺序)因为这意味着我需要像服务器似乎不喜欢的不同查询(低性能).最后,我尝试了一种有效的中间方法,但我不确定它是否是最佳的,我怀疑它不是:
第三种选择是填充DataSet包含所有信息并存储本地静态副本,但不将所有行转换为对象,只需按需执行(懒惰),如下所示:
public class ProductoTerminado : Articulo {
private static Dictionary<string, ProductoTerminado> productosTerminados = new Dictionary<string, ProductoTerminado>();
public PinturaTipo pinturaTipo { get; set; }
public ProductoTerminado(string id)
: base(id) { }
public static ProductoTerminado Obtener(string idArticulo)
{
idArticulo = idArticulo.ToUpper();
if (productosTerminados.ContainsKey(idArticulo))
{
return productosTerminados[idArticulo];
}
else
{
ProductoTerminado productoTerminado = new ProductoTerminado(idArticulo);
//This is where I get new …Run Code Online (Sandbox Code Playgroud)我有一个接口IArticle与几个实现:ProductType,RawMaterial,PaintType等.
然后我有我认为的参考(SKU),它是a IArticle和a 之间的复合元素Color:
public interface IReference
{
Color Color { get; set; }
IArticle Article { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
然后我有几个实现,每个实现都有一个相应的IArticle实现:
Product : IReference将有ProductType作为Article,SemifinishedGood: IReference将有RawMaterial作为ArticlePaint : IReference将有PaintType作为Article所以问题是......如何覆盖Article类型,如下所示:
public class Paint: IReference
{
public virtual Color Color { get; set; }
public virtual PaintType Article { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这样我就可以访问 …
我有一个C#应用程序将一些数据写入SQLite数据库,当我尝试将C#double存储到SQLite REAL字段时出现问题.
它用逗号作为小数分隔符将它保存到数据库中(像我国一样,不确定它是否有用).
该表具有以下结构:
CREATE TABLE `valoresStock` (
`Fecha` TEXT,
`IdArticulo` TEXT,
`IdColor` INTEGER,
`KgPorUnidad` REAL,
`Stock` REAL,
`CostePorKg` REAL,
`CostePorUnidad` REAL,
PRIMARY KEY(Fecha,IdArticulo,IdColor)
);
Run Code Online (Sandbox Code Playgroud)
我在我的应用程序中使用的查询如下:
cmd.CommandText = "INSERT INTO valoresStock (Fecha, IdArticulo, IdColor, KgPorUnidad, Stock, CostePorKg, costePorUnidad) VALUES ('" + DateTime.Today + "','" + referencia.idArticulo + "', '" + referencia.idColor + "', '" + referencia.kilos + "', '" + referencia.stockActual + "', '" + referencia.valorPorKg + "', '" + referencia.valorPorUnidad + "') ";
Run Code Online (Sandbox Code Playgroud)
正如我所说,最后四个字段是double类型.没有错误.
然后数据存储如下:

也就是说,当它没有小数部分时使用点,而当它没有时使用逗号.
这个问题是例如当我使用SELECT语句来计算时,例如: …