2014-02-25 111 views
0

我试图执行我的MVC应用程序,但当我想要显示 类别时,出现了NULL异常。我遵循MVC音乐商店的例子,但我没有使用他们的数据库数据。我有 一个称为产品的表格和一个称为类别的表格。我添加了2个产品和2个类别。 我想通过INDEX显示类别。我错过了一些东西,但我找不到它。 任何方式,我们可以验证数据库是否正确连接,这似乎不是这里的情况。数据库连接似乎不能正常工作

这里是我的StoreController我的指数

namespace WebStore.Controllers 
{ 
    public class StoreController : Controller 
    { 
     WebStoreEntites TP1StoreDB = new WebStoreEntites(); 

     // 
     // GET: /Store/ 

     public ActionResult Index() 
     { 
      var categories = TP1StoreDB.Categories.ToList(); <== this show a null result 

      return View(categories); 
     } 

这里是一个映射我的DB

<connectionStrings> 
<add name="WebStoreEntities" connectionString="Data 
    Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\TP1StoreDB.mdf;Integrated 
    Security=True;User Instance=True" providerName="System.Data.SqlClient"/> 
</connectionStrings> 

它曾经是这样的前

<connectionStrings> 
    <add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString="Data 
    Source=(LocalDb)\v11.0;Initial Catalog=aspnet-WebStore-20140225063823;Integrated 
    Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-WebStore-20140225063823.mdf" /> 
</connectionStrings> 

这里是在webconfig WebStoreEntities类

using System.Data.Entity; 

    namespace WebStore.Models 
    { 
    public class WebStoreEntites 
    { 
     public DbSet<Produit> Produits { get; set; } 
     public DbSet<Categorie> Categories { get; set; } 
    } 
} 

以下是错误:'System.ArgumentNullException'类型的异常出现在System.Core.dll但在用户代码

这里没有处理是categorie类

namespace WebStore.Models 
{ 
    public partial class Categorie 
    { 
     public int CategorieId { get; set; } 
     public string NomCategorie { get; set; } 
     public List<Produit> Produits { get; set; } 
    } 
} 

而且produit.cs

namespace WebStore.Models 
{ 
    public class Produit 
    { 
     public int ProduitId { get; set; } 
     public int CategorieId { get; set; } 
     public string NomProduit { get; set; } 
     public Categorie Categorie { get; set; } 
     public float Prix { get; set; } 
     public int Quantite { get; set; } 
    } 
} 
+0

请例外。讽刺地说 - 你是连接的,只是数据库是错误的,所以这个问题是完全错误的。数据库连接没有问题。查看数据。 – TomTom

+0

@TomTom我加了异常 – user3127986

+0

不够。这是开始得到FANCY。说,你真的玩sqlexpress和atachdbfilename吗?我希望这是作为例子,而不是生产的东西。但是,不,那个错误是 - 没有乐趣。 DT建立一个手动数据库连接,并通过旧的adi.net代码建立一个连接并做一个选择来检查该部分是否工作。 – TomTom

回答

0

我在WebStoreEntities类中丢失了DbContext

public class WebStoreEntites : DbContext <=== missing the DbContext 
相关问题