EF Core and ObservableCollection: modifieentity is marked as "Added" instead of

Posted by: sviluppo on 21 June 2023, 8:32 am EST

  • 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();
        }
    }
  • Posted 23 June 2023, 7:57 am EST

    Hi,

    We would need a sample to investigate the issue at our end. Could you kindly share us a minimal working sample with your implementation so that we could investigate the issue at our end and could assist you accordingly?

    If the sample contains confidential data, you may create a support ticket on our private portal and share the sample at: https://www.grapecity.com/my-account/my-support

    Regards,

    Ankit

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels