EF Core 7.0 with a SQL Table (DB First) having an Identity Column and a Computed Column (Primary Key)

amjee84 6 Reputation points
2023-03-15T17:23:05.2+00:00

Hi There,

I have a fairly simple table in SQL DB as below

[dbo].TBL_CUST

Column Comments
ID Int; Identity (1, 1)
D_ID Computed column Calculated as = "DID" + RIGHT('000' + Cast(ID as varchar(3)) , 3) i.e.... Like DID001 / DID019 etc...
Name Varchar (100); Nullable
Address Varchar(500); Nullable

There is one After Trigger for this Table which captured all the deleted / inserted / updated record....

I'm doing a CRUD from EF Core 7 as below....

Model Looks like this

namespace Cust.Models.dsCust
{
  [Table("TBL_CUST", Schema = "dbo")]
  public partial class dboTblCust
  {
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int ID
    {
      get;
      set;
    }
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
    public string DID
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public string NAME
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public string ADDRESS
    {
      get;
      set;
    }
    [ConcurrencyCheck]
    public string IS_ACTIVE
    {
      get;
      set;
    }
  }
}

And DBContext has below

  builder.Entity<Cust.Models.dsCust.dboTblCust>()
              .ToTable(tb => tb.HasTrigger("trigger"))
              .Property(p => p.IS_ACTIVE)
              .HasDefaultValueSql("((1))").ValueGeneratedNever();

Although I am able to view and update data from UI but I am unable to insert any record. Everytime (despite setting DID as database generated column), the insert query from EF Core 7 is trying to insert data from UI and eventually throwing error.

What other Options I tried (Needless to mention, none worked)

  1. Setting the following for DID column (instead of DatabaseGeneratedOption.Computed)
   DatabaseGeneratedOption.Identity
  1. Setting private set; in DID column
  2. Adding the following in DBContext
  builder.Entity<Cust.Models.dsCust.dboTblCust>()
              .ToTable(tb => tb.HasTrigger("trigger"))
              .Property(p => p.IS_ACTIVE)
              .HasDefaultValueSql("((1))").ValueGeneratedNever();

  builder.Entity<Cust.Models.dsCust.dboTblCust>()
              .Ignore(p => p.DID);

I already am experiencing the difficulty with EF Core 7 for Tables with Trigger & Computed Column. But has anyone came across such and managed any work around already, if yes kindly provide me some help here please.

Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
697 questions
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,374 questions
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,165 questions
Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,390 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,247 questions
0 comments No comments
{count} vote

1 answer

Sort by: Most helpful
  1. Karen Payne MVP 35,036 Reputation points
    2023-03-15T21:06:47.45+00:00

    Consider seeing the following project which is similar to what you are doing in regards to computed columns, article here. It may or may not serve your purpose but it shows adding, modify and view.