Linq DataContext InsertOnSubmit statement Inserts only NULL values in the table

1.2k views Asked by At

Please help!!!

I have this simple piece of code, I don't have a clue why is not doing what is supposed to do which is insert the data contained in the class, I have not idea what am I doing wrong, cause the insert statement produces only NULL values in the table.

This is the code:

    public void InsertData()
    {
        using (CTM dc = new CTM(conn))
        {
            //Table<ValidCCGpn> validCCGpn = dc.GetTable<ValidCCGpn>();

            for (int i = 0; i < 10; i++)
            {
                dc.ValidCCGpn.InsertOnSubmit(
                    new ValidCCGpn
                        {
                            Period = 201209,
                            GPN = "gpn" + i.ToString(),
                            CC_GPN = "cc_gpn" + i.ToString()
                        });
            }
            dc.SubmitChanges();
        }

    }

And this is my DataContext and the and table:

public partial class CTM : DataContext
{
    public CTM(string conn) : base(conn) { }

    public Table<ValidCCGpn> ValidCCGpn;
}


[Table(Name = "CAT.ValidCCGpn")]
public class ValidCCGpn
{
    [Column(IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert)]
    public int Id { get; set; }

    public int Period { get; set; }
    public string GPN { get; set; }
    public string CC_GPN { get; set; }

}

This is the sql script to create the table:

    CREATE TABLE [CAT].[ValidCCGpn](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [Period] [int] NULL,
    [GPN] [varchar](50) NULL,
    [CC_GPN] [varchar](50) NULL,
 CONSTRAINT [PK_ValidCCGpn] PRIMARY KEY CLUSTERED 
(
    [Id] 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 this is the result I get in the table:

Id  Period  GPN CC_GPN
1   NULL    NULL    NULL
2   NULL    NULL    NULL
3   NULL    NULL    NULL
4   NULL    NULL    NULL
5   NULL    NULL    NULL
6   NULL    NULL    NULL
7   NULL    NULL    NULL
8   NULL    NULL    NULL
9   NULL    NULL    NULL
10  NULL    NULL    NULL
1

There are 1 answers

1
AD.Net On BEST ANSWER

You are missing column attribute on your other properties

[Table(Name = "CAT.ValidCCGpn")] 
public class ValidCCGpn 
{ 
    [Column(IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert)] 
    public int Id { get; set; } 

    [Column]
    public int Period { get; set; } 
    [Column]
    public string GPN { get; set; } 
    [Column]
    public string CC_GPN { get; set; } 

}