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

  • Home
  • SEARCH
  • 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 738281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:49:06+00:00 2026-05-14T07:49:06+00:00

I’ve a simple class [Serializable] public class MyClass { public String FirstName { get;

  • 0

I’ve a simple class

[Serializable]
public class MyClass
{
  public String FirstName { get; set: }
  public String LastName { get; set: }

  //Bellow is what I would like to do
  //But, it's not working
  //I get an exception
  ContactDataContext db = new ContactDataContext();

  public void Save()
  {
   Contact contact = new Contact();
   contact.FirstName = FirstName;
   contact.LastName = LastName;

   db.Contacts.InsertOnSubmit(contact);
   db.SubmitChanges();
  }
}

I wanted to attach a Save method to the class so that I could call it on each object. When I introduced the above statement which contains ContactDataContext, I got the following error “In assembly … PublicKeyToken=null’ is not marked as serializable“

It’s clear that the DataContext class is generated by the framework (). I checked and did not see where that class was marked serialize.

What can I do to overcome that? What’s the rule when I’m not the author of a class? Just go ahead and mark the DataContext class as serializable, and pretend that everything will work?

Thanks for helping

  • 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-05-14T07:49:07+00:00Added an answer on May 14, 2026 at 7:49 am

    The problem is that the db field gets serialized, while clearly it doesn’t need to be serialized (it’s instantiated once the object is created).

    Therefore, you should decorate it with the NonSerialized attribute:

    [NonSerialized] 
    ContactDataContext db = new ContactDataContext();
    

    [Update]

    To make sure the db field is accesable after object initialization, you should use a lazy loading property and use this property instead of the field:

    [NonSerialized] 
    ContactDataContext db = null;
    
    [NonSerialized] 
    private ContactDataContext {
        get {
            if (db == null) {
                db = new ContactDataContext();
            }
            return db;
        }
        set {
            db = value;
        }
    }
    
    public void Save()
    {
        Contact contact = new Contact();
        contact.FirstName = FirstName;
        contact.LastName = LastName;
    
        Db.Contacts.InsertOnSubmit(contact);
        Db.SubmitChanges();
    }
    

    [Update2]

    You can serialize most objects, as long as it has a public parameterless constructor (or no constructor at all) and no properties/fields that cannot be serialized but require serializing. If the class itself is not marked as [Serializable], then you can do this yourself using a partial class. If the class has properties/fields that cannot be serialized, then you might achieve this by inheriting the class and overriding these properties/fields to decorate them as [NonSerialized].

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

Sidebar

Ask A Question

Stats

  • Questions 364k
  • Answers 364k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The SmtpClient can be used by code: SmtpClient mailer =… May 14, 2026 at 3:40 pm
  • Editorial Team
    Editorial Team added an answer I don't know of any off the shelf tools to… May 14, 2026 at 3:40 pm
  • Editorial Team
    Editorial Team added an answer The ownerType in the DependencyProperty.RegisterAttached call is the type that… May 14, 2026 at 3:39 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.