0

请帮忙!!!

我有这段简单的代码,我不知道为什么不做应该做的事情是插入类中包含的数据,我不知道我做错了什么,因为插入语句只产生表中的 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 和 and 表:

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
4

1 回答 1

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

} 
于 2012-10-12T23:22:42.510 回答