从数据库ASP.NET MVC填充模型

时间:2017-01-28 22:32:21

标签: asp.net-mvc entity-framework

我有以下型号:

namespace Factura.Models
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Data.Entity.Spatial;

    [Table("factura.marfa")]
    public partial class marfa
    {    
        [Key]
        public int idmarfa { get; set; }

        [StringLength(45)]
        public string denumire { get; set; }

        public int idfurnizor { get; set; } 
    } 
}

我还有一个继承DbContext的类:

namespace Factura.Models
{
    using System;
    using System.Data.Entity;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Linq;

    public partial class FacturaContext : DbContext
    {
        public FacturaContext() : base("name=FacturaContext")
        {
        }

        public virtual DbSet<marfa> marfas { get; set; }

        [...]

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            [...]
        }
    }
}

在我的控制器中,我有以下代码:

namespace Factura.Controllers
{
    [Authorize]
    public class CumparatorController : Controller
    {
        FacturaContext dbFactura = new FacturaContext();

        [HttpGet]
        public ActionResult Cumparaturi()
        {
            var marfas = dbFactura.marfas.ToList();
            return View(marfas);
        }
    }
}

我遇到的问题是dbFactura.marfas为空,即使填充了表,它也不会从数据库中带来任何内容。

任何人都可以帮我吗? 谢谢!

1 个答案:

答案 0 :(得分:0)

从marfa课程中移除[Table("factura.marfa")],并在OnModelCreating的{​​{1}}中添加以下内容

FacturaContext

创建迁移&amp;运行

相关问题