实体框架无效的列名称

实体框架无效的列名称

本文介绍了实体框架无效的列名称,EF将numeric 1添加到主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这两个实体

    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'

System.Data.SqlClient.SqlException: Invalid column name 'Suscriptores_IdSuscriptor1'

试图解决这个问题我在DBCOntext中添加流利

Trying to solve this problem I add this fluent at DBCOntext

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

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

But the problem persist. How Can I do to solve this.

推荐答案

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

Try add a many-to-one mapping as well. Please use pure Fluent API, and you should remove the [ForeignKey] annotations.

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

这篇关于实体框架无效的列名称,EF将numeric 1添加到主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 23:44