实体框架从mapper </int>获取ICollection <int>

时间:2012-02-06 22:49:09

标签: entity-framework entity-framework-4.1 entity-framework-mapping

使用Entity Framework 4.1尝试获取一组int,

基本上我有一个名为Spec

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

表Specs有Columns id,Name等,我正在尝试将TypeIds映射到带有specId TypeId的表SpecTypes,我无法找出它的映射

我一直在打这样的东西

modelBuilder.Entity<Spec>().HasMany(r => r.TypesIds)
       .WithMany()
       .Map(m => m.ToTable("SpecTypes")
          .MapLeftKey("SpecId")
          .MapRightKey("TypeId"));

2 个答案:

答案 0 :(得分:1)

我认为你不能拥有一个原始值集合的导航属性。我认为你只需要创建一个包含Id属性的实体,并拥有这些实体的集合。所以你会有更多或更少的东西:

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

答案 1 :(得分:1)

根据您的描述,您可能希望像这样做一对多的关系。您通常只需要使用模型构建器来解决一些在类定义中无法做到的复杂映射。

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