Linq DataContext InsertOnSubmit语句仅在表中插入NULL值

时间:2012-10-12 23:11:17

标签: linq insert null datacontext insertonsubmit

请帮助!!!

我有这段简单的代码,我不知道为什么不做什么应该做的是插入类中包含的数据,我不知道我做错了什么,导致插入statement只在表中生成NULL值。

这是代码:

    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();
        }

    }

这是我的DataContext和和表:

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; }

}

这是用于创建表的sql脚本:

    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]

这是我在表格中得到的结果:

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 个答案:

答案 0 :(得分:0)

您缺少其他属性的列属性

[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; } 

}