Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.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 web api GetAll在执行POST后返回null_C#_Asp.net_Asp.net Web Api - Fatal编程技术网

C# Asp.net web api GetAll在执行POST后返回null

C# Asp.net web api GetAll在执行POST后返回null,c#,asp.net,asp.net-web-api,C#,Asp.net,Asp.net Web Api,当我测试(使用Fiddler Web Debugger)通过Web api向省插入或发布数据时,数据通常会插入到数据库中,但当我访问GetAll Web api服务时,国家/地区返回空值: http://localhost:51372/api/province/ 但在我使用VS 2013 Express For Web Edition重新构建解决方案,然后再次从Web api访问getAll服务后,我发现国家正在返回值: 在我发布数据后,如何获取国家返回的值 这是我使用的详细代码 模型:

当我测试(使用Fiddler Web Debugger)通过Web api向省插入或发布数据时,数据通常会插入到数据库中,但当我访问GetAll Web api服务时,国家/地区返回空值:

http://localhost:51372/api/province/

但在我使用VS 2013 Express For Web Edition重新构建解决方案,然后再次从Web api访问getAll服务后,我发现国家正在返回值:

在我发布数据后,如何获取国家返回的值

这是我使用的详细代码

模型:

    public class Province
    {        
        public int ProvinceId { get; set; }
        [Required]
        public string ProvinceName { get; set; }
        public DateTime CreatedAt { get; set; }
        public DateTime UpdatedAt { get; set; }
        //Foreign Key
        [ForeignKey("Country")]
        public int CountryId { get; set; }
        //virtual
        public virtual Country Country { get; set; }
    }

    public class Country
    {
        [ScaffoldColumn(false)]
        public int CountryId { get; set; }        
        [Required]
        [Index(IsUnique=true)]
        [MaxLength(200)]
        public string CountryName { get; set; }
        public DateTime CreatedAt { get; set; }
        public DateTime UpdatedAt { get; set; }        
    }
存储库:

    private SelsContext db = new SelsContext();
    //get all provinces
    public IEnumerable<Province> GetAllProvince()
    {
        IEnumerable<Province> provinces = from c in db.Provinces select c;
        return provinces;
    }

    //post provinces
    public Models.Province Add(Models.Province item)
    {
        item.CreatedAt = DateTime.Now;
        item.UpdatedAt = DateTime.Now;

        db.Provinces.Add(item);
        db.SaveChanges();

        return item;
    }
    public IEnumerable<Province> GetAllProvince()
    {
        IEnumerable<Province> provinces = db.Provinces.Include("Country");            
        return provinces;
    }
private SelsContext db=new SelsContext();
//得到所有省份
公共IEnumerable GetAllProvince()
{
IEnumerable省=从数据库中的c开始。省选择c;
返回省份;
}
//邮政省
公共模型.省添加(模型.省项目)
{
item.CreatedAt=DateTime.Now;
item.UpdatedAt=DateTime.Now;
db.省。添加(项目);
db.SaveChanges();
退货项目;
}
控制器:

// GET api/province
[Queryable]
public IQueryable<Province> Get()
{
    return repository.GetAllProvince().AsQueryable();
}

// POST api/province
public HttpResponseMessage Post([FromBody]Province value)
{
    repository.Add(value);
    var response = Request.CreateResponse<Province>(HttpStatusCode.Created, value);

    string uri = Url.Link("DefaultApi", new { id = value.ProvinceId });
    response.Headers.Location = new Uri(uri);
    return response;
}
//获取api/省
[可查询]
公共IQueryable Get()
{
返回repository.GetAllProvince().AsQueryable();
}
//邮政署署长/省
公共httpresponsemessagepost([FromBody]省值)
{
增加(价值);
var response=Request.CreateResponse(HttpStatusCode.Created,value);
字符串uri=Url.Link(“DefaultApi”,新的{id=value.ProvinceId});
response.Headers.Location=新Uri(Uri);
返回响应;
}
编辑: 到目前为止我已经试过了

在存储库中:

    private SelsContext db = new SelsContext();
    //get all provinces
    public IEnumerable<Province> GetAllProvince()
    {
        IEnumerable<Province> provinces = from c in db.Provinces select c;
        return provinces;
    }

    //post provinces
    public Models.Province Add(Models.Province item)
    {
        item.CreatedAt = DateTime.Now;
        item.UpdatedAt = DateTime.Now;

        db.Provinces.Add(item);
        db.SaveChanges();

        return item;
    }
    public IEnumerable<Province> GetAllProvince()
    {
        IEnumerable<Province> provinces = db.Provinces.Include("Country");            
        return provinces;
    }
public IEnumerable GetAllProvince()
{
IEnumerable省=分贝省。包括(“国家”);
返回省份;
}
然后

public IEnumerable GetAllProvince()
{
IEnumerable provides=db.provides.SqlQuery(“select*from dbo.provides left join dbo.Countries.CountryId=dbo.provides.CountryId上的dbo.Countries”);
返回省份;
}
但是得到同样的结果


非常感谢您的帮助。

在您的存储库中,您可以执行以下操作:

IEnumerable<Province> provinces = db.Provinces.Include("Country");
IEnumerable provides=db.provides.Include(“国家”);

我试过了,但结果还是和以前一样,有什么想法吗?我在一个新的项目中再次尝试了你的方法,上面有两张表,我发现它很有魅力。谢谢