[RESOLVED]MVC Web API return JSON from edmx model problem deserializing

Hi all,

I have a web api that returns results in json format from edmx model. But I’m having problems deserializing the results because there are other values in the json result (looks like some other attributes of the edmx model to me). Below are codes:

TransactionRepository

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace My_Services.Models
{
    public class TransactionRepository : ITransactionRepository
    {
        private MyEntities db = new MyEntities();

        public IEnumerable<Transaction> GetAll()
        {
            return db.Transactions.ToList();
        }
    }
}

ITransactionRepository (interface)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using My_Services.Models;

namespace My_Services.Models
{
    public interface ITransactionRepository
    {
        IEnumerable<Transaction> GetAll();
    }
}

TransactionController

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using My_Services.Models;

namespace My_Services.Controllers
{
    public class TransactionController : ApiController
    {
        static readonly ITransactionRepository repository = new TransactionRepository();

        public IEnumerable<Transaction> GetAllTransactions()
        {
            return repository.GetAll();
        }
    }
}

WebApi.config

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using System.Net.Http.Headers;

namespace My_Services
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );

            config.Formatters.JsonFormatter.
            SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
        }
    }
}

Result:

[{"$id":"1","ID":1,"EmpID":"00001 ","EDate":"2014-10-10T00:00:00","TimeStart":"12:00 MN","TimeEnd":"12:00 MN","Venue":"Test Venue","Category":"TEST","Type":"Test Meeting Code","Particular":"Test Particular","WholeDay":true,"NoHrs":8.0,"Notes":"Test transaction","EntityKey":{"$id":"2","EntitySetName":"Transactions","EntityContainerName":"PTSEntities","EntityKeyValues":[{"Key":"ID","Type":"System.Int32","Value":"1"}]}},{"$id":"3","ID":2,"EmpID":"00001 ","EDate":"2014-10-10T00:00:00","TimeStart":"12:00 MN","TimeEnd":"12:00 MN","Venue":"Test Venue","Category":"TEST","Type":"Test Meeting Code","Particular":"Test 2","WholeDay":false,"NoHrs":0.0,"Notes":"Test 2","EntityKey":{"$id":"4","EntitySetName":"Transactions","EntityContainerName":"PTSEntities","EntityKeyValues":[{"Key":"ID","Type":"System.Int32","Value":"2"}]}}]

My fields are from "ID" up to "Notes" only.

TIA for any help.

put first

db.ObjectContext.ContextOptions.ProxyCreationEnabled = false

ignatandrei

put first

db.ObjectContext.ContextOptions.ProxyCreationEnabled = false

Still getting the same results after adding like below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace My_Services.Models
{
    public class TransactionRepository : ITransactionRepository
    {
        private MyEntities db = new MyEntities();

        public IEnumerable<Transaction> GetAll()
        {
            db.ContextOptions.ProxyCreationEnabled = false;
            return db.Transactions.ToList();
        }
    }
}

re-compile

ignatandrei

re-compile

Done. Also restarted VS. Still the same output.

Solved with below solution:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace My_Services.Models
{
    public class TransactionRepository : ITransactionRepository
    {
        private MyEntities db = new MyEntities();
        
        public List<TransactionModel> GetAll()
        {
            var results = db.Transactions.Select(r=> new 
                                {
                                    r.ID,
                                    r.EmpID,
                                    r.EDate,
                                    r.TimeStart,
                                    r.TimeEnd,
                                    r.Venue,
                                    r.Category,
                                    r.Type,
                                    r.Particular,
                                    r.WholeDay,
                                    r.NoHrs,
                                    r.Notes
                                });

            TransactionModel transmodel = new TransactionModel();
            List<TransactionModel> translist = new List<TransactionModel>();

            foreach (var item in results)
            {
                transmodel.ID = item.ID;
                transmodel.EmpID = item.EmpID;
                transmodel.EDate = item.EDate;
                transmodel.TimeStart = item.TimeStart;
                transmodel.TimeEnd = item.TimeEnd;
                transmodel.Venue = item.Venue;
                transmodel.Category = item.Category;
                transmodel.Type = item.Type;
                transmodel.Particular = item.Particular;
                transmodel.WholeDay = item.WholeDay;
                transmodel.NoHrs = item.NoHrs;
                transmodel.Notes = item.Notes;
                translist.Add(transmodel);
            }

            return translist;
        }
    }
}

A better solution:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace My_Services.Models
{
    public class TransactionRepository : ITransactionRepository
    {
        private MyEntities db = new MyEntities();
        
        public List<TransactionModel> GetAll()
        {
            List<TransactionModel> translist = db.Transactions.Select(t => new TransactionModel
                                                                {
                                                                    ID = t.ID,
                                                                    EmpID = t.EmpID,
                                                                    EDate = t.EDate,
                                                                    TimeStart = t.TimeStart,
                                                                    TimeEnd = t.TimeEnd,
                                                                    Venue = t.Venue,
                                                                    Category = t.Category,
                                                                    Type = t.Type,
                                                                    Particular = t.Particular,
                                                                    WholeDay = t.WholeDay,
                                                                    NoHrs = t.NoHrs,
                                                                    Notes = t.Notes
                                                                }).ToList();

            return translist;
        }
    }
}

Leave a Reply