EF keeps telling me there is no InsertFunction for entity from DB that has Identity set?
-
26-02-2021 - |
문제
Application uses Entity Framework 4.1 with database first approach.
I have in database a simple table for logging purposes:
CREATE TABLE [dbo].[LogEntrys](
[LogEntryID] [bigint] IDENTITY(1,1) NOT NULL,
[Timestamp] [datetime] NULL,
[Message] [varchar](5000) NULL,
[IP] [varchar](50) NULL,
[UserName] [varchar](256) NULL,
[Controller] [varchar](50) NULL,
[Action] [varchar](50) NULL
)
It translates ok in model into EntitySet 'LogEntrys' and Entity LogEntry. Problem is, when i try to insert something into the table from applicatio i get the following error:
Unable to update the EntitySet 'LogEntrys' because it has a DefiningQuery and no element exists in the element to support the current operation.
I have seen similar posts on SO and i have tried to solve it by editing the XML edmx file like explained in this tip: MSDN blog tip 34, I have removed the Defining query. The key was already set up as it should be.
but then i get the following error:
"Invalid object name 'AMSModelStoreContainer.LogEntrys'."
I am not sure what is the problem, and/or what i am doing wrong?
any ideas?
Thank you.
해결책
The problem is that the no field is set as primary key create your table like this
CREATE TABLE [dbo].[LogEntrys](
[LogEntryID] [bigint] IDENTITY(1,1) NOT NULL,
[Timestamp] [datetime] NULL,
[Message] [varchar](5000) NULL,
[IP] [varchar](50) NULL,
[UserName] [varchar](256) NULL,
[Controller] [varchar](50) NULL,
[Action] [varchar](50) NULL,
CONSTRAINT [PK_LogEntrys] PRIMARY KEY CLUSTERED
(
[LogEntryID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
and then update your model. You might need to completely remove the old one entry you had for it in your .edmx and re=add it now that you have tinkered with it.