0

Problems when saving object in LINQ/C#

when i try to save a new record i get this error:

Cannot insert the value NULL into column 'EmpresaID', table 'mydb.dbo.Empresa'; column does not allow nulls. INSERT fails.
The statement has been terminated.

Was Made the table with SQL Server Management Studio 2012

USE [mydb]
GO

/****** Object:  Table [dbo].[Empresa]    Script Date: 25/04/2012 03:40:00 p.m. ******/
DROP TABLE [dbo].[Empresa]
GO

/****** Object:  Table [dbo].[Empresa]    Script Date: 25/04/2012 03:40:00 p.m. ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[Empresa](
    [EmpresaID] [int] NOT NULL,
    [RazonSocial] [nvarchar](210) NOT NULL,
    [Nit] [nchar](20) NOT NULL,
 CONSTRAINT [PK_Empresa] PRIMARY KEY CLUSTERED 
(
    [EmpresaID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

i made a class for handle the data

[Table(Name = "Empresa")]
class Empresa
{
    private int _EmpresaID;
    private string _RazonSocial;
    private string _Nit;

    [Column(DbType = "int", IsPrimaryKey = true, IsDbGenerated = true)]
    public int EmpresaID
    {
        set { _EmpresaID = value; }
        get { return _EmpresaID; }
    }

    [Column(DbType = "nvarchar(210)", CanBeNull = false)]
    public string RazonSocial
    {
        set { _RazonSocial = value; }
        get { return _RazonSocial; }
    }

    [Column(DbType = "nchar(20)", CanBeNull = false)]
    public string Nit
    {
        set { _Nit = value; }
        get { return _Nit; }
    }

}

made and which method is this:

Entity.Empresa empresa = new Entity.Empresa() {                
    Nit = NuevaEmpresaNitTxtBox.Text,
    RazonSocial = NuevaEmpresaRazonSocialTxtBox.Text
};

MyDataContext.GetTable<Entity.Empresa>().InsertOnSubmit(empresa);
MyDataContext.SubmitChanges();

can tell me was worng with my code?

1
  • You should probably set DbType="Int NOT NULL" on the EmpresaID property since that'd be (more) correct. Commented Apr 25, 2012 at 21:04

4 Answers 4

1

You forgot to declare Identity on EmpressaId. :)

Sign up to request clarification or add additional context in comments.

1 Comment

It's really tricky this Management Studio. that problem to modify a table
1

Your mapping says EmpresaID is generated by the database (IsDbGenerated = true), however the table definition does not generate ids for the associated table.

You probably want to change the column type of EmpresaID to IDENTITY so it will generate primary key values.

Comments

1

You are not setting the EmpresaID.

Either set it explicitly in your code, or alter your SQL TABLE to add IDENTITY to your EmpresaID column.

Comments

1

You never give an ID for your primary key. There is two solution:

  • Give it an explicit ID from your code (you'll need to make sure it's always unique)
  • Add the key word IDENTITY on line [EmpresaID] [int] NOT NULL to make your id field an INT IDENTITY that will have a unique value assigned at each insert.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.