Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Asp.net核心WebApi将外键Id作为值返回_C#_Entity Framework_Asp.net Core_Asp.net Web Api_Entity Framework Core - Fatal编程技术网

C# Asp.net核心WebApi将外键Id作为值返回

C# Asp.net核心WebApi将外键Id作为值返回,c#,entity-framework,asp.net-core,asp.net-web-api,entity-framework-core,C#,Entity Framework,Asp.net Core,Asp.net Web Api,Entity Framework Core,网 在这里,我使用Entity Framework core(3.1.2版)开发Asp.net core,并使用mssql服务器作为我的数据库 我有两个实体类地区和国家 Region.cs [Key] public int Id { get; set; } [Column(TypeName ="nvarchar(20)")] [Required] public string Code { get; set; } [Column(TypeName = "nv

网 在这里,我使用Entity Framework core(3.1.2版)开发Asp.net core,并使用mssql服务器作为我的数据库

我有两个实体类地区和国家

Region.cs

[Key]
    public int Id { get; set; }
    [Column(TypeName ="nvarchar(20)")]
    [Required]
    public string Code { get; set; }
    [Column(TypeName = "nvarchar(150)")]
    [Required]
    public string Description { get; set; }
    [Column(TypeName = "Bit")]
    [Required]
    public Boolean Active { get; set; }
[Key]
public int Id { get; set; }

[Required]
public string Code { get; set; }
[Column(TypeName = "nvarchar(150)")]
[Required]
public string Description { get; set; }
[Column(TypeName = "Bit")]
[Required]
public Boolean Active { get; set; }
[Required]
public int RegionId { get; set; }
[ForeignKey("RegionId")]
public virtual Region Region { get; set;}
services.AddMvc()
           .SetCompatibilityVersion(CompatibilityVersion.Version_3_0)
           .AddNewtonsoftJson(options => {
             var resolver = options.SerializerSettings.ContractResolver;
             if (resolver != null)
               (resolver as DefaultContractResolver).NamingStrategy = null;
           });
  services.AddControllers();
  services.AddDbContext<MasterContext>(options =>
  options.UseSqlServer(Configuration.GetConnectionString("dev")));

    }
Country.cs

[Key]
    public int Id { get; set; }
    [Column(TypeName ="nvarchar(20)")]
    [Required]
    public string Code { get; set; }
    [Column(TypeName = "nvarchar(150)")]
    [Required]
    public string Description { get; set; }
    [Column(TypeName = "Bit")]
    [Required]
    public Boolean Active { get; set; }
[Key]
public int Id { get; set; }

[Required]
public string Code { get; set; }
[Column(TypeName = "nvarchar(150)")]
[Required]
public string Description { get; set; }
[Column(TypeName = "Bit")]
[Required]
public Boolean Active { get; set; }
[Required]
public int RegionId { get; set; }
[ForeignKey("RegionId")]
public virtual Region Region { get; set;}
services.AddMvc()
           .SetCompatibilityVersion(CompatibilityVersion.Version_3_0)
           .AddNewtonsoftJson(options => {
             var resolver = options.SerializerSettings.ContractResolver;
             if (resolver != null)
               (resolver as DefaultContractResolver).NamingStrategy = null;
           });
  services.AddControllers();
  services.AddDbContext<MasterContext>(options =>
  options.UseSqlServer(Configuration.GetConnectionString("dev")));

    }
和我的数据库上下文

    public class MasterContext: DbContext
  {
    public MasterContext(DbContextOptions options):  base(options)
    {}
    public DbSet<Region> regions { get; set; }
    public DbSet<country> countries { get; set; }
  }
    {
    "Id":10,
    "Code":"inn",
    "Description":"india",
    "Active":true,
    "RegionId":5,
    "Region": {
    "Id":5,
    "Code":"me",
    "Description":"middle east",
    "Active":true
}
    }
}

国家/地区总监

// GET: api/countries/5
        [HttpGet("{id}")]
        public async Task<ActionResult<country>> Getcountry(int id)
        {
            var country = await _context.countries.Include(i => i.Region).FirstOrDefaultAsync(i => i.Id == id);

            if (country == null)
            {
                return NotFound();
            }

            return country;
        }
我期待的是这个

{
"Id":10,
"Code":"inn",
"Description":"india",
"Active":true,
"RegionId": {
"Id":5,
"Code":"me",
"Description":"middle east",
"Active":true
}
}

请任何人给我一个解决方案…提前谢谢。

使用json忽略属性。例如,
[JsonIgnore]
尝试在您的
国家/地区
模型中添加以下属性(首先使用Newtonsoft.Json添加
的引用;


[ForeignKey(“RegionId”)]
是否应该位于RegionId字段而不是虚拟区域?我认为当前的行为完全正确。在Country类中,Region是一个对象,RegionId是带有Region对象Id的字符串。我不明白为什么这个json结构不适合您。也就是说,您可以尝试在Country类中使用属性。RegionId属性中的[JsonIgnore]将阻止对其进行序列化。[JsonProperty(“RegionId”)]区域属性中的[JsonProperty(“RegionId”)]将其序列化为“RegionId”。jsonignore注释通过无法读写来阻止该属性,请给出任何其他建议…,因为我想在其中执行crud操作我想在哪里使用that@Lvibescreation在乡下