0

我正在学习 EF,但我遇到了导航问题。我希望你们能告诉我路径:)

当我运行此代码时,EF 显示此消息:

System.Reflection.TargetInvocationException:调用的目标已引发异常。---> System.MissingMethodException:找不到方法:'System.Collections.Generic.List`1 Sis.Model.Ft1Cap300.get_Ft1Cap300l()'。”

我真的不明白问题出在哪里;我认为我的导航属性还可以,但 EF 说它无法识别 Ft1Cap300.Ft1Cap300l 属性。

我有 3 个模型实体。我将发布课程及其配置

public class Ft1Caratula
{
    [Key]
    public String EncuestaId { get; set; }
    public Int32 DistritoId { get; set; }       //fk
    public String Conglomerado { get; set; }
    [Required]
    public Int32 Area { get; set; }
    [Required]
    public Int32 NumHogar { get; set; }
    [Required]
    public Int32 TotHogar { get; set; }
    public Int32 TipoComunidad { get; set; }
    [Required]
    public String JefeNombres { get; set; }
    [Required]
    public String JefeApePat { get; set; }
    [Required]
    public String JefeApeMat { get; set; }
    public Int32 DirTipo { get; set; }
    public String DirDireccion { get; set; }
    public Nullable<Int32> Dir1 { get; set; }
    public String Obs { get; set; }
    public Int32 Idioma { get; set; }
    public String Idioma_o { get; set; }
    [Required]
    public DateTime Ent1Fecha { get; set; }
    [Required]
    public Int32 Ent1Res { get; set; }
    public String Ent1Res_o { get; set; }
    public String EncId { get; set; }           
    public DateTime EncFecha { get; set; }

    public virtual UbiDistrito Distrito { get; set; }
    public virtual SegTecnico Encuestador { get; set; }

    public virtual List<Ft1Cap100> Ft1Cap100 { get; set; }
    public virtual List<Ft1Cap300> Ft1Cap300 { get; set; }
}

public class Ft1Cap300
{
    public String EncuestaId { get; set; }
    [Required]
    public int P301 { get; set; }

    public virtual Ft1Caratula Ft1Caratula { get; set; }
    public virtual List<Ft1Cap300l> Ft1Cap300l { get; set; }
}

public class Ft1Cap300l
{
    public String EncuestaId { get; set; }
    public int NOrden { get; set; }
    public String P302 { get; set; }
    [Required]
    public int P303 { get; set; }
    [Required]
    public int P304 { get; set; }
    public String P305_1 { get; set; }
    public String P305_2 { get; set; }
    public String P305_3 { get; set; }

    public virtual Ft1Cap300 Ft1Cap300 { get; set; }
}

public class Ft1CaratulaConfiguracion : EntityTypeConfiguration<Ft1Caratula>
{
    public Ft1CaratulaConfiguracion()
    {
        ToTable("ft1caratula", "data");
        HasRequired(p => p.Encuestador).WithMany(l => l.Ft1Caratulas).HasForeignKey(p => p.EncId);
        HasRequired(p => p.Distrito).WithMany(l => l.Ft1Caratulas).HasForeignKey(p => p.DistritoId);
    }
}

public class Ft1Cap300Configuracion : EntityTypeConfiguration<Ft1Cap300>
{
    public Ft1Cap300Configuracion()
    {
        ToTable("ft1cap300", "data");
        HasKey(p => new { p.EncuestaId });
        HasRequired(p => p.Ft1Caratula).WithMany(l => l.Ft1Cap300).HasForeignKey(p => p.EncuestaId);

    }
}

public class Ft1Cap300lConfiguracion : EntityTypeConfiguration<Ft1Cap300l>
{
    public Ft1Cap300lConfiguracion()
    {
        ToTable("ft1cap300l", "data");
        HasKey(p => new { p.EncuestaId, p.NOrden });
        HasRequired(p => p.Ft1Cap300).WithMany(l => l.Ft1Cap300l).HasForeignKey(p => p.EncuestaId);
    }
}

我希望你能帮我解决这个问题。我有“Programming EF: CodeFirst”一书作为指南。

4

1 回答 1

0

你的代码目前存在以下问题

public class Ft1CaratulaConfiguracion : EntityTypeConfiguration<Ft1Caratula>
    {
        public Ft1CaratulaConfiguracion()
        {
            ToTable("ft1caratula", "data");
            HasRequired(p => p.Encuestador).WithMany(l => l.Ft1Caratulas).HasForeignKey(p => p.EncId);
            HasRequired(p => p.Distrito).WithMany(l => l.Ft1Caratulas).HasForeignKey(p => p.DistritoId);
        }
    }

Ft1Caratula 类没有属性EncuestadorDistrito

另外在

public virtual List<Ft1Cap100> Ft1Cap100 { get; set; }

您正在使用属性作为类型。所以你应该得到一些编译时错误。

于 2013-01-15T05:26:43.257 回答