Joschi
Joschi
CC#
Created by Jiry_XD on 11/1/2024 in #help
Reset identity colums EF Core error.
I mean SeedTableX().
17 replies
CC#
Created by Jiry_XD on 11/1/2024 in #help
Reset identity colums EF Core error.
Maybe try to create a new dbContext just before your seed methods? Because the error sounds a bit like efCore getting confused. But I'm just guessing here.
17 replies
CC#
Created by Jiry_XD on 11/1/2024 in #help
Reset identity colums EF Core error.
Ohh sorry I somehow assumed it was for tests.
17 replies
CC#
Created by Jiry_XD on 11/1/2024 in #help
Reset identity colums EF Core error.
The third option could be, to just not reset your identity columns and write your tests in a way that are agnostic of specific ids. Which should be the case in any way.
17 replies
CC#
Created by Jiry_XD on 11/1/2024 in #help
Reset identity colums EF Core error.
You could consider alternatives to what you are doing now. Both are more or less a variant on "don't manually try to reset your database state". The first would be to call context.EnsureDeleted() followed by context.EnsureCreated() at the start of your tests. This would delete the database and then create one from your code first context. (Note this does not use migrations, it just creates it from the existing config directly). The second option would be to use test containers, which creates a fresh DB as a docker container for your tests. MS has a whole series on articles with guidance on how to test applications using efcore https://learn.microsoft.com/en-us/ef/core/testing/
17 replies
CC#
Created by whatsinaname on 11/1/2024 in #help
✅ What is the free alternative to jet brains resharper ?
Renaming is something that every IDE can do.
32 replies
CC#
Created by Caleb Weeks on 10/28/2024 in #help
Data fetching in components?
If you create a new one every time you should just use the IDbContextFactory directly.
13 replies
CC#
Created by Caleb Weeks on 10/28/2024 in #help
Data fetching in components?
If you use prerendering, then OnParameterSet will always be called twice. https://github.com/dotnet/aspnetcore/issues/30037 It will also always be rerun, when blazor believes the parameter may have changed. If your db call does not depend on the parameter I suggest moving it into OnInitializedAsync after the call to the base method. If it has to be in OnParameterSet you could create a CancellationToken using a CancellationTokenSource and cancel the previous db call. Or just create a new DbContext.
13 replies
CC#
Created by Caleb Weeks on 10/28/2024 in #help
Data fetching in components?
In Blazor partial is also used for codebehind files mycomponent.razor.cs. That is possible because the .razor files are actually being translated into a C# class behind the scenes by a source generator.
13 replies
CC#
Created by Caleb Weeks on 10/28/2024 in #help
Data fetching in components?
partial allows you to split a class between different files. Like you could now go into a diferent .cs file, redeclare the class and add some properties or methods. Most of the time it is only done to allow source generators to extend your class. protected as an accessibility modifier means "Only I and inheriting classes can access this variable". So that already accomplishes your goal.
13 replies
CC#
Created by Caleb Weeks on 10/28/2024 in #help
Data fetching in components?
It's probably not important, but why is your class partial? Also could you share a minimal example, of how to produce that error?
13 replies
CC#
Created by Caleb Weeks on 10/28/2024 in #help
Data fetching in components?
This is what I'm using
c#
public abstract class UnitOfWorkComponentBase : ComponentBase, IDisposable, IAsyncDisposable
{
[Inject]
public required IDbContextFactory<SeasonContext> ContextFactory { get; init; }

protected SeasonContext Context { get; set; } = null!;

protected override async Task OnInitializedAsync()
{
Context = await ContextFactory.CreateDbContextAsync();
await base.OnInitializedAsync();
}

public void Dispose()
{
Context.Dispose();
}

public async ValueTask DisposeAsync()
{
await Context.DisposeAsync();
}
}
c#
public abstract class UnitOfWorkComponentBase : ComponentBase, IDisposable, IAsyncDisposable
{
[Inject]
public required IDbContextFactory<SeasonContext> ContextFactory { get; init; }

protected SeasonContext Context { get; set; } = null!;

protected override async Task OnInitializedAsync()
{
Context = await ContextFactory.CreateDbContextAsync();
await base.OnInitializedAsync();
}

public void Dispose()
{
Context.Dispose();
}

public async ValueTask DisposeAsync()
{
await Context.DisposeAsync();
}
}
And then you can just inherit it in your component with the @inherits directive. But you are responsible to ensure that the user cannot trigger two actions at the same time. For example if you have two buttons, both resulting in a database call, it would throw a concurrency exception, if the user clicks both buttons in rapid succession, before the first database call finishes.
13 replies
CC#
Created by Saiyanslayer on 10/25/2024 in #help
✅ Better way to set a property using linq?
You can, but you need to capture the variable in the loop.
foreach(var item in items){
var capturedItem = item;
<Component @bind="capturedItem'/>
}
foreach(var item in items){
var capturedItem = item;
<Component @bind="capturedItem'/>
}
6 replies
CC#
Created by Saiyanslayer on 10/25/2024 in #help
✅ Better way to set a property using linq?
Why don't you use two way binding?
6 replies
CC#
Created by Pedro Gil Mora on 10/7/2024 in #help
Dictionary vs switch in source generated code
You meant switch?
52 replies
CC#
Created by Pedro Gil Mora on 10/7/2024 in #help
Dictionary vs switch in source generated code
You use an immutable dictionary here. These are actually significantly slower in lookup time compared to frozen dictionaries. Iirc correctly they are even slower than normal dictionaries.
52 replies
CC#
Created by Pedro Gil Mora on 10/7/2024 in #help
Dictionary vs switch in source generated code
Something seems off about those tests. Would you mind sharing the test code?
52 replies
CC#
Created by Merineth 🇸🇪 on 10/7/2024 in #help
✅ Delegates. What are they and what are they used for?
Yes VS colors them green by default.
375 replies
CC#
Created by Daiko Games on 9/30/2024 in #help
How to convert a whole json file to C# classes with Newtonsoft.Json?
Yes, because that is not how you deal with Json. You want to use deserialization. https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/deserialization
43 replies
CC#
Created by Daiko Games on 9/30/2024 in #help
How to convert a whole json file to C# classes with Newtonsoft.Json?
Just to be sure. You are not actually working on and parsing the content of the file line by line?
43 replies