Trying to implement EF for existing DB SQLSERVER 2012- I have VB application running and trying to convert EF base web API
When I insert into table InboundEquipment where int InboundEquipmentID = IDENTITY column. I also have Insert Trigger for this table which uses InboundEquipmentID to insert into another table Message214Status.
Database:
CREATE TABLE [dbo].[InboundEquipment](
[InboundEquipmentID] [bigint] IDENTITY(1,1) NOT NULL,
...
CREATE TABLE [dbo].[Message214Status](
[InboundEquipmentID] [bigint] NOT NULL,
...
ALTER TRIGGER [dbo].[InboundEquipment] ON [dbo].[InboundEquipment] FOR INSERT AS
DECLARE
@biInbndEquip_ID BIGINT,
@iCust_ID INT,
...
SELECT @biInbndEquip_ID= InboundEquipmentID,@iCust_ID= c.Cust_ID, ...
FROM
INSERTED I
JOIN sometable c WITH(NOLOCK)ON ...
INSERT INTO dbo.Message214Status (InbndEquip_ID, Cust_ID) VALUES
(@biInbndEquip_ID,@iCust_ID )
...
public partial class InboundEquipment
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public long InboundEquipmentID { get; set; }
...
When DBContext.db.SaveChanges(); fails with exception :
InnerException {"Cannot insert the value NULL into column 'InboundEquipmentID', table 'dbo.Message214Status'; column does not allow nulls. INSERT fails.
\r\nThe statement has been terminated."} System.Exception {System.Data.SqlClient.SqlException}
This insert into Message214Status using identity value from table InboundEquipment
Basically Insert trigger fails, I cannot change database objects since there existing application running on this db.
Note that, Database is created with NO PK and FK relation as business requirement and K and FK relation is maintained withing the VB code (OLD application)
The error message seems to clearly state the problem. But I suggest you post your question to Stack Overflow, where you are likely to get a detailed answer.
Trying to implement EF for existing DB SQLSERVER 2012- I have VB application running and trying to convert EF base web API
When I insert into table InboundEquipment where int InboundEquipmentID = IDENTITY column. I also have Insert Trigger for this table which uses InboundEquipmentID to insert into another table Message214Status.
Database: CREATE TABLE [dbo].[InboundEquipment]( [InboundEquipmentID] [bigint] IDENTITY(1,1) NOT NULL, ...
CREATE TABLE [dbo].[Message214Status]( [InboundEquipmentID] [bigint] NOT NULL, ...
ALTER TRIGGER [dbo].[InboundEquipment] ON [dbo].[InboundEquipment] FOR INSERT AS DECLARE @biInbndEquip_ID BIGINT, @iCust_ID INT, ... SELECT @biInbndEquip_ID= InboundEquipmentID,@iCust_ID= c.Cust_ID, ... FROM INSERTED I JOIN sometable c WITH(NOLOCK)ON ... INSERT INTO dbo.Message214Status (InbndEquip_ID, Cust_ID) VALUES (@biInbndEquip_ID,@iCust_ID ) ...
CODE:
db.InboundEquipment.Add(ibData); try { db.SaveChanges(); var IBEquipID = ibData.InboundEquipmentID; } catch (Exception ex) { return ResponseMessage(Request.CreateErrorResponse (HttpStatusCode.InternalServerError, "ERROR:" + ex.Message)); }
public partial class InboundEquipment { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public long InboundEquipmentID { get; set; } ... When DBContext.db.SaveChanges(); fails with exception : InnerException {"Cannot insert the value NULL into column 'InboundEquipmentID', table 'dbo.Message214Status'; column does not allow nulls. INSERT fails. \r\nThe statement has been terminated."} System.Exception {System.Data.SqlClient.SqlException}
This insert into Message214Status using identity value from table InboundEquipment Basically Insert trigger fails, I cannot change database objects since there existing application running on this db. Note that, Database is created with NO PK and FK relation as business requirement and K and FK relation is maintained withing the VB code (OLD application)
When Disable the insert trigger and Save is OK
Please help! Raj