2

using Entity Framework 4.1 trying to get a collection of ints,

basically I have a Entity called Spec

public class Spec {
   public int Id{get;set;}
   public string Name {get;set;}
   public ICollection<int> TypeIds {get;set;} 
}

the table Specs has the Columns id, Name, etc and I'm trying to map TypeIds to table SpecTypes with column specId TypeId and I can't figure out the mappings for it

I have been tying something like this

modelBuilder.Entity<Spec>().HasMany(r => r.TypesIds)
       .WithMany()
       .Map(m => m.ToTable("SpecTypes")
          .MapLeftKey("SpecId")
          .MapRightKey("TypeId"));
Mat
  • 202,337
  • 40
  • 393
  • 406
Bob The Janitor
  • 20,292
  • 10
  • 49
  • 72
  • Currently EF doesn't support collections of primitive and complex types. Only collections of entities are supported, unfortunately. – hival Feb 07 '12 at 09:00

2 Answers2

1

I don't think you can have a navigation property to a collection of primitive values. I think you just need to created an entity that contains Id property and have a collection of these. So you would have more or less something like this:

public class Spec { 
   public int Id{get;set;} 
   public string Name {get;set;} 
   public ICollection<TypeEntity> TypeIds {get;set;}  
} 

public class TypeEntity { 
   public int Id {get;set;} 
} 
Pawel
  • 31,342
  • 4
  • 73
  • 104
1

Based on what you describe, you probably want to do one-to-many relationship like this. You normally only need to use modelbuilder to solve some complicated mapping which you cannot do it in class definition.

public class Spec {
   public int Id{get;set;}
   public string Name {get;set;}
   public SpecType SpecType {get;set;} 
}


public class SpecType {
   public int Id{get;set;}
   public ICollection<Spec> Specs {get;set;}
}
J.W.
  • 17,991
  • 7
  • 43
  • 76