实体框架4.1具有相同属性名称的两个表 [英] entity framework 4.1 two tables with the same property name

查看:142
本文介绍了实体框架4.1具有相同属性名称的两个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在实体框架和一个sql数据库中有一些问题。
所以我的问题是这样的:
在我的数据库中有两个具有相同属性的表,它们在类型和长度上是相同的,但是它们不以任何方式相关。没有外来的钥匙
一旦我开始与数据库实体框架交互,就会吐出着名的错误:


错误0019:每个属性名称在一个类型中必须是唯一的。属性名称已经定义。


我正在使用代码第一个方法。



这是表的类表示之一:

  [Table(bo)] 
public class TbPBO
{
[必需(AllowEmptyStrings = false,ErrorMessage =O campo de nome nao pode ser nulo)]
[MaxLength(55,ErrorMessage =O campo
[列(nome,TypeName =char,Order = 4)]
公共字符串TbBonome {get;组; }
}

这是表的其他类表示形式:

  [Table(bi)] 
public class TbPBi
{
[必需(AllowEmptyStrings = 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;组;我已经尝试通过使用这个但没有任何运气修改我的上下文

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

}
}

有关如何解决的任何想法这个?还有一件事。数据库不能以任何方式进行更改。我不能修改它。



帮助将不胜感激。
提前感谢

解决方案

经过多次搜索和尝试和错误。我发现ef电动工具,安装它,让它逆向工程我的数据库。我让程序运行。它映射了表。创建了类和上下文。对于我一直在测试的问题已经解决了。
感谢您的帮助


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

解决方案

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

这篇关于实体框架4.1具有相同属性名称的两个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆