LINQ-to-SQL - 默认值&列列表 [英] LINQ-to-SQL - default values & column lists

查看:81
本文介绍了LINQ-to-SQL - 默认值&列列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直遇到LINQ问题,这是默认列值的行为。



如果您将表定义为具有"DEFAULT",那么值,当从指定的列列表中省略列时,数据库应该插入默认值。



例如,



CREATE TABLE [dbo] [ABC]



[Name] [nvarchar] 25 NOT < font color ="#808080"size = 2> NULL,


[IDNumber] INT DEFAULT(10)NOT NULL




在SQL下,一个操作n喜欢这样:


INSERT INTO ABC(Name,IDNumber)VALUES('Jim' ,22);



将产生一个值为'Jim'的行',在Name和IDNumber列中分别为22。



如果我这样做的话这个:



INSERT INTO ABC(名称)VALUES('Jack')



操作仍然会成功,即使我省略了'IDNumber'列,因为该表是使用


默认值10创建的身份证号码。因此,结果行将包含值'Jack',10。如果没有分配默认值,操作将失败,因为'IDNumber'列不允许NULL。



麻烦的是我无法让LINQ给我这样的行为。当我们为这样的表生成一个对象时,我们在类中得到一个成员来表示类型为'int'的IDNumber。如果我们使用记录执行InsertOnSubmit(),则会将对象上的现有值发送到数据库。我无法在LINQ语法中知道包含一个列表,该列表说明了"嘿 - 即使对象上有值 - 也不要在列列表中使用它"。



我遇到了这个帖子:



http://forums.microsoft.com/MSDN/ShowPost.aspx ?PostID = 2252728& SiteID = 1



但是我碰到了最后指示的响应者所在的墙。简而言之:



1)在DBML文件中将列设置为可空,然后为此列发送NULL值没有帮助,因为设计器代码在此时没有干预并尝试将NULL发送到数据库。



2)将列属性设置为IsDBGenerated = true会忽略列列表中的值,以便输入默认值,但这对您没有帮助不希望为该列插入默认值。



3)行为不受AutoSync设置的影响。



是po可以通过LINQ直接控制列列表吗?



谢谢,


Jim



解决方案

我发现这个工作:

http://weblogs.asp.net/stefansedich/archive/2008/01/08/handling-default-values-with-linq-to- sql.aspx


实现OnCreated并设置默认值:


结果

I've been having a problem with LINQ for a while, and that is the behavior of default column values.

 

If you have a table defined to have a "DEFAULT" value, the database is supposed to insert the default value when the column is omitted from the specified column list.

 

e.g.,

 

CREATE TABLE [dbo].[ABC](

[Name] [nvarchar](25) NOT NULL,

[IDNumber] INT DEFAULT(10) NOT NULL

)

 

Under SQL, an operation like this:

INSERT INTO ABC(Name, IDNumber) VALUES ('Jim', 22);

 

will produce a row with values 'Jim', 22 in the Name and IDNumber columns, respectively.

 

Now if I do this:

 

INSERT INTO ABC(Name) VALUES('Jack')

 

the operation will still succeed, even though I omitted the 'IDNumber' column, since the table was created with a

default value of 10 for IDNumber.  Therefore, the resulting row would contain values 'Jack', 10.  If there was no default value assigned, the operation would fail, since the 'IDNumber' column does not allow NULLs.

 

The trouble is I can't get LINQ to give me behavior like this.  When we generate an object for a table like this, we get a member in the class to represent IDNumber which is of type 'int'.  If we do an InsertOnSubmit() with a record, the existing value on the object is sent to the database.  There is no way I know of within LINQ syntax to include a column list that says to the effect of "hey - even though there's a value on the object - don't use it in the column list".

 

I ran across this posting:

 

 http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2252728&SiteID=1

 

but I hit the same wall that the responder at the end indicated.  In short:

 

1) Setting the column as nullable in the DBML file and then sending a NULL value down for this column does not help, since the designer code does not intervene at that point and tries to send the NULL to the database.

 

2) Setting the column attribute to IsDBGenerated=true omits the value from the column list so that the default value comes in, but this does not help when you don't want to insert a default value for that column.

 

3) The behavior is not affected with AutoSync settings.

 

Is it possible to control the column list directly through LINQ?

 

Thanks,

Jim

 

 

解决方案

I found this work around:

http://weblogs.asp.net/stefansedich/archive/2008/01/08/handling-default-values-with-linq-to-sql.aspx


Implement OnCreated and set defaults:



这篇关于LINQ-to-SQL - 默认值&amp;列列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆