c# - ASP.NET Massive - 如何创建数据库以显示 VIEW

标签 c# asp.net-mvc-3 massive northwind

大家好,我想寻求帮助,因为我不知道如何在 ASP.NET MVC 3 中使用 MASSIVE.CS 创建可以使用的数据库。

我一直在弄清楚如何将这个 MASSIVE 类实现到我的 MVC 项目中,以及连接字符串如何连接到 northwind 数据库中。我只有this tutorial https://github.com/robconery/massive但我仍然无法理解。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MovieMassive.Models;

namespace MovieMassive.Controllers
{
    public class MoviesController : Controller
    {

        MovieTable dbMovies = new MovieTable();

        public ActionResult Index()
        {
            dbMovies.Query("SELECT * FROM MovieTable");
            return View(dbMovies);
        }

        public ActionResult Add() {
            return View();
        }
    }
}

Connection string:

<add name="MyConnectionString" connectionString="data source=|DataDirectory|MyDatabase.sdf"  providerName="System.Data.SqlServerCe.4.0"/>

电影表类:

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

namespace MovieMassive.Models
{
    public class MovieTable : DynamicModel
    {
        public MovieTable() : base("movieMassive", "MovieTable", "ID") { }
    }
}

好的,这就是我想要的,你能帮助我如何正确运行 View Index.cshtml。属性还没有。因为不知道用数据库来实现它...任何帮助将不胜感激..

@model IEnumerable<MovieMassive.Models.MovieTable>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>

<table>
    <tr>
        <th>Title</th>
        <th>ReleaseDate</th>
        <th>Genre</th>
        <th>Price</th>
        <th>Rating</th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Title)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.ReleaseDate)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Genre)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Price)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
            @Html.ActionLink("Details", "Details", new { id=item.ID }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.ID })
        </td>
    </tr>
}

</table>

我需要这个正确运行以使用 massive 创建一个基本的 CRUD。提前致谢..

最佳答案

在代码更新成问题后更新 OP 评论

Jed,把你的 MoviesController 改成这个

public class MoviesController : Controller
{
    public ActionResult Index()
    {
        MovieTable dbMovies = new MovieTable();
        dbMovies.All();
        return View(dbMovies);
    }
    ...
}

访问 TekPub.com,观看 MVC 2 视频(因为它们是免费的),因为 Rob Conery 向您展示了如何将 Massive 文件实现到 MVC 应用程序中

这是直接链接

ASP.NET MVC Concepts - free

Rob 的 Massive.cs 文件基本上是一个 ORM(对象关系映射)工具,用于查询数据存储中的表并将它们用作应用程序中的对象。

实现起来很容易。你需要:

  • web.config 中指向数据库的 ConnectionString。下面是一个示例 SQL 连接字符串:

    <connectionStrings>
        <add name="MyConnectionString" 
             connectionString="Data Source=DNSServerName;Initial Catalog=DatabaseName;user id=Username;password=Password" 
             providerName="System.Data.SqlClient" />
    </connectionStrings>
    

如果您使用的不是 MS SQL,您可以针对您的特定平台访问 connectionstrings.com

  • 将从 GitHub 下载的 Massive.cs 文件放到您的应用程序中。您的解决方案如何划分将对您添加文件的位置产生影响。如果您在 ASP.NET MVC 应用程序中只使用一个项目,那么您可以将它添加到项目根目录中,或者为它创建一个文件夹。如果我没记错的话,Rob 使用了一个名为 Infrastructure 的文件夹。

注意 - 大多数现实世界的应用程序都由一个 UI 项目、一个业务逻辑层和一个数据访问层组成。如果您正在使用这样的 N 层解决方案,那么 Massive.cs 文件应该进入您的 DAL。

  • 准备好了,这就是实现 Massive 的全部内容!

用法

要在您的应用中使用 massive,只需执行以下操作: 我正在使用单个项目 MVC 应用程序的简单示例,而不是分离层。

public ActionResult Index() {
    var table = new Products();
    var products = table.All();
    return View(products);
}

同样,这不是最严格的用法。您应该学习所有关于使用 MVC 模式的知识,以及如何将您的解决方案/应用程序构造成正确的设计。通常,您会使用具有 Product 属性的 ViewModel,然后将从 Massive 调用返回的 products 映射到 table.All(); 到你的 ViewModel

这是一个简单的速成类(class),希望它对某人有所帮助:-)

关于c# - ASP.NET Massive - 如何创建数据库以显示 VIEW,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10778103/

相关文章:

c# - ASP.NET MVC View 在产品环境中未返回

c# - WebAPI 发布 JSON 字符串并将其映射到模型

c# - 设计用于 F# 和 C# 的 F# 库的最佳方法

c# - 清除 onMouseLeave JQuery 的大纲

massive - 如何使用 Massive ORM 更新带有复合键的表?

c# - 将 Massive 与 SQLite 一起使用时出现 "Database is locked"异常

c# - 如何使用 C# 删除前导零

javascript - 如何使用 jQuery 和 Asp.Net MVC 3 实现提供额外动态生成字段的客户端注册表单?

C# MVC 3 : Prevent magic string in property attribute