【问题标题】:(21,6) : error 0019: Each property name in a type must be unique.(21,6) : 错误 0019: 类型中的每个属性名称必须是唯一的。
【发布时间】:2013-10-24 20:44:15
【问题描述】:

您好,我知道这已经被问过几次了,尽管我阅读了其他用户的回答,但我似乎仍然无法弄清楚,在尝试先使用 EF 代码初始化我的数据库后出现此错误:

(21,6):错误 0019:类型中的每个属性名称必须是唯一的。属性名称“AlumnoID”已定义。

这是我的项目结构:http://s17.postimg.org/u2jsvjke7/solutionexplorer.png

这是我收到错误的 poco 类的配置:

   public class AlumnosConfiguracion : EntityTypeConfiguration<tblAlumnos>
    {
    public AlumnosConfiguracion()
    {
        this.HasKey(p => p.AlumnoID);
       // this.Property(p => p.AlumnoID).HasColumnOrder(0);


        this.Property(p => p.Matricula).IsRequired();
        this.Property(p => p.ApellidoPaterno).HasMaxLength(120).IsRequired();
        this.Property(p => p.ApellidoMaterno).HasMaxLength(120).IsRequired();
        this.Property(p => p.Nombres).HasMaxLength(25).IsRequired();
        this.Property(p => p.Edad).IsRequired();
        this.Property(p => p.Sexo).HasMaxLength(10).IsRequired();
        this.Property(p => p.FechaNacimiento).HasMaxLength(20).IsRequired();
        this.Property(p => p.Nacionalidad).HasMaxLength(25).IsRequired();
        this.Property(p => p.Telefono).HasMaxLength(25).IsOptional();
        this.Property(p => p.DomicilioID).IsOptional();
        this.Property(p => p.Activo).IsRequired();

        //Relacion direccion a alumnos, un alumno una direccion, una direccion muchos alumnos.
        this.HasRequired(a => a.objDomicilio)
            .WithMany(d => d.alumnos)
            .HasForeignKey(a => a.DomicilioID)
            .WillCascadeOnDelete(false);
        //Relacion Plantel a alumnos, donde un planetel puede tener muchos alumnos, pero un alumno un plantel.
        this.HasRequired(a => a.objPlantel)
            .WithMany(p => p.alumnos)
            .HasForeignKey(a => a.PlantelID)
            .WillCascadeOnDelete(false);
    }
}

这里是数据库上下文类:

命名空间 GestionAlumnos.Contexto { 公共类 Alumnosdb : DbContext { //构造函数 para que el nombre de la base de datos sea igual en sqlserver. 公共 Alumnosdb() : base(nameOrConnectionString: Alumnosdb.ConnectionStringName){}

    //Static Constructor
    static Alumnosdb()
    {
        //here I initialize my database
        Database.SetInitializer(new AlumnosDbInicializador());
    }


    public DbSet<tblAlumnos> Alumnos { get; set; }
    public DbSet<tblUsuarios> Usuarios { get; set; }
    public DbSet<AlumnosSemestres> AlumnosSemestres { get; set; }
    public DbSet<AnioPeriodo> AnioPeriodo { get; set; }
    public DbSet<tblColegios> Colegios { get; set; }
    public DbSet<tblDomicilios> Domicilios { get; set; }
    public DbSet<tblPeriodos> Periodos { get; set; }
    public DbSet<tblPlanteles> Planteles { get; set; }
    public DbSet<tblSemestres> Semestres { get; set; }
    public DbSet<tblTurnos> Turnos { get; set; }
    public DbSet<tblRoles> Roles { get; set; }


    public static string ConnectionStringName
    {
        get
        {                                       
            if (ConfigurationManager.AppSettings["ConnectionStringName"] != null)
            {
                return ConfigurationManager.AppSettings["ConnectionStringName"].ToString();
            }

            return "DefaultConnection";
        }
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {

        modelBuilder.Configurations.Add(new AlumnosConfiguracion());
        modelBuilder.Configurations.Add(new AlumnosSemestresConfiguracion());
        modelBuilder.Configurations.Add(new AnioPeriodoConfiguracion());
        modelBuilder.Configurations.Add(new ColegiosConfiguracion());
        modelBuilder.Configurations.Add(new DomiciliosConfiguracion());
        modelBuilder.Configurations.Add(new PeriodosConfiguracion());
        modelBuilder.Configurations.Add(new PlantelesConfiguracion());
        modelBuilder.Configurations.Add(new RolesConfiguracion());
        modelBuilder.Configurations.Add(new SemestresConfiguracion());
        modelBuilder.Configurations.Add(new TurnosConfiguracion());
        modelBuilder.Configurations.Add(new UsuariosConfiguracion());

        //base.OnModelCreating(modelBuilder);
    }


}

}

这是我尝试初始化数据库后遇到的错误:

http://s12.postimg.org/q019oyo4d/error.png

我很绝望,我已经为此苦苦挣扎了好几天,如果有人想查看我的代码,请告诉我,我会将其发送给您,但请帮助解决这些问题,已经与 Julie 一起尝试过Lerman 说要删除剩余的程序集,但仍然没有结果,希望这里的人能够帮助我。

感谢您对未来的建议。

【问题讨论】:

    标签: c# entity-framework ef-code-first


    【解决方案1】:

    在这里找到解决方案:

    public class UsuariosConfiguracion : EntityTypeConfiguration<tblUsuarios>
         {
        public UsuariosConfiguracion()
        {
            //Relacion uno a uno. Un alumno un Usuarios.
            this.HasRequired(u => u.objAlumno)
                .WithRequiredPrincipal(a => a.objUsuario);
                .Map(p => p.MapKey("AlumnoID")); // <<< Right HERE  
          }
       }
    

    当我尝试与 EF 执行一对一关系时,我正在映射到现有数据库,这就是为什么 EF 抱怨说已经有一个名为“AlumnoID”的实体所以我做了什么来解决它摆脱了映射,就这样离开了它:

    this.HasRequired(u => u.objAlumno)
    .WithRequiredPrincipal(a => a.objUsuario);
    

    这解决了我过去几天一直在苦苦挣扎的问题,所以如果有人遇到和我一样的问题,请先检查你的 EF 关系。

    感谢您阅读本文。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-25
      • 1970-01-01
      • 2011-07-27
      • 1970-01-01
      • 2020-09-24
      • 1970-01-01
      • 2022-06-25
      • 1970-01-01
      相关资源
      最近更新 更多