使用.net core + angular 5,并尝试返回列表,但JSON响应中有一个字段 null 。使用Postman触发调试并在VS中看到该字段具有来自DB的值。 不知道为什么它不在JSON响应中。
[HttpGet("[action]")]
public IEnumerable<HikingTrail> HikingTrails()
{
var dbOptions = new DbContextOptionsBuilder<HikingTrailContext>();
dbOptions.UseSqlServer("Server = (localdb)\\mssqllocaldb; Database = HikingApp");
var dbContext = new DAO.HikingTrailContext(dbOptions.Options);
return dbContext.HikingTrails.ToList();
}
返回:
我对&#34; mountainRange &#34;感兴趣字段不为空。在调试窗口中,它具有正确的值。
{ &#34; url&#34;:null, &#34; hikingTrailId&#34;:159, &#34; mountainRange &#34;:null, &#34; name&#34;:&#34;我的自定义名称&#34;, &#34; startPoint&#34;:null, &#34; endPoint&#34;:null, &#34; trailCheckpoints&#34;:null, &#34;输入&#34;:2, &#34; dificulty&#34;:null, &#34;持续时间&#34;:&#34; 2 1/2 - 3 h&#34;, &#34; minDuration&#34;:&#34; 00:00:00&#34;, &#34; maxDuration&#34;:&#34; 00:00:00&#34;, &#34;季节性&#34;:&#34; mediu&#34;, &#34; equipmentLevel&#34;:null, &#34; trailMarking&#34;:null, &#34; hasTrailType&#34;:false },
我想的可能是它的EF Core,并且已经进行了第二次尝试(即将dblude()添加到dbContext查询中):
[HttpGet("[action]")]
public IEnumerable<HikingTrail> HikingTrails()
{
var dbOptions = new DbContextOptionsBuilder<HikingTrailContext>();
dbOptions.UseSqlServer("Server = (localdb)\\mssqllocaldb; Database = HikingApp");
var dbContext = new DAO.HikingTrailContext(dbOptions.Options);
return dbContext.HikingTrails.Include( x => x.MountainRange).ToList();
}
无法在邮递员中获得任何回复。
编辑:
public class HikingTrailContext : DbContext
{
public HikingTrailContext(DbContextOptions<HikingTrailContext> options) : base(options)
{
}
public HikingTrailContext():base(){
}
public DbSet<HikingTrail> HikingTrails { get; set; }
public DbSet<MountainRange> MountainRanges { get; set; }
public DbSet<TrailScrapingSessionInfo> TrailScrapingHistory { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
}
}
public class HikingTrail
{
[Key]
public int HikingTrailId { get; set; }
public HikingTrail() { }
public MountainRange MountainRange { get; set; }
public String Name { get; set; }
public Location StartPoint { get; set; }
public Location EndPoint { get; set; }
public List<Location> TrailCheckpoints { get; }
public TrailType Type => TrailType.Undetermined;
public String Dificulty { get; set; }
public String Duration { get; set; }
public TimeSpan MinDuration { get; set; }
public TimeSpan MaxDuration { get; set; }
public String Seasonality { get; set; }
public String EquipmentLevel { get; set; }
public String TrailMarking { get; set; }
public String URL;
public bool HasTrailType
{
get
{
return this.Type != TrailType.Undetermined;
}
}
public override bool Equals(object obj)
{
return (((HikingTrail)obj).Name == this.Name);
}
public override int GetHashCode()
{
int hash = 17;
// Suitable nullity checks etc, of course :)
hash = hash * 23 + Name.GetHashCode();
hash = hash * 23 + Type.GetHashCode();
hash = hash * 23 + StartPoint.GetHashCode();
return hash;
}
public override string ToString()
{
return Name.ToString();
}
}
编辑: 我在 dbContext.HikingTrails.Include(x =&gt; x.MountainRange).xhere(x =&gt; x.MountainRange!= null).ToList(); 上分析了数据库,生成的查询是好的,这意味着它也有MountainRange的Name列。
P.S。:几个字段为空,但那些字段还没有数据。
答案 0 :(得分:0)
找到一个解决方案, Projection 到匿名类型。还必须注意不要有两个具有相同名称“名称”的字段
[HttpGet("[action]")]
public dynamic HikingTrails3()
{
var dbOptions = new DbContextOptionsBuilder<HikingTrailContext>();
dbOptions.UseSqlServer("Server = (localdb)\\mssqllocaldb; Database = HikingApp");
var dbContext = new DAO.HikingTrailContext(dbOptions.Options);
var trails = dbContext.HikingTrails.Include(x => x.MountainRange).
Select( i =>new { Name= i.Name, MountainRangeName = i.MountainRange.Name, i.Duration,
i.Dificulty,i.EquipmentLevel, i.Seasonality, i.Type }).ToList();
return trails;
}