locked
Code First Entity Framework with Generic Types RRS feed

  • Question

  • Does the Entity Framework support generic types within class definition? I want the generic types to persist to the database.

    Example that works (is persisted in the db) using a non generic SimpleDateRangeClass - the date fields are added to the db;

     

        public class PersonName
        {
            public int PersonNameId { getset; }
            public string Surname { getset; }
            public string Forename { getset; }
            public SimpleDateRange Effective { getset; }
        }
     
        public class SimpleDateRange
        {
            public DateTime Start { getset; }
            public DateTime End { getset; }
        }

     

     

    Example that does not work - the date fields are not added to the db;

        public class PersonName
        {
            public int PersonNameId { getset; }
            public string Surname { getset; }
            public string Forename { getset; }
            public Range<DateTime> Effective { getset; }
        }
     
        public class Range<T>
        {
            public T Start { getset; }
            public T End { getset; }
        }

     

    I'm fairly new to EF (esp code first), does anyone know if it is possible to use the generic version of my example above?

    Thanks...

     

    Friday, July 22, 2011 4:34 PM

Answers

  • Entity framework doesn't support mapping generic types out of the box. Not sure if you can get this up with a bolt on.
    Devlin Liles http://twitter.com/devlinliles http://www.devlinliles.com/ If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by david.newman Thursday, July 28, 2011 8:24 AM
    Friday, July 22, 2011 7:52 PM