2017-01-28 8 views
0

I以下のモデルがあります:データベースASP.NET MVCからモデルを読み込む

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) 
     { 
      [...] 
     } 
    } 
} 

をI、次のコードを持っています:

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

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

私が持っている問題は、それがanythをもたらすものではありません、dbFactura.marfasがnullであるということですテーブルにデータが入力されていても、データベースから取得できます。

誰もがこれを手伝ってくれますか? ありがとうございました!

+0

コードが正常に見える移行&実行を作成FacturaContext

modelBuilder.Entity<marfa>().ToTable("factura_marfa"); //updated . with _ 

であなたのOnModelCreatingで次のマーファクラスから[Table("factura.marfa")]を削除し、追加します。それが「ヌル」であることをどのように確認しましたか? – Shyju

+0

クラッシュし、その行にブレークポイントを設定します – Cristina

答えて

0

+0

どのように移行を作成しますか? – Cristina

+0

最初にコードを使用している場合は、パッケージマネージャコンソールに移動し、 'FacturaContext'が存在するプロジェクトを選択して、' add-migration changes'を実行してから 'update-database'を実行してください –

+0

初めて移行を成功させましたが、それでも何ももたらさなかったのですが、ToTable( "factura_marfa")の代わりにToTable( "factura.marfa") 'を間違えて入力しました。そして、今、 'update-database'で私はエラーが発生しました: "オブジェクト参照がオブジェクトのインスタンスに設定されていません。" – Cristina

関連する問題