Asp.net core Asp.net Core不会序列化我的新类型
我正在使用.NETCore+angular 5,并试图返回一个列表,但JSON响应中有一个字段为null。我使用Postman触发调试,并在VS中看到该字段有一个来自DB的值。 不知道为什么它不在JSON响应中Asp.net core Asp.net Core不会序列化我的新类型,asp.net-core,ef-core-2.1,Asp.net Core,Ef Core 2.1,我正在使用.NETCore+angular 5,并试图返回一个列表,但JSON响应中有一个字段为null。我使用Postman触发调试,并在VS中看到该字段有一个来自DB的值。 不知道为什么它不在JSON响应中 [HttpGet("[action]")] public IEnumerable<HikingTrail> HikingTrails() { var dbOptions = new DbContextOptionsBuilder<HikingTrailConte
[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();
}
[HttpGet(“[action]”)
公共IEnumerable HikingTrails()
{
var dbOptions=new DbContextOptionsBuilder();
dbOptions.UseSqlServer(“服务器=(localdb)\\mssqllocaldb;数据库=HikingApp”);
var dbContext=newdao.HikingTrailContext(dbOptions.Options);
返回dbContext.HikingTrails.ToList();
}
这将返回: 我对“mountainRange”字段不为空感兴趣。在调试窗口中,它具有正确的值 { “url”:空, “hikingTrailId”:159, “山脉范围”:空, “名称”:“我的自定义名称”, “起始点”:空, “端点”:null, “trailCheckpoints”:空, “类型”:2, “困难”:空, “持续时间”:“2 1/2-3小时”, “专注”:“00:00:00”, “最大持续时间”:“00:00:00”, “季节性”:“中等”, “设备级别”:空, “追踪标记”:空, “hasTrailType”:false }, 我在想它可能是EF Core,并进行了第二次尝试(即在dbContext查询中添加了Include()):
[HttpGet(“[action]”)
公共IEnumerable HikingTrails()
{
var dbOptions=new DbContextOptionsBuilder();
dbOptions.UseSqlServer(“服务器=(localdb)\\mssqllocaldb;数据库=HikingApp”);
var dbContext=newdao.HikingTrailContext(dbOptions.Options);
返回dbContext.HikingTrails.Include(x=>x.MountainRange.ToList();
}
无法在邮递员中获得任何响应
编辑:
public类HikingTrailContext:DbContext
{
公共HikingTrailContext(DbContextOptions options):基本(选项)
{
}
公共HikingTrailContext():base(){
}
公共数据库集HikingTrails{get;set;}
公共DbSet MountainRanges{get;set;}
公共DbSet TrailScrapingHistory{get;set;}
模型创建时受保护的覆盖无效(ModelBuilder ModelBuilder)
{
}
}
公营飞行路线
{
[关键]
public int HikingTrailId{get;set;}
公共HikingTrail(){}
公共山脉山脉{get;set;}
公共字符串名称{get;set;}
公共位置起始点{get;set;}
公共位置终结点{get;set;}
公共列表跟踪检查点{get;}
公共TrailType=>TrailType.未确定;
公共字符串困难{get;set;}
公共字符串持续时间{get;set;}
公共时间跨度持续时间{get;set;}
公共TimeSpan MaxDuration{get;set;}
公共字符串季节性{get;set;}
公共字符串设备级别{get;set;}
公共字符串跟踪标记{get;set;}
公共字符串URL;
公共bool HasTrailType
{
得到
{
返回此.Type!=TrailType.undeminated;
}
}
公共覆盖布尔等于(对象对象对象)
{
return(((HikingTrail)obj.Name==this.Name);
}
公共覆盖int GetHashCode()
{
int hash=17;
//适当的空值检查等,当然:)
hash=hash*23+Name.GetHashCode();
hash=hash*23+Type.GetHashCode();
hash=hash*23+StartPoint.GetHashCode();
返回散列;
}
公共重写字符串ToString()
{
返回Name.ToString();
}
}
编辑:
我分析了dbContext.HikingTrails.Include(x=>x.MountainRange).Where(x=>x.MountainRange!=null).ToList()上的数据库;生成的查询是OK的,这意味着它也有一个MountainRange的名称列
注意:有几个字段为空,但这些字段还没有数据。找到了一个解决方案,投影到匿名类型。还必须小心不要让两个字段具有相同的名称“name”
[HttpGet(“[action]”)
公共动态HikingTrails3()
{
var dbOptions=new DbContextOptionsBuilder();
dbOptions.UseSqlServer(“服务器=(localdb)\\mssqllocaldb;数据库=HikingApp”);
var dbContext=newdao.HikingTrailContext(dbOptions.Options);
var trails=dbContext.HikingTrails.Include(x=>x.MountainRange)。
选择(i=>new{Name=i.Name,MountainRangeName=i.MountainRange.Name,i.Duration,
i、 困难,i.设备级别,i.季节性,i.类型});
返回路线;
}
你能给我们看看HikingTrailContext
吗?是的,我已经把它添加到我的帖子里了。谢谢你的回答你能给我们看一下你的HikingTrail模型吗?是的,谢谢你的回答编辑,所以dbContext.HikingTrails.ToList()的元素实际上有MountainRange null。我不知道为什么,这是以前设定的。
[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();
}
}
[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;
}