开发者

Using LINQ to SQL; How Do I Insert a Row in a Table Without Setting all Column Values?

开发者 https://www.devze.com 2022-12-19 14:11 出处:网络
So I\'m doing something like this, and I need to specifically NOT insert the value for one of the columns, a uniqueidentifier that is defaulted to newid() in the database. How do I do this? The follow

So I'm doing something like this, and I need to specifically NOT insert the value for one of the columns, a uniqueidentifier that is defaulted to newid() in the database. How do I do this? The following code is what I want, but doesn't work because the tagID column is getting defaulted to new Guid() -- all zeros and SQL server obviously doesn't allow multiple inserts of the same uniqueidentier in a PK column.

 String[] tags = this.TextBoxTags.Text.Split(',');
                for (int i = 0; i < tags.Length; i++)
                {
                    tags[i] = tags[i].Trim();
                }
                if (tags.Length > 0)
                {
                    foreach (String tag in tags)
                    {
                        tblDATA_BlogTag blogTag = new tblDATA_BlogTag();
                        blogTag.blogID = blogPost.postID;
  开发者_如何学Python                      blogTag.tagText = tag;
                        blogPost.tblDATA_BlogTags.Add(blogTag);
                    }
                }
                dbTimecard.SubmitChanges();


using-default-values-in-linq-to-sql

set the Auto-Synch property to OnInsert, in code it looks like...

[Column(AutoSync=AutoSync.OnInsert,IsDbGenerated=true)]
public DateTime Created
{
}
0

精彩评论

暂无评论...
验证码 换一张
取 消