简体   繁体   中英

LINQ query returns nulll on non-nullable field

on my code i've been using Linq to acces data on my Database, but there's one Table where it does'nt work. This Table has several fields :

ID => string, not nullable
Name => string, not nullable
Date => DateTime, not nullable
UserID => string, not nullable.

When I use a query to try and get those data every one of my fields returns the correct data, except one : UserID , it will ALWAYS return null , no matter what's in it.

there's my query :

using(ProgetEntities pg = ProgetEntities()){
   List<Alloc> ListAlloc = new List<Alloc>();
   ListAlloc = pg.Allocations.ToList();
}

There's my properties for Alloc :

public partial class Alloc
{
    public string ID { get; set; }
    public string Name { get; set; }
    public Nullable<System.DateTime> Date { get; set; }
    public string UserID { get; set; }
}

I Have tried using AsEnumerable in my query, but the result was the same.

Has that kind of problem happened to anyone else before ? And does anyone else have some idea of how i could try to fix this?

EDIT

By looking into the query i've found the problem ( i don't know why i did'nt look into it sooner), there's the query (from the debugger) :

pg.Alloc    {SELECT 
[Extent1].[ID] AS [ID], 
[Extent1].[NAME] AS [NAME], 
[Extent1].[DATE] AS [DATE], 
FROM [dbo].[Alloc] AS [Extent1]}    
System.Data.Entity.DbSet<ClassGetMS.Models.Alloc>

It would seem that my query does not include the UserID field.

答案很简单,该属性未正确映射,所以我进入了.edmx模型,并手动添加了它,现在一切正常。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM