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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:25:20+00:00 2026-06-09T22:25:20+00:00

I want to store an object that contains a List of primitives using EF.

  • 0

I want to store an object that contains a List of primitives using EF.

public class MyObject {
    public int Id {get;set;}
    public virtual IList<int> Numbers {get;set;}
}

I know that EF cannot store this, but I’d like to know possible solutions to solve this problem.

The 2 Solutions I can think of are:

1.Create a Dummy object that has an Id and the Integervalue, e.g.

public class MyObject {
    public int Id {get;set;}
    public virtual IList<MyInt> Numbers {get;set;}
}

public class MyInt {
    public int Id {get;set;}
    public int Number {get;set;}
}

2.Store the list values as a blob, e.g.

public class MyObject {
    public int Id {get;set;}

    /// use NumbersValue to persist/load the list values
    public string NumbersValue {get;set;}

    [NotMapped]
    public virtual IList<int> Numbers {
         get {
              return NumbersValue.split(',');
         }
         set {
             NumbersValue = value.ToArray().Join(",");
         }
    }
}

The Problem with the 2. approach is, that I have to create a Custom IList implementation to keep track if someone modifies the returned collection.

Is there a better solution for this?

  • 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-09T22:25:22+00:00Added an answer on June 9, 2026 at 10:25 pm

    Although I do not like to answer my own question, but here is what solved my problem:

    After I found this link about Complex Types I tried several implementations, and after some headache I ended up with this.

    The List values get stored as a string on the table directly, so it’s not required to perform several joins in order to get the list entries. Implementors only have to implement the conversation for each list entry to a persistable string (see the Code example).

    Most of the code is handled in the Baseclass (PersistableScalarCollection). You only have to derive from it per datatype (int, string, etc) and implement the method to serialize/deserialize the value.

    It’s important to note, that you cannot use the the generic baseclass directly (when you remove the abstract). It seems that EF cannot work with that. You also have to make sure to annotate the derived class with the [ComplexType] attribute.

    Also note that it seems not to be possible to implement a ComplexType for IList<T> because EF complains about the Indexer (therefore I went on with ICollection).

    It’s also important to note, that since everything is stored within one column, you cannot search for values in the Collection (at least on the database). In this case you may skip this implementation or denormalize the data for searching.

    Example for a Collection of integers:

        /// <summary>
        /// ALlows persisting of a simple integer collection.
        /// </summary>
        [ComplexType]
        public class PersistableIntCollection : PersistableScalarCollection<int> {
            protected override int ConvertSingleValueToRuntime(string rawValue) {
                return int.Parse(rawValue);
            }
    
            protected override string ConvertSingleValueToPersistable(int value) {
                return value.ToString();
            }
        }
    

    Usage example:

    public class MyObject {
        public int Id {get;set;}
        public virtual PersistableIntCollection Numbers {get;set;}
    }
    

    This is the baseclass that handles the persistence aspect by storing the list entries within a string:

        /// <summary>
        /// Baseclass that allows persisting of scalar values as a collection (which is not supported by EF 4.3)
        /// </summary>
        /// <typeparam name="T">Type of the single collection entry that should be persisted.</typeparam>
        [ComplexType]
        public abstract class PersistableScalarCollection<T> : ICollection<T> {
    
            // use a character that will not occur in the collection.
            // this can be overriden using the given abstract methods (e.g. for list of strings).
            const string DefaultValueSeperator = "|"; 
    
            readonly string[] DefaultValueSeperators = new string[] { DefaultValueSeperator };
    
            /// <summary>
            /// The internal data container for the list data.
            /// </summary>
            private List<T> Data { get; set; }
    
            public PersistableScalarCollection() {
                Data = new List<T>();
            }
    
            /// <summary>
            /// Implementors have to convert the given value raw value to the correct runtime-type.
            /// </summary>
            /// <param name="rawValue">the already seperated raw value from the database</param>
            /// <returns></returns>
            protected abstract T ConvertSingleValueToRuntime(string rawValue);
    
            /// <summary>
            /// Implementors should convert the given runtime value to a persistable form.
            /// </summary>
            /// <param name="value"></param>
            /// <returns></returns>
            protected abstract string ConvertSingleValueToPersistable(T value);
    
            /// <summary>
            /// Deriving classes can override the string that is used to seperate single values
            /// </summary>        
            protected virtual string ValueSeperator {
                get {
                    return DefaultValueSeperator;
                }
            }
    
            /// <summary>
            /// Deriving classes can override the string that is used to seperate single values
            /// </summary>        
            protected virtual string[] ValueSeperators {
                get {
                    return DefaultValueSeperators;
                }
            }
    
            /// <summary>
            /// DO NOT Modeify manually! This is only used to store/load the data.
            /// </summary>        
            public string SerializedValue {
                get {
                    var serializedValue = string.Join(ValueSeperator.ToString(),
                        Data.Select(x => ConvertSingleValueToPersistable(x))
                        .ToArray());
                    return serializedValue;
                }
                set {
                    Data.Clear();
    
                    if (string.IsNullOrEmpty(value)) {
                        return;
                    }
    
                    Data = new List<T>(value.Split(ValueSeperators, StringSplitOptions.None)
                        .Select(x => ConvertSingleValueToRuntime(x)));
                }
            }
    
            #region ICollection<T> Members
    
            public void Add(T item) {
                Data.Add(item);
            }
    
            public void Clear() {
                Data.Clear();
            }
    
            public bool Contains(T item) {
                return Data.Contains(item);
            }
    
            public void CopyTo(T[] array, int arrayIndex) {
                Data.CopyTo(array, arrayIndex);
            }
    
            public int Count {
                get { return Data.Count; }
            }
    
            public bool IsReadOnly {
                get { return false; }
            }
    
            public bool Remove(T item) {
                return Data.Remove(item);
            }
    
            #endregion
    
            #region IEnumerable<T> Members
    
            public IEnumerator<T> GetEnumerator() {
                return Data.GetEnumerator();
            }
    
            #endregion
    
            #region IEnumerable Members
    
            IEnumerator IEnumerable.GetEnumerator() {
                return Data.GetEnumerator();
            }
    
            #endregion
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In GWT I want to store an Object on the server that contains session
My User object that I want to create and store in the datastore has
I am having a class Student. I want to store the object of class
I have a Store that contains a list of Products : var store =
In my ios application i want to store an object's reference. This object can
I want to store a .NET object into Azure Blob Storage. Currently I serialize
I want to store 10 Obj object in objList , but i don't know
I want to use session object in my web app.I want to store some
I want to serialize an object and store it inside sdcard under my project
Want to know ways to store OBJECTS of a Class in some persistent storage.

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.