Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 178253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T14:11:56+00:00 2026-05-11T14:11:56+00:00

I have been trying out both Linq to Sql and EF in my ASP.NET

  • 0

I have been trying out both Linq to Sql and EF in my ASP.NET MVC application. After switching to EF I realized my XML/JSON serialization output has extra cruft.

XML:

<Test xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'>   <EntityKey>     <EntitySetName>Persons</EntitySetName>     <EntityContainerName>PersonEntities</EntityContainerName>     <EntityKeyValues>       <EntityKeyMember>         <Key>Id</Key>         <Value xsi:type='xsd:int'>1</Value>       </EntityKeyMember>     </EntityKeyValues>   </EntityKey>   <Id>1</Id>   <Name>John</Name> </Test> 

JSON:

{'Id':1,'Name':'John','EntityState':2,'EntityKey'{'EntitySetName':'Persons','EntityContainerName':'PersonEntities','EntityKeyValues':[{'Key':'Id','Value':1}],'IsTemporary':false}} 

Instead I would just like my output to be:

{'Id':1, 'Name':'John'} 

My EF query to retrieve the object is:

Tests.First(t => t.Id == testId); 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T14:11:56+00:00Added an answer on May 11, 2026 at 2:11 pm

    You can shape the JSON result in your controller like this:

    public JsonResult Person(int id) {   var person = PersonRepository.FindByID(id);   var result = new { Id = person.Id, Name = person.Name };   return Json(result); } 

    This will limit the DTO which is serialized to contain only the values you want.

    Edit: As a paritial answer to your comment question; you can create a simpler PersonViewModel class (DTO) that you can map the properties to. As John Saunders mentioned in his answer Automapper is a nice way to simplify the copying of the property values out of the EF Person instance:

    The modified Action method may look like this:

    public JsonResult Person(int id) {   var person = PersonRepository.FindByID(id);   var dto = Mapper.Map<Person, PersonViewModel>(person);   return Json(dto); } 

    The only other option I can think of is to use reflection to modify the DataMemberAttributes on the Person entity to suppress the EntityKey property.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying out the NameValueDeserializer from MVCContrib, which will take a IList
I have been trying to find out why the following lines of code do
I have been trying to figure out a solution but nothing has really presented
I have been trying to figure out a way to tag several methods from
I have been trying to find out what the difference is between the IS
I am fairly new to Emacs and I have been trying to figure out
I have been trying to figure this out for way to long tonight. I
I have been trying to use a formula that is used to work out
I have been trying to make a div fade in evey 30sec and out
I have been going mad trying to figure out why my scripts weren't working,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.