开发者

ASP.net MVC 3 is not creating a SQL table automatically (Following Pluralsight)

I am trying to follow this guide http://www.pluralsight-training.net/microsoft/players/PSODPlayer.aspx?author=scott-allen&name=mvc3-building-data-i&mode=live&clip=0&course=aspdotnet-mvc3-intro (Part 3 Code Comes first) but I have followed it like a slave.

Now I have installed a MSDN version so its the Visual Studio Ultimate and not the Web developer express 2010 like he uses and I wonder is that the sole reason for this not working? Because then I will just install that instead.

I get this error when I try to access a site that should use the database

    Server Error in '/' Application.
Value cannot be null.
Parameter name: key
Description: An unhandled exception occurred during the

execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentNullException: Value

cannot be null. Parameter name: key

Source Error:

Line 15:     
Line 16: 
Line 17: @foreach (var item in Model)
Line 18: {
Line 19:     @item.Title


Source File: c:\Users\Mech0z\Documents\Visual

Studio 2010\Projects\FirstWeb\FirstWeb\Views\Gallery\Index.cshtml Line: 17

Stack Trace:

My code is:

Connectionstring:

<connectionStrings>
    <add name="ApplicationServices"
         connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"
         providerName="System.Data.SqlClient" />
    <add name="GalleryDb"
         connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;initial catalog=GalleryDb"
         providerName="System.Data.SqlClient"/>
  </connectionStrings>

Picture model

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace FirstWeb.Models
{
    public class Picture
    {
        public int ID { get; set; }
        public string Title { get; set; }
        public string Path { get; set; }
        public List<Comment> Comments { get; set;}
        public int ConcertYear { get; set; }
        public HttpPostedFileBase File { get; set; }
        public string UploadBy { get; set; }
    }
}

My controller

namespace FirstWeb.Controllers
{
    public class GalleryController : Controller
    {
        GalleryDb _db = new GalleryDb();
        //
        // GET: /Gallery/

        public ActionResult Index()
        {
            var model = _db.Pictures;
            return View(model);
        }

And my galleryDB

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;

namespace FirstWeb.Models
{
    public class GalleryDb : DbContext
    {
        public DbSet<Picture> Pictures { get; set; }
        public DbSet<Comment> Comments { get; set; }

        public DbSet<Picture> GetPictures()
        {
            return Pictures;
        }

        public void AddPicture(Picture model)
        {
            Pictures.Add(new Picture
            {
                Title = model.Title,
                Path = model.Path,
                Comments = new List<Comment>(),
                ConcertYear = model.ConcertYear
            });
        }
    }
}

I have a few extra methods thats not workign whicih is just something from when I used temp data without SQL

But as said I cant connect to the db the way he does (Typing .\sqlexpress and typing GalleryDb as name) and it gives me an error

But the da开发者_如何学Ctabase must be running as I can create a user and login at any point


Apparently the Data Entity do not like this property

public HttpPostedFileBase File { get; set; }

So after removing that from my model it worked fine

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜