Sparky
Sparky
CC#
Created by Sparky on 1/24/2023 in #help
❔ Is there an easy way to serialize a Dictionary<string, string> to XML nodes?
I'm writing a program that has to produce the following XML:
<Attributes>
<Color>red</Color>
<Size>small</Size>
<Material>plastic</Material>
</Attributes>
<Attributes>
<Color>red</Color>
<Size>small</Size>
<Material>plastic</Material>
</Attributes>
I'm trying to figure out the best data model that can be serialized to something like this. The most obvious choice seems to be a Dictionary<string, string>, but I can also use a custom class with two properties (Name, Value). I've seen some solutions on stack overflow, but all of them involve creating a bunch of helper classes and writing hundreds of lines of additional code, and I refuse to believe that there is no simpler way of doing this, with Xml attributes or something like that.
11 replies
CC#
Created by Sparky on 12/16/2022 in #help
❔ How do I resolve a 'package downgrade' if one of my dependencies needs an older version of a dll?
I'm developing an app that uses the newest EntityFramework Core 7 (.NET 7) to communicate with its own database. One of the libraries in this app needs to communicate with an external Firebird database. The problem is, that the official Firebird provider for EF Core doesn't support EF Core 7 yet, only EF Core 6, and this leads to a package incompatibility issue between the main project and the Firebird module. As far as I can tell, I have two options: 1) Rewrite all functionality of the Firebird plugin by using the native FIrebird client, with raw SQL strings (so it no longer uses EF Core) 2) Somehow figure out a way to let these two different versions of EF Core (6.0.12 and 7.0.1) co-exist in my application so I don't have to rewrite anything In this case I'm hoping to go with option 2, but I'm not sure if it's even possible, and I need some help. All packages are resolved from Nuget.
5 replies
CC#
Created by Sparky on 10/20/2022 in #help
EF Core is throwing DbUpdateConcurrencyException, but only on the production database (MySQL)
I am trying to deploy a web application to a server, but I'm running into a weird issue. There is a schedules task in the app, that downloads some data from a different server, and inserts a large amount of rows into a database table. On my local development machine (Windows 11, MySQL 8.0.31) this works without any issues. But when I connect it to the production database (MySQL 8.0.30-0ubuntu0.20.04.2), this specific part of the application fails by throwing a DbUpdateConcurrencyException. Here's the simplified version of the code in question:
public async Task Execute(IJobExecutionContext context)
{
// databaseFactory is injected as a dependency (IDbContextFactory<AppDatabase>)
await using var database = await databaseFactory.CreateDbContextAsync();

int page = 0;
while(true)
{
List<WarehouseEntry> dbItems = warehouse.QueryPage(page, 1000); // Retrieves 1000 entries from somewhere
if(dbItems.Count == 0)
break;

database.ProductWarehouseCache.AddRange(dbItems.Select(p => new CachedWarehouseProduct {
Sku = p.Name,
Barcode = p.Barcode,
Stock = p.Stock,
Price = p.Price
}));

await database.SaveChangesAsync(); // <---- DbUpdateConcurrencyException is thrown here
page++;
}
}
public async Task Execute(IJobExecutionContext context)
{
// databaseFactory is injected as a dependency (IDbContextFactory<AppDatabase>)
await using var database = await databaseFactory.CreateDbContextAsync();

int page = 0;
while(true)
{
List<WarehouseEntry> dbItems = warehouse.QueryPage(page, 1000); // Retrieves 1000 entries from somewhere
if(dbItems.Count == 0)
break;

database.ProductWarehouseCache.AddRange(dbItems.Select(p => new CachedWarehouseProduct {
Sku = p.Name,
Barcode = p.Barcode,
Stock = p.Stock,
Price = p.Price
}));

await database.SaveChangesAsync(); // <---- DbUpdateConcurrencyException is thrown here
page++;
}
}
I am testing this from my local dev machine, so the only difference between crash and no crash is the connection string to the MySQL database, the rest of the environment is the same (I can access the remote DB through an SSH tunnel). I have a feeling that this issue has to do with the MySQL configuration on the prod server, but I can't figure out what it is. Despite the error, there is no actual concurrence, because this code doesn't run multiple times in parallel, and nothing else is reading/writing from/to the database while the app is running. What could be the cause of this exception, and why does it only happen on a linux-based MySQL installation?
3 replies
CC#
Created by Sparky on 10/18/2022 in #help
How to detect if my ASP.Core app is running from the ef-core tool in Design time?
I'm running into a weird issue where the dotnet ef tool keeps trying to start up my web app, instead of using the DesignTimeDbContextFactory that is implemented in the same project. This wouldn't be a big issue, but every time I run an ef tool, it dumps a huge StopTheHostException on the console, because of course it fails to fully load the web app, and this is kind of annoying. Is there a way to detect if the program was launched by dotnet ef in my Main() somehow, so I can skip the web app startup?
1 replies
CC#
Created by Sparky on 8/24/2022 in #help
How can I sort a list of entries (Id, ParentId) into a tree hierarchy? [Answered]
I have an unsorted list of entries with two notable fields: Id and ParentId. I would like to sort this data in such a way that if an item has a non-null ParentId, it ends up underneath the parent item with the corresponding Id. The ordering of items at a particular level (e.g. the direct children of a parent) has to be increasing. The ordering or "root" items (ParentId == null) also has to be increasing. Both Id and ParentId are of type int?. The data hierarchy can be any number of levels deep. I've tried every LINQ solution from StackOverflow, including a custom one that uses a for loop:
var categories = woo.Category.Enumerate().OrderBy(c => c.id).ToList(); // Source data
var ordered = new ObservableCollection<ProductCategory>(categories);

for (int i = 0; i < ordered.Count; i++)
{
var category = ordered[i];
if (category.parent is null or 0)
ordered.Move(i, 0); // Move root item to the top, shift everything down
else if (ordered.FirstOrDefault(o => o.id == category.parent) is { } parent && ordered.IndexOf(parent) is { } parentIndex and > 0)
ordered.Move(i, parentIndex + 1); // Move item under its parent, shift everything down
}
var categories = woo.Category.Enumerate().OrderBy(c => c.id).ToList(); // Source data
var ordered = new ObservableCollection<ProductCategory>(categories);

for (int i = 0; i < ordered.Count; i++)
{
var category = ordered[i];
if (category.parent is null or 0)
ordered.Move(i, 0); // Move root item to the top, shift everything down
else if (ordered.FirstOrDefault(o => o.id == category.parent) is { } parent && ordered.IndexOf(parent) is { } parentIndex and > 0)
ordered.Move(i, parentIndex + 1); // Move item under its parent, shift everything down
}
but nothing gives the desired result. I only have to do this once to sort a dataset, so performance and efficiency do not matter at all.
8 replies