Search code examples
c#sqlwinformslinqsql-server-ce

Linq db.submitchanges() not working


I am facing issue in submit changes in table using linq code executed successfully by it is not saving changes in table.

Some one says your table must have primary key to submit changes but my table have primary key.

Some one says use integrated security = true to but it generate exception invalid keyword integrated security

Here is my connection string

<add name="POSConnectionString" connectionString="Persist Security Info=False; Data Source=|DataDirectory|\App_Data\POS.sdf; mode=Read Write;"
    providerName="Microsoft.SqlServerCe.Client.4.0" /> 

Code to insert data in table

public bool InsertItem(Item item)
{
    try
    {
        using (SqlCeConnection conn = new SqlCeConnection(connectionString))
        {
            using (POSContext db = new POSContext(conn))
            {                        
                db.Items.InsertOnSubmit(item);
                db.SubmitChanges();
                return true;
            }
        }
    }
    catch (Exception ex)
    {
        return false;
    }

}

Here is my table script

CREATE TABLE [Item] (
  [ItemID] int IDENTITY (1,1) NOT NULL
, [ItemName] nvarchar(100) NOT NULL
, [ItemCode] nvarchar(100) NOT NULL
, [Weight] int NOT NULL
, [IsActive] bit NOT NULL
, [Stock] int NULL
, [UnitID] int NULL
);
GO
ALTER TABLE [Item] ADD CONSTRAINT [PK_Item] PRIMARY KEY ([ItemID]);
GO

I am working in windows form application development Database Sql server compact database 4.0 Visual studio 2012


Solution

  • I got the solution by changing database properties to don't copy and it works as it making a copy of database in bin/debug and saving changes there