如何停止获取Web API 2中的重复数据

时间:2019-03-07 12:24:36

标签: entity-framework-6 asp.net-web-api2

我想设计RESTfull API,这里没有重复的数据。 这是我的上下文课

public class ReviewDbContext : DbContext
{
    public ReviewDbContext() : base("name=Con")
    {
        Database.SetInitializer(new MigrateDatabaseToLatestVersion<ReviewDbContext, ReviewsCalculateSystem.Models.Migrations.Configuration>("Con"));
        this.Configuration.LazyLoadingEnabled = false;
        //this.Configuration.ProxyCreationEnabled = false;
    }
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
    }
    private void FixEfProviderServicesProblem()
    {
        var instance = System.Data.Entity.SqlServer.SqlProviderServices.Instance;
    }
    //Here some entity....
}

我遇到此错误:

  

“ ObjectContent`1”类型未能序列化内容类型“ application / xml”的响应主体; charset = utf-8'。

此错误已解决,可以在我的Global.asax文件中添加关注代码

GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings
            .ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
        GlobalConfiguration.Configuration.Formatters
            .Remove(GlobalConfiguration.Configuration.Formatters.XmlFormatter);

reviewerReviewForEachProductById服务类

 public JsonResult reviewerReviewForEachProductById(int reviewerId, int productId)
    {
        var productReviewInfo = db.ReviewerTaskAsigns.Where(x => x.ReviewerId == reviewerId && x.ProductId == productId).Select(x => new { x.Product, x.ReviewCollectMargin, x.NumberOfReviewCollect }).FirstOrDefault();
        //var reviewList = db.Reviews.Where(x => x.ReviewerId == reviewerId && x.ProductId == productId).ToList();
        return new JsonResult
        {
            Data = new{productReviewInfo},
            JsonRequestBehavior = JsonRequestBehavior.AllowGet
        };
    }

当我使用邮递员enter image description here来访问以上服务时

上面的服务类中,我添加了一些额外的代码来执行某些操作,并获取了我解决的上述错误

public JsonResult reviewerReviewForEachProductById(int reviewerId, int productId)
    {
        var productReviewInfo = db.ReviewerTaskAsigns.Where(x => x.ReviewerId == reviewerId && x.ProductId == productId).Select(x => new { x.Product, x.ReviewCollectMargin, x.NumberOfReviewCollect }).FirstOrDefault();
        var reviewList = db.Reviews.Where(x => x.ReviewerId == reviewerId && x.ProductId == productId).ToList();
        return new JsonResult
        {
            Data = new{
                productReviewInfo,
                reviewList
            },
            JsonRequestBehavior = JsonRequestBehavior.AllowGet
        };
    }

当我使用邮递员访问上述服务时,我发现这里有很多重复数据

{
"productReviewInfo": {
    "Product": {
        "Admin": null,
        "ReviewerTaskAsigns": null,
        "Reviews": [
            {
                "Reviewer": null,
                "ReviewId": 3,
                "SwapmeetFbProfileLink": "B1",
                "SwapmeetProductLink": "B1",
                "SwapmeetReviewLink": "B1",
                "OwnReviewLink": null,
                "ReviewStatus": true,
                "isPay": false,
                "ProductId": 4,
                "ReviewerId": 1
            }
        ],
        "ProductId": 4,
        "ProductName": "C# Book",
        "ProductAsin": "64dFDF45F",
        "ProductLink": "https://www.amazon.com/Healthy-Meal-Prep-Cookbook-Wholesome-ebook/dp/B07492912F/ref=sr_1_1_sspa?s=digital-text&ie=UTF8&qid=1549435615&sr=1-1-spons&keywords=keto+meal+prep&psc=1",
        "CurrentStatus": true,
        "NumberOfReviewNeed": 30,
        "NumberOfReviewCollect": null,
        "ReviewStartDate": "2019-02-08T00:00:00",
        "ReviewEndDate": null,
        "AdminId": 1
    },
    "ReviewCollectMargin": 20,
    "NumberOfReviewCollect": 1
},
"reviewList": [
    {
        "Product": {
            "Admin": null,
            "ReviewerTaskAsigns": null,
            "Reviews": [],
            "ProductId": 4,
            "ProductName": "C# Book",
            "ProductAsin": "64dFDF45F",
            "ProductLink": "https://www.amazon.com/Healthy-Meal-Prep-Cookbook-Wholesome-ebook/dp/B07492912F/ref=sr_1_1_sspa?s=digital-text&ie=UTF8&qid=1549435615&sr=1-1-spons&keywords=keto+meal+prep&psc=1",
            "CurrentStatus": true,
            "NumberOfReviewNeed": 30,
            "NumberOfReviewCollect": null,
            "ReviewStartDate": "2019-02-08T00:00:00",
            "ReviewEndDate": null,
            "AdminId": 1
        },
        "Reviewer": null,
        "ReviewId": 3,
        "SwapmeetFbProfileLink": "B1",
        "SwapmeetProductLink": "B1",
        "SwapmeetReviewLink": "B1",
        "OwnReviewLink": null,
        "ReviewStatus": true,
        "isPay": false,
        "ProductId": 4,
        "ReviewerId": 1
    }
]

}

产品和评论模型类

 public class Product
{
    public int ProductId { get; set; }
    public string ProductName { get; set; }
    public string ProductAsin { get; set; }
    public string ProductLink { get; set; }
    public bool CurrentStatus { get; set; }
    public int NumberOfReviewNeed { get; set; }
    public int? NumberOfReviewCollect { get; set; }
    public DateTime? ReviewStartDate { get; set; }
    public DateTime? ReviewEndDate { get; set; }
    public int AdminId { get; set; }
    public virtual Admin Admin { get; set; }
    public virtual ICollection<Review> Reviews { get; set; }
    public virtual ICollection<ReviewerTaskAsign> ReviewerTaskAsigns { get; set; }

}

public class Review
{
    public int ReviewId { get; set; }
    public string SwapmeetFbProfileLink { get; set; }
    public string SwapmeetProductLink { get; set; }
    public string SwapmeetReviewLink { get; set; }
    public string OwnReviewLink { get; set; }
    public bool ReviewStatus { get; set; }
    public bool isPay { get; set; }
    public int ProductId { get; set; }
    public virtual Product Product { get; set; }
    public int ReviewerId { get; set; }
    public virtual Reviewer Reviewer { get; set; }
}

如何停止提取重复数据?

0 个答案:

没有答案