实体框架无效的列名,EF在主键上添加了数字1

23

我有这两个实体:

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: Invalid column name 'Suscriptores_IdSuscriptor1'

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

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

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


2
只是好奇,这不应该是多对多的关联吗?我不想发布一些东西,知道我只能得到一个订阅者 :) - Gert Arnold
这是今天第三个+1了,Gert。我喜欢你的帖子 :-) - phil soady
谢谢Gert的“评论”,我对EF还很陌生,只有一点经验。每个“订阅者”都有许多“发布”,而每个“发布”都有一个“订阅者”,这就是我想建模的内容。 - Juan Pablo Gomez
好的,就这样吧。在Fluent API中映射外键的方式是映射不属于类模型的关键字段。使用属性进行映射应该没问题。你是否为了简单起见遗漏了PublicacionSuscriptores之间的其他关联? - Gert Arnold
不,这是它们之间唯一的关联。 - Juan Pablo Gomez
5个回答

12

请尝试添加一个多对一的映射关系,仅使用纯Fluent API,并且应该删除[ForeignKey]注释。

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

3
你能否解释一下为什么你说他们应该使用纯Fluent API?注解有什么问题吗? - AFetter

11
在非外键列中,我遇到了这个错误,并浪费了很多时间来尝试找出错误。但其实这个错误是由于我的代码问题,而不是 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。无论我怎么看,都看不出自己的错误。希望这篇文章能帮助其他人节省时间。


5

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

事实证明,我的数据模型中有一个Order的List属性,因为我没有在该配置中忽略它,所以导致了Item表中同样的问题。如果两个表没有相同名称的属性,这种情况是不会发生的,所以我必须这样做...其实我本来就应该这样做。

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


2

大家好,在我的案例中,我有一个遗留代码,其中有两个类使用了同一外键但名称不同。通过添加注释来引用正确的列和其他类中具有相同名称的属性的名称,然后使用注释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; }
}

2
我得到了相同的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? Organisation_Account_Manager_ID。以前我的 [ForeignKey] 注释是使用 Organisation_Account_Manager,而这是数据库的字段名 -- 但这是不正确的。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接