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 7869479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:20:05+00:00 2026-06-03T01:20:05+00:00

I am using JSON.net to serialize my EntityFramework objects. In the past, I have

  • 0

I am using JSON.net to serialize my EntityFramework objects.

In the past, I have created a class that applies the “JsonIgnore” attribute to a property, and then I set the “MetadataType” attribute of my main EntityFramework class to that newly created class.

Here is an example:

The class that will be applied to the EF class:

 public class Role_DoNotSerialize
    {
        [JsonIgnore]
        public string Users { get; set; }
    }

The partial class file for the EF class:

[MetadataType(typeof(Role_DoNotSerialize))]
    public partial class Role
    { 
    }

In the above example, the property “Users” will not be serialized when serializing a “Role” object.

My problem is, this same technique fails to work when I add in the EntityKey property like so:

public class Role_DoNotSerialize
    {
        [JsonIgnore]
        public string Users { get; set; }

        [JsonIgnore]
        public System.Data.EntityKey EntityKey { get; set; }
    }

Using this class, the “EntityKey” property is still serialized. What am I doing wrong?

  • 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. Editorial Team
    Editorial Team
    2026-06-03T01:20:07+00:00Added an answer on June 3, 2026 at 1:20 am

    you can do this by implementing your own ContractResolver ( example code with JSON.NET 4.5, but also possible with older versions )

    public class ExcludeEntityKeyContractResolver : DefaultContractResolver
    {
        protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
        {
            IList<JsonProperty> properties = base.CreateProperties(type,memberSerialization);
            return properties.Where(p => p.PropertyType != typeof (System.Data.EntityKey)).ToList();
        }
    }
    

    you can then set this to set the ContractResolver for your JsonSerializerSettings object

    JsonSerializerSettings serializerSettings = new JsonSerializerSettings();
    serializerSettings.ContractResolver = new ExcludeEntityKeyContractResolver();
    

    Note that you are not limited to just that one lambda function but that you can implement any sort of checks you want. You can even override the Converter per property to do custom serialization.

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

Sidebar

Related Questions

I have some data that I have to serialize to JSON. I'm using JSON.NET.
I have a service that attempting to serialize a list into JSON (using JSON.NET)
I am using Json.NET to serialize an object graph. For each object that is
I'm using JSON.NET implementation to serialize/deserialize .NET objects to JS and vice versa, all
I want to serialize enum as string using JSON.NET using attributes similar to [JsonIgnore]
I have constructed several web services that successfully serialize my .NET types into JSON.
I am using json.net to parse objects and delivering them to a webservice I
I'm using asp.net mvc2 and trying to send a list of json objects with
I was using Json.Net to serialize dictionaries of type Dictionary, and when I added
I have an entity framework entity that i want to serialize as a json

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.