Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 实体框架一对多映射_.net_Entity Framework - Fatal编程技术网

.net 实体框架一对多映射

.net 实体框架一对多映射,.net,entity-framework,.net,Entity Framework,这可能只是一些简单的初学者的错误,但我似乎无法理解 我只是想开始使用实体框架(EF5.0、.NET4.5),到目前为止,我一直在使用自己的sql语句。我首先尝试使用现有的数据库和EF代码,使用两个简单的类,它们具有一对多的关系:clips和clip_ratings(每个clip可以有零或多个评级)。课程的相关部分包括: public class clip { public clip() { this.Ratings = new List<clip_ratin

这可能只是一些简单的初学者的错误,但我似乎无法理解

我只是想开始使用实体框架(EF5.0、.NET4.5),到目前为止,我一直在使用自己的sql语句。我首先尝试使用现有的数据库和EF代码,使用两个简单的类,它们具有一对多的关系:clips和clip_ratings(每个clip可以有零或多个评级)。课程的相关部分包括:

public class clip
{
    public clip()
    {
        this.Ratings = new List<clip_ratings>();
    }

    public virtual ICollection<clip_ratings> Ratings { get; set; }

    public int clip_id { get; set; }
    public Nullable<int> clip_type { get; set; }
    // ...
 }

public class clip_ratings
{
    public int cr_id { get; set; }
    public int cr_clip_id { get; set; }
    // ...

    [ForeignKey("cr_clip_id")]
    public clip Clip { get; set; }
}
该查询从数据库返回正确的结果集,但在item.Ratings.Count上出现错误。当我刚刚输出item.clip\u linktext时,一切正常,但对于item.Ratings.Count,我得到以下错误:

元数据集合中有多个项与标识“clip_ratings”匹配

我试着跟随所有的微软EF教程,阅读更多的帖子,但似乎无法理解。希望这里有人能在这方面帮助我(而且它很容易修复;)

更新:添加classMap.cs源:

public class clipMap : EntityTypeConfiguration<clip>
{
    public clipMap()
    {
        // Primary Key
        this.HasKey(t => t.clip_id);
        // ...


        // Table & Column Mappings
        this.ToTable("clips");
        this.Property(t => t.clip_id).HasColumnName("clip_id");
        this.Property(t => t.clip_type).HasColumnName("clip_type");
        ...
     }
 }

public class clip_ratingsMap : EntityTypeConfiguration<clip_ratings>
{
    public clip_ratingsMap()
    {
        // Primary Key
        this.HasKey(t => t.cr_id);
        // Table & Column Mappings
        this.ToTable("clip_ratings");
        this.Property(t => t.cr_id).HasColumnName("cr_id");
        this.Property(t => t.cr_clip_id).HasColumnName("cr_clip_id");
        // ...
    }
}
公共类clipMap:EntityTypeConfiguration
{
公共clipMap()
{
//主键
this.HasKey(t=>t.clip\u id);
// ...
//表和列映射
本表为可折叠(“剪辑”);
this.Property(t=>t.clip_id).HasColumnName(“clip_id”);
this.Property(t=>t.clip_type).HasColumnName(“clip_type”);
...
}
}
公共类剪辑映射:EntityTypeConfiguration
{
公共剪辑评分SMAP()
{
//主键
this.HasKey(t=>t.cru id);
//表和列映射
本表为ToTable(“clip_评级”);
this.Property(t=>t.cr_id).HasColumnName(“cr_id”);
this.Property(t=>t.cr_clip_id).HasColumnName(“cr_clip_id”);
// ...
}
}

标记
剪辑id
cr\u id
,并标记
评级
属性,如下所示:

public class clip
{
    public clip()
    {
        this.Ratings = new List<clip_ratings>();
    }

    [InverseProperty("Clip")]
    public virtual ICollection<clip_ratings> Ratings { get; set; }

    [Key]
    public int clip_id { get; set; }
    public Nullable<int> clip_type { get; set; }
    // ...
 }

public class clip_ratings
{
    [Key]
    public int cr_id { get; set; }
    public int cr_clip_id { get; set; }
    // ...

    [ForeignKey("cr_clip_id")]
    public clip Clip { get; set; }
}
公共类剪辑
{
公共剪辑()
{
this.Ratings=新列表();
}
[反向属性(“剪辑”)]
公共虚拟ICollection分级{get;set;}
[关键]
公共int clip_id{get;set;}
公共可为空的片段类型{get;set;}
// ...
}
公开课的收视率
{
[关键]
公共int cr_id{get;set;}
公共int cru clip_id{get;set;}
// ...
[外键(“cr_clip_id”)]
公共剪辑剪辑{get;set;}
}
了解


PS<强>严重< /强>考虑阅读和

< P>马克>代码> clipIID/<代码>和<代码> CRYID >和<代码>收视率< /代码>属性如下:

public class clip
{
    public clip()
    {
        this.Ratings = new List<clip_ratings>();
    }

    [InverseProperty("Clip")]
    public virtual ICollection<clip_ratings> Ratings { get; set; }

    [Key]
    public int clip_id { get; set; }
    public Nullable<int> clip_type { get; set; }
    // ...
 }

public class clip_ratings
{
    [Key]
    public int cr_id { get; set; }
    public int cr_clip_id { get; set; }
    // ...

    [ForeignKey("cr_clip_id")]
    public clip Clip { get; set; }
}
公共类剪辑
{
公共剪辑()
{
this.Ratings=新列表();
}
[反向属性(“剪辑”)]
公共虚拟ICollection分级{get;set;}
[关键]
公共int clip_id{get;set;}
公共可为空的片段类型{get;set;}
// ...
}
公开课的收视率
{
[关键]
公共int cr_id{get;set;}
公共int cru clip_id{get;set;}
// ...
[外键(“cr_clip_id”)]
公共剪辑剪辑{get;set;}
}
了解


PS<强>严重/强>考虑阅读和

< P> OK,在这个简单例子的每一部分都花了一个小时之后,我发现了一个解决方案:显然EF不喜欢被称为“收视率”的集合,即使在DB或代码中没有任何部分具有该名称的属性/字段/属性。

将名称更改为投票,而不是评级

    public clip()
    {
        this.Votes = new List<clip_ratings>();
    }

    public virtual ICollection<clip_ratings> Votes { get; set; }
public clip()
{
this.vows=新列表();
}
公共虚拟ICollection投票{get;set;}
一切都很好

我猜这就是为什么到目前为止我一直避免使用这种框架,你永远不知道它们在幕后做什么;)

是的,如果我有机会重新设计db,我将尝试遵循命名约定


如果有人能很好地解释为什么它不适用于评级,我还是很想听听,这样以后就可以避免类似的情况了。

好吧,在又一个小时对这个简单示例的每一部分进行了处理后,我找到了一个解决方案:显然EF不喜欢这个集合被称为“评级”,即使数据库或代码中没有任何部分具有该名称的属性/字段/属性

将名称更改为投票,而不是评级

    public clip()
    {
        this.Votes = new List<clip_ratings>();
    }

    public virtual ICollection<clip_ratings> Votes { get; set; }
public clip()
{
this.vows=新列表();
}
公共虚拟ICollection投票{get;set;}
一切都很好

我猜这就是为什么到目前为止我一直避免使用这种框架,你永远不知道它们在幕后做什么;)

是的,如果我有机会重新设计db,我将尝试遵循命名约定


如果有人能很好地解释为什么它不适用于收视率,我还是很想听听,以避免将来出现类似情况。

不幸的是,这没有帮助,仍然是同样的错误。我还忘了提到主键是在类映射文件中配置的,但我也尝试过使用PKs的注释而不是fluent api,就像你发布的那样。那么你是在混合fluent api和数据注释?如果你能发布你的类图就太好了。。。对于命名约定部分,我仍然遵循C#约定,并使用
ColumnAttribute
映射列。所有键、列和表名都通过EF power tools反向工程代码首先创建的fluent api文件进行映射。然后手动添加注释和关系。到目前为止,我所读的内容让我相信,只要没有冲突的设置/属性,两者的混合就不是问题。当我让VS显示实体图时,它看起来很好(包括1:n关系),sql查询也可以正确地转换以连接两个表。我只是不知道是什么导致了这个错误…我