实体框架获取 ICollection来自映射器

发布于 2025-01-03 05:32:34 字数 538 浏览 5 评论 0原文

使用Entity Framework 4.1尝试获取整数集合,

基本上我有一个名为Spec的实体,

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

表Specs具有列id、名称等,我试图将TypeIds映射到具有列specId TypeId的表SpecTypes,但我不能找出它的映射

我一直在绑这样的东西

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

╰◇生如夏花灿烂 2025-01-10 05:32:34

我认为您不能拥有原始值集合的导航属性。我认为您只需要创建一个包含 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;} 
} 

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;} 
} 
甜柠檬 2025-01-10 05:32:34

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

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

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;}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文