【问题标题】:Entity Framework - Code First 4.1 - ASP.NET MVC3实体框架 - 代码优先 4.1 - ASP.NET MVC3
【发布时间】:2011-04-05 14:20:56
【问题描述】:

我在 MVC3 中编程,我有以下模型:

 public class L_CabecRegistoPedido
    {
        [Key]
        public int Id { get; set; }
        public int Numero { get; set; }
        public int Ano { get; set; }
        public int Utilizador { get; set; }
        public String Cliente { get; set; }
        public String NomeEmpresa { get; set; }
        public String MarcaEmpresa { get; set; }
        public int? Marca { get; set; }
        public String Projecto { get; set; }
        public String Responsavel { get; set; }
        public String EmailResp { get; set; }
        public String TelefoneResp { get; set; }
        public String DepartamentoEmpresa { get; set; }
        public int? Departamento { get; set; }
        public DateTime DataRegisto { get; set; }
        public int? EstadoPedido { get; set; }
        public int? FasePedido { get; set; }
        public DateTime DataEntregaRequisitada { get; set; }
        public DateTime DataEntrega { get; set; }
        public String Observacoes { get; set; }
        public bool Transformacao { get; set; }
        public int Versao { get; set; }
        public List<L_LinhaRegistoPedido> Linhas { get; set; }

    }

    public class L_LinhaRegistoPedido
    {
        [Key]
        public int Id { get; set; }
        public int? IdCabec { get; set; } /* **Foreign Key to L_CabecRegistoPedido ** */
        public int Utilizador { get; set; }
        public String Artigo { get; set; }
        public String CabDescricao { get; set; }
        public String Descricao { get; set; } /* TODO: espaço neste campo*/
        public Double Quantidade { get; set; }
        public DateTime DataRegisto { get; set; }
        public DateTime DataEntregaRequisitada { get; set; }
        public DateTime DataEntrega { get; set; }
        public int? EstadoLinhasPedido { get; set; }
        public int? FaseLinhasPedido { get; set; }
        public String Observacoes { get; set; }
        public bool Transformacao { get; set; }
        public L_CabecRegistoPedido CabecRegisto { get; set; }
        public List<L_SubLinhaRegistoPedido> SubLinhas { get; set; }

    }

然后我像这样使用 DBContext:

 public class AppADO : DbContext
    {

        public AppADO(String ConnectionStringName)
            : base(ConnectionStringName) { }

        public DbSet<L_CabecRegistoPedido> L_CabecRegistoPedido { get; set; }
     }

当我在控制器上进行 SaveChanges() 时,我有这个代码:

 [HttpPost]
        public ActionResult Create(FormCollection formCollection)
        {

            L_LinhaRegistoPedido linhaRegisto = new L_LinhaRegistoPedido();
            try
            {
                if (ModelState.IsValid)
                {
                    if (String.IsNullOrEmpty(formCollection["IDArtigo"])) linhaRegisto.Artigo = null; else linhaRegisto.Artigo = formCollection["IDArtigo"];

                    linhaRegisto.IdCabec =   int.Parse(formCollection["IDRegistoPedido"]);
                    linhaRegisto.Quantidade = Double.Parse(formCollection["IDQuantidade"]);
                    linhaRegisto.CabDescricao = formCollection["IDCabDescricao"];
                    linhaRegisto.Descricao = formCollection["IDDescricao"];
                    linhaRegisto.DataRegisto = DateTime.Parse(formCollection["IDDataRegisto"]);
                    linhaRegisto.DataEntrega = DateTime.Parse(formCollection["IDDataEntrega"]);
                    linhaRegisto.DataEntregaRequisitada = DateTime.Parse(formCollection["IDDataEntregaRequisitada"]);

                    if (string.IsNullOrEmpty(formCollection["IDEstadoLinha"])) linhaRegisto.EstadoLinhasPedido = null; else linhaRegisto.EstadoLinhasPedido = int.Parse(formCollection["IDEstadoLinha"]);
                    if (string.IsNullOrEmpty(formCollection["IDFaseLinha"])) linhaRegisto.FaseLinhasPedido = null; else linhaRegisto.FaseLinhasPedido = int.Parse(formCollection["IDFaseLinha"]);

                    linhaRegisto.Observacoes = formCollection["IDObservacoes"];

                    appAdo.L_LinhaRegistoPedido.Add(linhaRegisto);
                    appAdo.SaveChanges();
                }

                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", ex);
                return View(linhaRegisto);
            }
        }

但我得到了错误:

“列名 'CabecRegisto_Id' 无效。”

此列是由实体框架生成的,我不知道如何将 L_LinhaRegistoPedido ( IdCabec ) 的外键关联到 L_CabecRegisto 以便实体框架找到外键并且不自动生成这个..

【问题讨论】:

    标签: entity-framework asp.net-mvc-3


    【解决方案1】:

    尝试使用ForeignKeyAttribute 明确标记该列,例如:

    public class L_LinhaRegistoPedido
    {
        [Key]
        public int Id { get; set; }
    
        /* **Foreign Key to L_CabecRegistoPedido ** */
        [ForeignKey("CabecRegisto")] 
        public int? IdCabec { get; set; } 
        public L_CabecRegistoPedido CabecRegisto { get; set; }       
    
        // All other properties omitted 
    }
    

    ForeignKeyAttribute 的构造函数中,我放置了属性名称CabecRegistro,我假设它充当IdCabec 的导航属性。您可能需要设置CabecRegistro virtual 来启用Layzy 加载。

    public virtual L_CabecRegistoPedido CabecRegisto { get; set; }
    

    顺便说一句,如果您完全删除 IdCabec,EF 应该负责在数据库本身中创建外键关系。在这种情况下,您当然会失去对命名的控制权。

    【讨论】:

      猜你喜欢
      • 2011-08-02
      • 2011-08-05
      • 1970-01-01
      • 1970-01-01
      • 2012-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多