C# Can';t首先在EF代码上生成组合主键

C# Can';t首先在EF代码上生成组合主键,c#,entity-framework,ef-code-first,entity-framework-migrations,C#,Entity Framework,Ef Code First,Entity Framework Migrations,我正在尝试在EF上进行新项目的第一次迁移,但我一直遇到一个毫无意义的异常 我为每个业务类使用单独的配置类,接收异常的是: public class AlunoAcessaArquivoMapeamento : EntityTypeConfiguration<AlunoAcessaArquivo> { public AlunoAcessaArquivoMapeamento() { ToTable(Regex.Replace(typeof(AlunoAcessaA

我正在尝试在EF上进行新项目的第一次迁移,但我一直遇到一个毫无意义的异常

我为每个业务类使用单独的配置类,接收异常的是:

public class AlunoAcessaArquivoMapeamento : EntityTypeConfiguration<AlunoAcessaArquivo> {
    public AlunoAcessaArquivoMapeamento() {
        ToTable(Regex.Replace(typeof(AlunoAcessaArquivo).Name, "([^A-Z])([A-Z])", "$1_$2").ToLower());
        HasKey(e => new {e.AlunoId, e.ArquivoId});
        HasRequired(a => a.Aluno).WithMany(a => a.AlunosAcessaArquivos).HasForeignKey(a => a.AlunoId);
        HasRequired(a => a.Arquivo).WithMany(a => a.AlunosAcessaArquivos).HasForeignKey(a => a.ArquivoId);
    }
}
当我尝试
添加迁移时
出现异常:

System.InvalidOperationException:属性表达式“e=>new f__AnonymousType0`2(AlunoId=e.AlunoId,ArquivoId=e.ArquivoId)”无效。表达式应该表示一个属性:C#::“t=>t.MyProperty”VB.Net:“Function(t)t.MyProperty”。指定多个属性时,请使用匿名类型:C#::“t=>new{t.MyProperty1,t.MyProperty2}”VB.Net:“函数(t)new With{t.MyProperty1,t.MyProperty2}”


这个例外没有任何意义。请注意,我在第一个代码示例的第四行配置了我的主键,它显然遵循在异常中指定的匿名类型格式,因此我坚持使用它。

您的
AlunoId
ArquivoId
中的
alunoaccessaarquivo
是字段,而不是属性。这就是问题所在。将其设置为以下属性:

public class AlunoAcessaArquivo : EntidadeBase {

    public virtual Aluno Aluno { get; set; }

    public virtual Arquivo Arquivo { get; set; }

    public long AlunoId { get; set; } // <-- You missed { get; set; }
    public long ArquivoId { get; set; } // <-- You missed { get; set; }
}
公共类alunoaccessaarquivo:EntidadeBase{
公共虚拟Aluno Aluno{get;set;}
公共虚拟Arquivo Arquivo{get;set;}

公共长AlunoId{get;set;}/您的
AlunoId
ArquivoId
中的
alunoaccessaarquivo
是字段,而不是属性。这就是问题所在。请按如下方式设置它们的属性:

public class AlunoAcessaArquivo : EntidadeBase {

    public virtual Aluno Aluno { get; set; }

    public virtual Arquivo Arquivo { get; set; }

    public long AlunoId { get; set; } // <-- You missed { get; set; }
    public long ArquivoId { get; set; } // <-- You missed { get; set; }
}
公共类alunoaccessaarquivo:EntidadeBase{
公共虚拟Aluno Aluno{get;set;}
公共虚拟Arquivo Arquivo{get;set;}
公共长AlunoId{get;set;}//