Bob The Janitor
Bob The Janitor

Reputation: 20792

Entity Framework getting a ICollection<int> from mapper

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"));

Upvotes: 2

Views: 476

Answers (2)

J.W.
J.W.

Reputation: 18181

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;}
}

Upvotes: 1

Pawel
Pawel

Reputation: 31610

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;} 
} 

Upvotes: 1

Related Questions