Posted 21 June 2023, 8:32 am EST
I’m developing a Blazor Server app with .NET 7, EF Core and C1 FlexGrid object.
I’m having this issue: if I try to edit a value of an existing row in the FlexGrid when I do the save command the Entity of that row has the “Added” state instead of “Modified”. And then it throws an exception saying that the primary key value already exist in the table, obviously!
I can add new rows and delete them without problems.
I tried to bypass the ObservableCollection and use directly MyDbContext.IVA.Local as FlexGrid’s ItemsSource. Edit existing rows worked but new\delete not.
Is a problem of the FlexGrid component? Or some fault in my code? It’s my first time using .NET7 and Entity Framework.
<button @onclick="Save">Salva</button>
<FlexGrid @ref="grid" ItemsSource="list" AutoGenerateColumns="false" NewRowPosition="GridNewRowPosition.Bottom" NewRowPlaceholder="Doppio click qui per aggiungere una nuova riga">
<FlexGridRows>
<GridFilterRow Placeholder="Cerca" AutoComplete="true"/>
</FlexGridRows>
<FlexGridColumns>
<GridColumn Header="Codice" Binding="Codice" />
<GridColumn Header="Descrizione" Binding="Descrizione" />
<GridColumn Header="Aliquota IVA" Binding="Aliquota" />
<EnumDropDownColumn T="Natura" Header="Natura" Binding="Natura" />
# </FlexGridColumns>
</FlexGrid>
@code {
FlexGrid grid;
ObservableCollection<IVA> list;
protected override void OnInitialized()
{
MyDbContext.IVA.Load();
list = MyDbContext.IVA.Local.ToObservableCollection();
}
private void Save()
{
grid.FinishRowEditing();
MyDbContext.SaveChanges();
}
}