Insert Failing When PK is a SQL DB computed Column

Dear Experts
I am onboarding Radzen in our team, and currently doing some basic tests. While everything works as expected, we are struggling for several days on one problem. Although I am pretty much convinced that this is not radzen issue rather it has something to do with the behavior of Entity Framework Core etc etc... but still posting here because probably there are some .Net Expert here who can show me some light.

So, we have the following table (apparently very simple, but it has one Identity column, and another Computed column, which is also the Primary Key).
I generated code using Radzen blazor... and noticed, Update & Delete working absolutely fine but we are unable to insert any record.
We tried numerous options, like,     [DatabaseGenerated(DatabaseGeneratedOption.Identity)] for D_ID or     [DatabaseGenerated(DatabaseGeneratedOption.Computed)] for D_ID etc..... No Luck.

Everytime insert failing because (for 'God Knows what reason') Code is trying to create an insert statement where it is trying to insert D_ID...

If anyone can give it a quick try and find a solution/workaround - That will be sooooo much helpful. Thanks a lot in advance,





/****** Object:  Table [dbo].[TBL_NAME]    Script Date: 3/21/2023 2:42:50 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[TBL_NAME](
	[ID] [int] IDENTITY(1,1) NOT NULL,
	[D_ID]  AS ('DID'+right('000000'+CONVERT([varchar](6),[ID]),(6))) PERSISTED NOT NULL,
	[NAME] [varchar](50) NOT NULL,
	[IS_ENABLED] [bit] NULL,
	[ADDRESS] [varchar](2000) NULL,
 CONSTRAINT [Pk_TBL_NAME_D_ID] PRIMARY KEY CLUSTERED 
(
	[D_ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY],
 CONSTRAINT [UNQ_TBL_NAME_D_ID] UNIQUE NONCLUSTERED 
(
	[NAME] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO

ALTER TABLE [dbo].[TBL_NAME] ADD  CONSTRAINT [defo_TBL_NAME_IS_ENABLED]  DEFAULT ((1)) FOR [IS_ENABLED]
GO

Just tried your schema and Radzen properly identified this field as computed:



As you can see there are no editors for this field in Add/Edit pages.

Hi @enchev Thanks a lottt for trying my example.
Ofcourse Radzen correctly identified this D_ID as computed and not showing on Edit screen/add screen etc.... but are you able to Add? Have you really tried that one once please?
While Adding it is throwing error for all 4 developers of my team.. we all tried, exact same experience.

Please try to add a record using your add screen then hopefully you'll be able to replicate my error experience. Please give it one try...

PFB My situation ---

I've checked what model is generated for your schema and I've found the following:

[Table("TBL_NAME", Schema = "dbo")]
  public partial class TblName
  {
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int ID
    {
      get;
      set;
    }
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public string D_ID
    {
      get;
      set;
    }
    public string NAME
    {
      get;
      set;
    }
    public bool? IS_ENABLED
    {
      get;
      set;
    } = true;
    public string ADDRESS
    {
      get;
      set;
    }
  }

The first property is not primary key however it's marked as identity and the problematic field is a primary key and computed. Not sure what's the purpose of this setup however I'm unable to find how such table should be mapped in order to tell EF to perform inserts properly.
Let me know if you have suggestions.

I am too desperately looking for some work-around here. Regarding your question... (without getting too much into that - because it will deviate our main topic).. my short answer is: D_ID column will act as foreign key in it's child table. Therefore that column has to be a PK here and not "ID" column.

IMHO ID field should be the primary key since at the moment if you add [DatabaseGenerated(DatabaseGeneratedOption.Computed)] for the D_ID field EF will throw exception immediately:

[2023-03-21T16:23:10.593Z] Error: System.InvalidOperationException: The property 'D_ID' cannot be configured as 'ValueGeneratedOnUpdate' or 'ValueGeneratedOnAddOrUpdate' because it's part of a key and its value cannot be changed after the entity has been added to the store.

I've found also this:

You can search StackOverflow for more workarounds.

Again Thanks @enchev
I don't really have a choice to go and change entire database due to this problem of Entity Framework. We certainly have other ways to manage and build UI which probably will support this. It will be really great if some more people notice this and may be (!!) come up with some workaround. (Not changing the Database design)

Maybe you can use stored procedure to add records to such tables instead default EF approach.

1 Like

Yes. That's one of the options we are thinking as well.
How easy/difficult it is to (Not writing the SP - that one very easy for sure) incorporate such SP for Save button click action of the form here in Radzen?
Or is that something we have do completely in Visual Studio?

Suppose, I already have a SP as below...

CREATE PROCEDURE SP_ADD_ITEM_TBL_NAME 
(
	@NAME [varchar](50) NULL,
	@IS_ENABLED [bit] NULL,
	@ADDRESS [varchar](2000) NULL
)
AS
BEGIN
IF (@NAME IS NOT NULL)
	INSERT INTO  [dbo].[TBL_NAME] ([NAME], [IS_ENABLED], [ADDRESS])
		VALUES (@NAME, @IS_ENABLED, @ADDRESS)

END

Radzen will generate service method for this procedure if included on infer of the data source. You can change the CreateXXX service method call on form Submit event to your procedure method and pass needed parameters.

Thanks much. I'll give it a try and will get back to you... Really appreciate your help.
Very vey soon (ideally this week itself) we are going to onboard this tool, I am not sure if we have any option to directly reach out/call anyone for any quick training /overview of this tool (other than what we managed to experience in past few weeks with self try etc.)
We have various requirements but not sure whether /how some of this (probably) could be done easily with Radzen,

You might consider Radzen Blazor Studio where you have much more capabilities compared to Radzen IDE:
https://www.radzen.com/blazor-studio/

https://www.radzen.com/blazor-studio/documentation/differences-from-radzen/

Just a quick Update... using SP, I now managed to insert easily... now I am banging my head to the wall as the EF Core spoilt several days for us, trying to such a simple insert :slight_smile: :slight_smile:

Regarding Blazer Studio, unfortunately I haven't yet tried at all and now it might be a little last moment for procurement team to change the decision and they are coordinating with us for sometime. Although I will check tomorrow...

Some of the missing elements that I am concerned about anyways for all the products I reviewed by far...
(a) How to provide a better look and feel and most importantly how to easily customize GridView. I am tired of seeing only 10/15 records in entire page and each row heights are so big.
(b) Power BI embedding option
(c) Somewhat Excel like behavior of Grid. Specially Allowing Bulk Insert/Update/Delete
(d) Uploading of Excel Data Option
(e) Some better charting & reporting option,.. specially a Matrix Grid kind of thing.
etc....

May be few things could be done with some tools/including Radzen... I am yet to fully explore. I still believe if we could have some tool on-boarding training available from Product Team. But anyways....