1

EF 4.1 MVC3 を使用してアプリケーションを作成しています。CRUDを実装しようとしています。問題は、最初のインデックス ページだけを実装しようとすると、エラーが発生することです。中央サーバーから外部データベースを使用しています。簡単な登録フォームを試しています。私は最初のモデルクラスRegModel.csを持っています

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

namespace Registration.Models
{
    public class Register
    {
        public int Id { get; set; }
        public string UserName { get; set; }
        public string Password { get; set; }
        public string Email { get; set; }
        public string Address { get; set; }
    }
}

次に、RegModelContext.cs

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

namespace Registration.Models
{
    public class Register
    {
        public int Id { get; set; }
        public string UserName { get; set; }
        public string Password { get; set; }
        public string Email { get; set; }
        public string Address { get; set; }
    }
}

私のコントローラー名はRegController.csです(最初にIndexを実装しようとしています)。アプリケーションを実行するとreturn View(db.Registrant.ToString());、「entitycommandexecutionexception was unhandled」「コマンド定義の実行中にエラーが発生しました。詳細については、内部例外を参照してください」というエラーが表示されました。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Registration.Models;
using System.Configuration;


namespace Registration.Controllers
{
    public class RegController : Controller
    {
        //
        // GET: /Reg/
        private string CmdStr = ConfigurationManager.ConnectionStrings["MyConn"].ConnectionString;      
        public ActionResult Index()
        {
            using (var db = new RegModelContext(CmdStr))
            {
                return View(db.Registrant.ToString());
            }

        }

    }
}

次に、Index を使用して、インデックス (Index.cshtml) のビューを作成します。

@model IEnumerable<Registration.Models.Register>

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <title>Index</title>
</head>
<body>
    <p>
        @Html.ActionLink("Create New", "Create")
    </p>
    <table>
        <tr>
            <th></th>
            <th>
                UserName
            </th>
            <th>
                Password
            </th>
            <th>
                Email
            </th>
            <th>
                Address
            </th>
        </tr>

    @foreach (var item in Model) {
        <tr>
            <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>
            <td>
                @item.UserName
            </td>
            <td>
                @item.Password
            </td>
            <td>
                @item.Email
            </td>
            <td>
                @item.Address
            </td>
        </tr>
    }

    </table>
</body>
</html>

このweb.configファイルを使用しています

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=152368
  -->
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.3.1.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
  </configSections>
  <appSettings>
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.0">
      <assemblies>
        <add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Web.Helpers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Web.WebPages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
      </assemblies>
    </compilation>
    <authentication mode="Forms">
      <forms loginUrl="~/Account/LogOn" timeout="2880" />
    </authentication>
    <pages>
      <namespaces>
        <add namespace="System.Web.Helpers" />
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Routing" />
        <add namespace="System.Web.WebPages" />
      </namespaces>
    </pages>
  </system.web>
  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <modules runAllManagedModulesForAllRequests="true" />
  </system.webServer>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <connectionStrings>
    <add name="MyConn" connectionString="Data Source=WIN-A32JMN2HC3A\SAIGMAMSSQL;Initial Catalog=Registration;User Id=SAIGMA110;Password=12345;"/>    
  </connectionStrings>
</configuration>
4

1 に答える 1

3

問題は次のコードにあります。

string CmdStr = ConfigurationManager.ConnectionStrings["MyConn"].ConnectionString; 
using (var db = new RegModelContext(CmdStr))
{
    return View(db.Registrant.ToString());
}

まず、接続文字列を明示的に指定する必要はありません。慣例により、Entity Framework は web.config ファイルで一致する接続文字列を探します。接続文字列の名前が DbContext の名前と一致していることを確認してください。

次に、Registerデータベース トラフ内のオブジェクトのコレクションにアクセスしていますdb.Registrant。呼んToString()でも意味がありません。

Registration.Models.Registerあなたのビューは、文字列ではなくリストを期待しています。コードを次のように変更します。

using (var db = new RegModelContext())
{
    return View(db.Registrant.ToList());
}

Registerこれにより、データベース内のすべてのアイテムが返されることに注意してください。それは大量のデータになる可能性があります。ページングや別の種類のフィルタリングを実装して、すべてが期待どおりに機能することを確認したい場合があります。

于 2012-08-08T11:05:07.120 に答える