I created a column in a table named [Id] and made its type nvarchar(50), and its value is automatically by newid(), which is a mixture of letters and numbers, when adding a new record inside the database through a (sql server managment) , the new number does not appear until I execute the table.
The problem is not here, the real problem is when creating a new record from within EF6, the record cannot be added without specifying the identity value, and the following message appears:
The key field 'Id' cannot have a value of null. A non-null value is required for the key fields defined on type 'Booking'.
this code for sqlserver table
CREATE TABLE [dbo].[TableName](
[id] [varchar](10) NOT NULL,
[Price] [nvarchar](10) NULL,
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]
GO
ALTER TABLE [dbo].[TableName] ADD DEFAULT (concat(left(newid(),(4)),'-',abs(checksum(newid()))%(10000))) FOR [id]
GO
In Ef6 Use Code
context Db = new context ();
t TableName = new TableName{
Price=10
};
db.TableName.add(t);
db.SaveChanges();