2013-10-24 28 views
2

嗨,我知道这已被问了几次安静,虽然我读了其他用户的答案我仍然似乎无法弄清楚,我试图用EF代码初始化我的数据库后出现此错误:(21,6):错误0019:类型中的每个属性名称都必须是唯一的。

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

这里是我的proyect结构: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 { public class Alumnosdb:DbContext { //基础数据海构造函数ual en sqlserver。 公共Alumnosdb():基地(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); 
    } 


} 

}

,这是试图初始化我dababase后,我得到的错误:

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

我很绝望我一直在为此奋斗几天,如果有人想看看我的代码让我知道,我会把它发送给你,但请帮助这些,已经尝试与朱莉勒曼说过要删除剩余的组件,但仍然没有结果,希望这里有人能够帮助我。

感谢您的未来建议。

回答

4

找到了解决办法就在这里:

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); 

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

感谢您阅读本文。

相关问题