3

Is it possible to do fluent mapping on a collection property of primitive type in Entity Framework 6.1 Code First?

I know that this was not possible with earlier versions.

For example something like:

public class Foo
{
      public int Id;
      public virtual List<string> Bar;
}

with fluent mapping:

modelBuilder.Entity<Foo>()
            .HasMany<string>(f => f.Bar);
Charles
  • 50,943
  • 13
  • 104
  • 142
  • What does the relation of one Foo to many strings represent in a model? What is what you want to achieve. Relations are created between entities, and a primitive type is not an entity. – JotaBe Jun 04 '14 at 00:10
  • @JotaBe I want to be able to program against OO principles and have DB design concerns abstracted. If I wanted to work strictly according to relational database principles, I would not have made use of an ORM. However, I do want to be able to make use of OO design principles and excluding the use of a collection of primitives on the basis of principles which only applies to the domain of db design is contrary to the intent of ORM technologies. The practical implications thereof is also that the technology becomes too restrictive. Other ORM's support this functionality for this reason. –  Jun 04 '14 at 08:32
  • I'd like to see a sample of the model which includes this kind of relation between an entity and a primitive type collection. I'm not speaking about the DB model, but your app's domain model. If I understand how your model looks like, perhaps there will be a way to map it to EF ORM. – JotaBe Jun 04 '14 at 08:39
  • @JotaBe I appreciate that, but I can assure you that this is a problem I frequently run into and that it most cases it is actually nonsensical to go and create a separate entity just because EF does not support this feature. I know how to work around it, but I actually just wanted to know if the restriction still applied. –  Jun 04 '14 at 08:58
  • I don't doubt is an usual problem for you, but I'd like to see an example of this case. And I also would be grateful if you told me which ORM allows to make that type of mapping. The nearest thing in EF is complex type, but it doesn't support collections out of the box: you'd have to do it indirectly. But to show you how, I'd need to see a sample so that I can grab the concept. – JotaBe Jun 04 '14 at 09:15

1 Answers1

0

In EF it cannot be donde directly. However, there is a workaround: you can create a collection property which is not mapped to the database, and another property, mapped to the DB, which holds a serialized version of your collection. If the collection has many elements you'll suffer performance problems. If not, it's alright to use it.

Seudocode:

public class Entity
{
  [NotMapped]
  public List<string> MyStrings 
  { 
     get { return Serializer.Deserialize(SerializedMyStrings); }
     set { SerializedMyStrings = Serializer.Serialize(value); } 
  }

  // Mapped to DB
  public string SerializedMyString { get; set; }

}

NOTE: depending on the serializer implementation the property could be an string (XML, JSON, concatenation with a special separator, i.e. anything represented by text) or byte[] (if you use a binary serializer). This data will be stored in single column of your DB table

There are several problems:

  • there is an additional property that shouldn't be directly used (the serialzide property). This can be mitigated by making it protected.
  • the collection is always loaded (there can be no lazy loading)
  • the serialization will degrade the performance
Community
  • 1
  • 1
JotaBe
  • 38,030
  • 8
  • 98
  • 117