【问题标题】:Entity framework Invalid Column name, EF adds number 1 to primary key实体框架列名无效,EF主键加数字1
【发布时间】:2013-02-16 22:29:19
【问题描述】:

我有这两个实体:

public partial class Suscriptores
{
    public Suscriptores()
    {
       this.Publicacion = new HashSet<Publicacion>();
    }

    [Key]
    public int IdSuscriptor { get; set; }
    public string LogoSuscriptor { get; set; }
    public string Identificacion { get; set; }
    public string Nombre { get; set; }
    public string Direccion { get; set; }
    public string Telefono { get; set; }
    public string Email { get; set; }
    public string Fax { get; set; }
    public string Home { get; set; }

    public virtual ICollection<Publicacion> Publicacion { get; set; }
}

public partial class Publicacion
{
    public Publicacion()
    {
        this.Edictos = new HashSet<Edictos>();
    }

    [Key]
    public decimal IdPublicacion { get; set; }
    public System.DateTime FechaPublicacion { get; set; }
    public string IdUsuario { get; set; }
    public System.DateTime FechaPublicacionHasta { get; set; }
    public System.DateTime FechaArchivoHasta { get; set; }
    public int IdSuscriptor { get; set; }
    public decimal IdTipoPublicacion { get; set; }

    [ForeignKey("IdSuscriptor")]
    public virtual Suscriptores Suscriptores { get; set; }
}

当我尝试运行此查询时:

public ActionResult DetailsVSTO(string Identificacion)
{
    var SusQ = from s in db.Suscriptores
               where s.Identificacion == Identificacion
               select s;

    return Json(SusQ.First(), JsonRequestBehavior.AllowGet);
}

它抛出这个消息:

System.Data.SqlClient.SqlException:列名“Suscriptores_IdSuscriptor1”无效

为了解决这个问题,我在 DBContext 中添加了这段流畅的 API 代码:

modelBuilder.Entity<Suscriptores>()
    .HasMany(x => x.Publicacion)
    .WithRequired(x => x.Suscriptores)
    .Map(a => a.MapKey("IdSuscriptor"));

但问题仍然存在。我该如何解决这个问题?

【问题讨论】:

  • 只是好奇,这不应该是多对多关联吗?我不想发布知道我只能得到一个订阅者的东西:)
  • 这就是今天第三次 +1 格特。我喜欢你的帖子:-)
  • Tks Gert 为您的“评论”,真的,我是 EF 的新手,只是对它有一点经验。每个“Suscriptores”都有很多“Publicaciones”,每个“Publicaciones”都有一个“Suscriptores”,这就是我想要建模的。
  • 好的,就这样吧。在 fluent API 中映射外键的方式是映射不属于类模型的键字段。与属性的映射应该没问题。 PublicacionSuscriptores 之间是否存在您为简单起见而忽略的另一个关联?
  • 没有。这是它们之间的唯一关联。

标签: entity-framework entity-framework-4 entity-framework-5


【解决方案1】:

也尝试添加多对一映射。请使用纯 Fluent API,您应该删除 [ForeignKey] 注释。

modelBuilder.Entity<Publicacion>()
            .HasRequired(x => x.Suscriptores)
            .WithMany(x => x.Publicacion);

【讨论】:

  • 你能解释一下为什么你说他们应该使用纯 Fluent API 吗?注释有什么问题?
【解决方案2】:

我收到与非外键列有关的此错误,并浪费了太多时间试图找出错误。它在我的代码中,而不是在 EF 或数据库中。我只是以为我输入了

this.Property(t => t.Revision).HasColumnName("Revision");
this.Property(t => t.DistributionClass).HasColumnName("DistributionClass");

但我输入的是

this.Property(t => t.Revision).HasColumnName("Revision");
this.Property(t => t.Revision).HasColumnName("DistributionClass");

我想我正在查看上面的行并输入t.Revision 而不是t.DistributionClass。无论我看了多久,我都看不到自己的错误。运气好的话,这将为其他一些可怜的灵魂节省一些时间。

【讨论】:

    【解决方案3】:

    我在我刚刚添加的属性(列)上的 Item 表中遇到了这个问题,真是令人沮丧!

    原来我在 Order 的数据模型中有一个 List 属性,因为我在该配置中没有忽略它,所以它在 Item 表中导致了同样的问题。除非两个表都有同名的属性,否则不会发生这种情况,所以我必须这样做......无论如何我都应该这样做。

    public OrderConfiguration() { Ignore(p => p.Items); }

    【讨论】:

      【解决方案4】:

      大家好,就我而言,我有一个遗留代码 具有相同外键的不同名称的两个类。 添加注释对正确列的引用和其他类中具有相同名称的属性名称。然后注释ForeignKey在两列之间进行匹配。

      [Table("LFile")]
      public partial class LFile
      {
          [Key]
          public int FileId { get; set; }
      
          [StringLength(500)]
          public string FileName { get; set; }
      
          public int? LRRFileDetailId { get; set; }
      
          public byte[] FileData { get; set; }
      
          public FileType Type { get; set; }
      
          [Column("LUpload_Id")] //Foreign Key of the class
          public int? LUploadId { get; set; }
      
          [ForeignKey("LUploadId")] //Foreign key inherited
          public virtual LParserProcess LParserProcess { get; set; }
      }
      

      【讨论】:

        【解决方案5】:

        我收到相同的错误 SqlException 消息,其中数字 1 被附加到我的字段名称。

        一旦我意识到我错误地假设 [ForeignKey] 注释引用数据库中的字段名称,我就能够解决它。相反,它们应该与模型中定义的属性名称匹配。

        例如:

        [Column("Organisation_Account_Manager")]
        [Display(Name = "Organisation_Account_Manager_ID")]
        [DisplayFormat(NullDisplayText = "Unknown")]
        public int? Organisation_Account_Manager_ID { get; set; }
        
        [Display(Name = "Account Manager")]
        [ForeignKey("Organisation_Account_Manager_ID")]
        public Contact Account_Manager { get; set; }
        

        在这个例子中它会起作用,因为 [ForeignKey("Organisation_Account_Manager_ID")] 与 public int 完全匹配?组织_帐户_经理_ID。以前我的 [ForeignKey] 注释使用的是 Organisation_Account_Manager,这是数据库中的字段名称 - 但这是不正确的。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-11-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-08-28
          相关资源
          最近更新 更多