EF4.1中诡异的GUID为空问题
生活随笔
收集整理的這篇文章主要介紹了
EF4.1中诡异的GUID为空问题
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?在用EF4.1過程中發現了一個非常詭異的GUID為空問題,不說廢話,直接看問題吧:
測試表UserInfo(SQL SERVER 2008 R2):
?
這里是建表SQL語句,供有興趣朋友測試:
CREATE?TABLE?[dbo].[UserInfo](????[ID]?[int]?IDENTITY(1,1)?NOT?NULL,
????[UserID]?[uniqueidentifier]?NOT?NULL,
????[UserName]?[nvarchar](50)?NULL,
????[Email]?[varchar](50)?NOT?NULL,
????[PicID]?[uniqueidentifier]?NULL,
????PRIMARY?KEY([ID])
)
GO
?
UserInfo實體類:
using?System;using?System.Collections.Generic;
using?System.Linq;
using?System.Text;
using?System.Runtime.Serialization;
using?System.Data.Entity;
using?System.ComponentModel.DataAnnotations;
namespace?ConsoleTest
{
????[Serializable]
????[DataContract]
????[Table("UserInfo")]
????public?class?UserInfo
????{
????????[DataMember]
????????[Key]
????????public?int?ID?{?get;?set;?}
????????[DataMember]
????????public?Guid?UserID?{?get;?set;?}
????????[DataMember]
????????public?string?UserName?{?get;?set;?}
????????[DataMember]
????????public?string?Email?{?get;?set;?}
????????[DataMember]
????????public?Guid?PicID?{?get;?set;?}
????}
}
?
EF配置代碼:
using?System;using?System.Collections.Generic;
using?System.Linq;
using?System.Text;
using?System.Data.Entity;
using?System.Configuration;
namespace?ConsoleTest
{
????public?class?TestDBContext?:?DbContext
????{
????????public?TestDBContext()
????????????:?base("Test")
????????{
????????}
????????public?DbSet<UserInfo>?UserInfos?{?get;?set;?}
????}
}
?
測試方法:
using?System;using?System.Collections.Generic;
using?System.Linq;
using?System.Text;
using?System.Data.SqlClient;
namespace?ConsoleTest
{
????public?class?NullTest
????{
????????public?List<UserInfo>?GetUserInfo()
????????{
????????????using?(TestDBContext?context?=?new?TestDBContext())
????????????{
????????????????List<UserInfo>?users?=?context.UserInfos
????????????????????.Where(m=>m.UserID!=null)
????????????????????.Where(n=>n.UserName!=null)
????????????????????.Where(p=>p.Email!=null)
????????????????????.Where(q=>q.PicID!=null)
????????????????????.ToList();
????????????????return?users;
????????????}
????????}
????????public?List<UserInfo>?GetUserInfoNew()
????????{
????????????using?(TestDBContext?context?=?new?TestDBContext())
????????????{
????????????????List<UserInfo>?users?=?context.UserInfos
????????????????????.Where(m?=>?m.UserID?!=?Guid.Empty)
????????????????????.Where(n?=>?n.UserName?!=?null)
????????????????????.Where(p?=>?p.Email?!=?null)
????????????????????.Where(q?=>?q.PicID?!=?new?Guid("00000000-0000-0000-0000-000000000000"))
????????????????????.ToList();
????????????????return?users;
????????????}
????????}
????}
}?
?
調用:
using?System;using?System.Collections.Generic;
using?System.Linq;
using?System.Text;
using?System.Text.RegularExpressions;
namespace?ConsoleTest
{
????class?Program
????{
????????static?void?Main(string[]?args)
????????{
????????????NullTest?nullTest?=?new?NullTest();
????????????nullTest.GetUserInfo();
????????????nullTest.GetUserInfoNew();
????????????Console.Read();
????????}
????}
}?
?
用SQL SERVER Profiler工具檢測到的第一個方法SQL語句:
SELECT?[Extent1].[ID]?AS?[ID],?
[Extent1].[UserID]?AS?[UserID],?
[Extent1].[UserName]?AS?[UserName],?
[Extent1].[Email]?AS?[Email],?
[Extent1].[PicID]?AS?[PicID]
FROM?[dbo].[UserInfo]?AS?[Extent1]
WHERE?([Extent1].[UserName]?IS?NOT?NULL)?AND?([Extent1].[Email]?IS?NOT?NULL)?
??
?第二個方法SQL語句:
?exec?sp_executesql?N'SELECT?
[Extent1].[ID]?AS?[ID],?[Extent1].[UserID]?AS?[UserID],?
[Extent1].[UserName]?AS?[UserName],?
[Extent1].[Email]?AS?[Email],?
[Extent1].[PicID]?AS?[PicID]
FROM?[dbo].[UserInfo]?AS?[Extent1]
WHERE?([Extent1].[UserID]?<>?@p__linq__0)?AND?([Extent1].[UserName]?IS?NOT?NULL)?AND?([Extent1].[Email]?IS?NOT?NULL)?AND?(cast(''00000000-0000-0000-0000-000000000000''?as?uniqueidentifier)?<>?[Extent1].[PicID])',N'@p__linq__0?uniqueidentifier',@p__linq__0='00000000-0000-0000-0000-000000000000'
?
詭異在第一個方法生成的SQL語句中類型為GUID的兩個字段的不為空的條件沒有了,第二個方法是本人針對第一種方法不足提供的一種解決方案,不知大家有什么看法,怎么解決這一問題的,請不吝賜教!
本文轉自Artwl博客園博客,原文鏈接:http://www.cnblogs.com/artwl/,如需轉載請自行聯系原作者
總結
以上是生活随笔為你收集整理的EF4.1中诡异的GUID为空问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 易捷加油app怎么圈存(汉典易字的基本解
- 下一篇: 剪映app如何去除视频中的水印