开发者

Linq2SQL can't read a null-value from table-field as ENUM

开发者 https://www.devze.com 2023-03-08 10:13 出处:网络
You can attach a ENUM Datatype to a sqlserver-table-field in Linq2SQL with global::Namespace and specify it\'s Server Data Type.

You can attach a ENUM Datatype to a sqlserver-table-field in Linq2SQL with global::Namespace and specify it's Server Data Type.

I have removed a table and a relationship to prior a ENUM instead. In Linq2Sql DataContext, the type is set to global::Namespace, NULLable=True and its corresponding server data type (small int).

Why am I get

InvalidOperationException
The null value cannot be assig开发者_开发问答ned to a member with type 
<Namespace> which is a non-nullable value type. 

..when loading rows that have this value as null?


How about simply opening up the generated code and adding the '?' to the corresponding declaration? I know you had to do something similar some time ago to make sure Guid-fields would get db-generated values. (It supports it but Linq2Sql didn't add that parameter correctly..)

I completely agree with you, this is not recommended at all. But if you see no other solution than to change the generated code I would strongly recommend documenting it clearly somewhere as well, somewhere not in the generated code. I did not in the project I mentioned above, and a few months later I had to change something in the DB for a new version. Of course I didn't think about my changes to the auto generated code at the time and regenerated it, resulting in quite an impressive amount of errors, which was a little messy to sort out since I didn't exactly remember what I had changed earlier.. Good times, good times! :o)


I tried to promote a commenter to write an answer.

This question was solved with find/replace inside the auto-generated code. This is really not recommended and all changes could be replaced onced you just press Save in the ORM-tool (if it isn't support the changes).

In this case, you can probably solve it attaching a question mark after the datatype, inside the ORM designer. Which means global::Namespace.ClassName.EnumName will be global::Namespace.ClassName.EnumName? just like a i.e. nullable int int?. The other properties must of course fit as usual.

Also worth to mention revert to above (read a null value); If the data type would save a null value, the enum need to handle NULL.

0

精彩评论

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