问题
i'm having some problems with entity framework and a sql database. so my problem is this: in my database there are two tables that have the same property, they are identical in type and length but they are not related in any way. no foreign key whatsoever. as soon as i start to interact with the database entity framework spits out the famous error:
error 0019: Each property name in a type must be unique. Property name was already defined.
i'm using the code first approach by the way.
this is one of the class representations of the tables:
[Table("bo")]
public class TbPBO
{
[Required(AllowEmptyStrings = false, ErrorMessage = "O campo de nome nao pode ser nulo")]
[MaxLength(55, ErrorMessage = "O campo de nome nao pode ter mais que 55 caracteres")]
[Column("nome", TypeName = "char", Order = 4)]
public string TbBonome { get; set; }
}
this is the other class representation of the table:
[Table("bi")]
public class TbPBi
{
[Required(AllowEmptyStrings = false, ErrorMessage = "O campo nome nao pode ser nulo")]
[MaxLength(55, ErrorMessage = "O campo nome nao pode ter mais que 55 caracteres")]
[Column("nome", TypeName = "char", Order = 62)]
public string TbBinome { get; set; }
}
i've tried modifying my context by using this but without any luck.
public class PHCDbContext:DbContext
{
public PHCDbContext(string connection):base(connection)
{
Database.SetInitializer<PHCDbContext>(null);
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<TbPBO>()
.Map(mbo => { mbo.Properties(tnome => new { tnome.TbBonome }); mbo.ToTable("bo"); });
modelBuilder.Entity<TbPBi>()
.Map(mbo => { mbo.Properties(tnome => new { tnome.TbBinome }); mbo.ToTable("bi"); });
}
}
any idea on how to work around this? one more thing. the database must not be changed in any way. i cannot modify it.
help would be appreciated. thanks in advance
回答1:
after much googling and trial and error. i discovered ef power tools, installed it and let it reverse engineer my database. i let the process run. it mapped the tables. created the classes and the context. and for what i've been testing the problem is solved. thanks for the help
来源:https://stackoverflow.com/questions/14185189/entity-framework-4-1-two-tables-with-the-same-property-name