Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.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
Asp.net mvc 由于异常,实体框架无法检索数据_Asp.net Mvc_Entity Framework 4.1 - Fatal编程技术网

Asp.net mvc 由于异常,实体框架无法检索数据

Asp.net mvc 由于异常,实体框架无法检索数据,asp.net-mvc,entity-framework-4.1,Asp.net Mvc,Entity Framework 4.1,这是我的两个POCO: [Table("Movie", Schema= "dbo")] public class Movie: BaseClass { public string TitleName { get; set; } public virtual ICollection<Actor> Actor{ get; set; } } [Table("Actor", Schema="dbo")] public class Actor: BaseClass {

这是我的两个POCO:

[Table("Movie", Schema= "dbo")]
public  class Movie: BaseClass
{
    public string TitleName { get; set; }

    public virtual ICollection<Actor> Actor{ get; set; }

}

[Table("Actor", Schema="dbo")]
public class Actor: BaseClass
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime? BirthDate { get; set; }
}
我在StackOverflow中搜索了一个类似的问题,他们(向其他用户)建议使用外键,因此我在Actor集合前面创建了一个属性

[ForeignKey("Actor")]
然后我得到另一个例外,它说:

The ForeignKeyAttribute on property 'Actor' on type 
'Movie' is not valid. The foreign key name 
'Actor' was not found on the dependent type     
'Actor'.
The Name value should be a comma separated list of foreign key property names.
这里有人遇到过这个问题吗?

请参阅帖子以获取更多帮助。我认为这将有助于解释如何做到这一点。您必须向下滚动才能进入外键部分。它基本上说:

使用外键时,它们可以在模型中公开,也可以不公开。 当我说“暴露在模型中”时,它意味着有一个额外的 作为主体值的从属实体上的属性 实体。在下面的代码中,PrimaryCategoryCode属性是 公开了主类别的外键

public string PrimaryCategoryCode { get; set; }

public virtual Category PrimaryCategory { get; set; }
要指出这一点,请在PrimaryCategory上使用ForeignKey属性 财产如下:

public string PrimaryCategoryCode { get; set; }


[ForeignKey("PrimaryCategoryCode")]
public virtual Category PrimaryCategory { get; set; }

EF 4.1代码优先约定将在电影演员之间创建一对多关系。由于需要多对多关系,因此必须使用Fluent API对此进行配置:

public class MyContext : DbContext
{
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Movie>()
            .HasMany(m => m.Actor)
            .WithMany()
            .Map(a => {
                a.MapLeftKey("MovieId");  // your PK column name in Movie table
                a.MapRightKey("ActorId"); // your PK column name in Actor table
                a.ToTable("MovieActor");  // your join table name
            });
    }
}
公共类MyContext:DbContext
{
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
modelBuilder.Entity()
.HasMany(m=>m.Actor)
.有很多
.Map(a=>{
a、 MapLeftKey(“MovieId”);//电影表中的PK列名
a、 MapRightKey(“ActorId”);//Actor表中的PK列名
a、 ToTable(“MovieActor”);//您的联接表名称
});
}
}

Ahh我错过了多对多的要求。请参阅Slauma的答案,因为它是正确的。
public class MyContext : DbContext
{
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Movie>()
            .HasMany(m => m.Actor)
            .WithMany()
            .Map(a => {
                a.MapLeftKey("MovieId");  // your PK column name in Movie table
                a.MapRightKey("ActorId"); // your PK column name in Actor table
                a.ToTable("MovieActor");  // your join table name
            });
    }
}