This is my T-Sql
CREATE TABLE [dbo].[Details](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](max) NULL,
[SId] [int] NULL,
[PId] [int] NULL,
[Detail] [nvarchar](max) NULL,
[Documents] [int] NULL,
[Mobile_No] [int] NULL,
[IsActive] [bit] NULL,
[Status] [nvarchar](max) NULL,
CONSTRAINT [PK_dbo.GrievanceDetails] PRIMARY KEY CLUSTERED
(
[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]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
I want to take the Id and concat it with the word Agri
For example if my Id is 1 then it should br 1Agri
How can i do this?
Naimish MakwanaPosted Mar 8, 2023, 7:14 AM
Try below
In this example, a new property called
AgriIdis added to theDetailsclass. The[NotMapped]attribute is used to indicate that this property should not be mapped to a column in the database. Instead, this property is computed based on the value of theIdproperty using string concatenation.Now, every time you retrieve a
Detailsobject from the database, you can access theAgriIdproperty to get the concatenated value ofIdand "Agri". Note that sinceAgriIdis a computed property, you cannot set its value directly. If you need to update the value ofId, you should update theIdproperty directly and theAgriIdproperty will be automatically updated accordingly.Thanks
Sachin SinghPosted Mar 8, 2023, 9:22 AM
@Garima
On ModelCreating is not related to webform or MVC, it is related to EF, so if you are using EF then you must have this method inside DbContext class.
Garima BansalPosted Mar 8, 2023, 9:18 AM
Sachin Singh
I am not working on Asp.net MVC, its a webform project
So there is no OnModelCreating() method in it
Sachin SinghPosted Mar 8, 2023, 8:55 AM
You can not make an int column computed varchar , you need to modify the Id column to be a varchar or you need to introduce a new column as
later on use AgriID instead of Id in your project
Tuhin PaulPosted Mar 8, 2023, 8:47 AM
You can use the CONCAT function in T-SQL to concatenate the 'Id' column with the string 'Agri' to get the desired output. Here's an example:
This will return a new column 'NewId' with the concatenated value of 'Id' and 'Agri', along with the other columns from the 'Details' table. If you want to update the 'Id' column in the table with the new concatenated values, you can use the following UPDATE statement:
This will update the 'Id' column in the 'Details' table with the new concatenated values.
Garima BansalPosted Mar 8, 2023, 7:10 AM
Naimish Makwana
if i create it with model then how
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string? Name { get; set; }
public int? SId { get; set; }
public int? PId { get; set; }
public string? Details { get; set; }
public int? Documents { get; set; }
public int? MobileNo { get; set; }
public bool? IsActive { get; set; }
public string? Status { get; set; }
Naimish MakwanaPosted Mar 8, 2023, 7:04 AM
Hello Garima,
You can use the
CONCAT()function to concatenate the string "Agri" with theIdcolumn. Example add new columnAgriIdto the table,Thanks
Naimish Makwana