absolution183
absolution183
CC#
Created by absolution183 on 3/8/2023 in #help
❔ Using a Custom JSON Converter on NswagStudio's Generated C# Client
Does anyone know how I can make NswagStudio use a custom json converter that I made on the generated C# client?
public class CustomDateTimeOffsetConverter : DateTimeConverterBase
{
public override object ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer)
{
DateTimeOffset? value = null;

if (string.IsNullOrEmpty(reader.Value?.ToString())
|| !DateTimeOffset.TryParse(reader.Value?.ToString(), out DateTimeOffset output))
{
return value;
}

if (output < DateTimeOffset.MaxValue)
{
value = output;
}

return value;
}

public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer)
{
if (string.IsNullOrEmpty(value?.ToString())
|| !DateTimeOffset.TryParse(value.ToString(), out DateTimeOffset output)
|| (DateTimeOffset.TryParse(value.ToString(), out output) && output == DateTimeOffset.MaxValue))
{
writer.WriteNull();
}

writer.WriteValue(value);
}
}
public class CustomDateTimeOffsetConverter : DateTimeConverterBase
{
public override object ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer)
{
DateTimeOffset? value = null;

if (string.IsNullOrEmpty(reader.Value?.ToString())
|| !DateTimeOffset.TryParse(reader.Value?.ToString(), out DateTimeOffset output))
{
return value;
}

if (output < DateTimeOffset.MaxValue)
{
value = output;
}

return value;
}

public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer)
{
if (string.IsNullOrEmpty(value?.ToString())
|| !DateTimeOffset.TryParse(value.ToString(), out DateTimeOffset output)
|| (DateTimeOffset.TryParse(value.ToString(), out output) && output == DateTimeOffset.MaxValue))
{
writer.WriteNull();
}

writer.WriteValue(value);
}
}
4 replies
CC#
Created by absolution183 on 1/20/2023 in #help
❔ Updating an entity/record with 1 query(?) on Entity Framework
My senior told me that there's a way to select and update something in EF in 1 query. So instead of this:
void MyMethod(int id)
{
BundleRecord bundle = await databaseContext.Bundles.TagWithCallerName().SingleOrDefaultAsync(x => x.Id == id);
bundle.ScanUploaded = true;

await databaseContext.SaveChangesAsync();
}
void MyMethod(int id)
{
BundleRecord bundle = await databaseContext.Bundles.TagWithCallerName().SingleOrDefaultAsync(x => x.Id == id);
bundle.ScanUploaded = true;

await databaseContext.SaveChangesAsync();
}
He said, if he remembers correctly, I should use the Attach() method. I've been Googling, and this is how I understood how to use this method:
void MyMethod(int id)
{
BundleRecord newBundle = new()
{
Id = id,
ScanUploaded = true // eliminates the "bundle.ScanUploaded = true;" line?
};

databaseContext.Bundles.Attach(newBundle).State = EntityState.Modified;

await databaseContext.SaveChangesAsync();
}
void MyMethod(int id)
{
BundleRecord newBundle = new()
{
Id = id,
ScanUploaded = true // eliminates the "bundle.ScanUploaded = true;" line?
};

databaseContext.Bundles.Attach(newBundle).State = EntityState.Modified;

await databaseContext.SaveChangesAsync();
}
Just wondering if my approach and understanding of the Attach() method are correct.
15 replies