嗨,我知道这已经被问过几次了,虽然我阅读了其他用户的答案,但我似乎仍然无法弄清楚,在尝试首先使用 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);
    }
}
这是数据库上下文类:
namespace GestionAlumnos.Contexto { public class Alumnosdb : DbContext { //Constructor 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 说过删除剩余的程序集但仍然没有结果,希望这里有人能够帮助我。
感谢您对未来的建议。