开发者

Entity Framework mistakes composite key with some varchar columns as identity

开发者 https://www.devze.com 2023-02-27 04:49 出处:网络
I have the following table: CREATE TABLE [dbo].[MyTable]( [timestamp] [timestamp] NOT NULL, [Col1] [varchar](20) NOT NULL,

I have the following table:

CREATE TABLE [dbo].[MyTable](
    [timestamp] [timestamp] NOT NULL,
    [Col1] [varchar](20) NOT NULL,
    [Col2] [varchar](20) NOT NULL,
    [col3] [datetime] NOT NULL,
    [col4] [varchar](10) NOT NULL,
    [Col5] [varchar](10) NOT NULL,
    [Col6] [varchar](10) NOT NULL,
    [Col7] [decimal](38, 20) NOT NULL,
    [Col8] [decimal](38, 20) NOT NULL,
    [Col9] [datetime] NOT NULL,
 CONSTRAINT [MyTable$0] PRIMARY KEY CLUSTERED 
(
    [Col1] ASC,
    [Col2] ASC,
    [Col3] ASC,
    [Col4] ASC,
    [Col5] ASC,
    [Col6] ASC,
    [Col7] ASC
) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON, FILLFACTOR = 90) ON [Data Filegroup 1]
) ON [Data Filegroup 1]

I 开发者_高级运维use the EF Generator to clreate the MyTable class, set all the fields and save changes. I get the following error:

Store-generated keys are only supported for identity columns. Key column 'Col2' has type 'SqlServer.varchar', which is not a valid type for an identity column.

What I don't understand is why it thinks it is an identity column. How would I fix that error? Am I looking in the right direction?


The problem seems to be that the error message is irrelevant. What it should say is something about non-nullable PK values. It seems that one of the values involved in the PK was null. I think this problem has to do with the String type in C# being nullable, since Nullable was set to False in the EF Designer and did not throw an error since it was simply not being set.

0

精彩评论

暂无评论...
验证码 换一张
取 消