2017-08-16 10 views
0

SpectrumContextはweb.configで定義された接続です。なぜこれが読み込まれていないのか分かりません。エンティティフレームワークで何が起きているのか分かりませんMVCセットアップ

エラー以下投げている私の文字列変数 "結果":

The type 'System.Web.Routing.RouteCollection+IgnoreRouteInternal' and the type 'System.Web.Mvc.RouteCollectionExtensions+IgnoreRouteInternal' both have the same simple name of 'IgnoreRouteInternal' and so cannot be used in the same model. All types in a given model must have unique simple names. Use 'NotMappedAttribute' or call Ignore in the Code First fluent API to explicitly exclude a property or type from the model.

HomeController.cs

using System; 
using System.Collections.Generic; 
using System.Data.Common; 
using System.Data.Entity; 
using System.Data.Entity.Core.EntityClient; 
using System.Data.SqlClient; 
using System.Linq; 
using System.Web.Mvc; 
using WebApplication1.Controllers; 
using WebApplication1.DataAccessLayer; 
namespace WebApplication1.Controllers 
{ 
    public class HomeController : Controller 
    { 
     public ActionResult Index() 
    { 
     BaseContext db1 = new BaseContext("SpectrumContext"); 
     string sql = "select description from dbo.workcategory where workcategoryid = 3"; 

     string results = db1.Database.SqlQuery<string>(sql).First(); 

     return View(results); 
    } 

    public ActionResult About() 
    { 
     ViewBag.Message = "Your application description page."; 

     return View(); 
    } 

    public ActionResult Contact() 
    { 
     ViewBag.Message = "Your contact page."; 

     return View(); 
    } 
} 
} 

BaseContext.cs

using System; 
using System.Collections.Generic; 
using System.Data.Common; 
using System.Data.Entity; 
using System.Data.Entity.Core.EntityClient; 
using System.Data.SqlClient; 
using System.Linq; 
using System.Web; 
`test`using WebApplication1.Controllers; 

namespace WebApplication1.DataAccessLayer 
{ 
public class BaseContext : DbContext 
{ 
    protected string connectionName; 
    public DbSet<HomeController> description { get; set; } 

    /** 
    * Created the connection to the server using the giving connection string name 
    * 
    * @param connName 
    */ 
    public BaseContext(string connName = "SpectrumContext") 
     : base(connName) 
    { 
     connectionName = connName; 
    } 

    /** 
    * Changes the default database 
    * 
    * @param databaseName 
    */ 
    public BaseContext setDatabase(string databaseName) 
    { 
     var connectionString = System.Configuration.ConfigurationManager.ConnectionStrings[connectionName].ConnectionString; 

     SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(connectionString); 

     //change the database before creating the new connection 
     builder.InitialCatalog = databaseName; 

     string sqlConnectionString = builder.ConnectionString; 

     return new BaseContext(sqlConnectionString); 
    } 
} 
} 

Index.cshtml

@{ 
    ViewBag.Title = "Home Page"; 
} 

<div class="jumbotron"> 
<h1>ASP.NET</h1> 

</div> 

<div> 

</div> 
+0

何を(https://stackoverflow.com/questions/30553883/system-web-routing -routecollection-and-system-web-mvc-routecollectionextensions)は役に立ちますか? – maccettura

+0

例外を投げているのはどこですか? – mxmissile

+2

なぜあなたのデータベースにHomeControllerインスタンスを保持しようとしていますか?それは珍しいことです。 –

答えて

0

簡単なSQL文を実行したい場合は、EFがどのように動作し、どのようなものが使用されているかを理解するまで、(EF)DbContextをここで削除してください。

あなたは、このような生のADO使用して簡単なSQL文を実行することができ、学習している間:[こちら]から

const string sql = "select description from dbo.workcategory where workcategoryid = 3"; 
var connectionString = ConfigurationManager.ConnectionStrings["SpectrumContext"].ConnectionString; 
using (var cn = new SqlConnection(connectionString)) 
{ 
    using (var cmd = new SqlCommand(sql, cn)) 
    { 
     var results = Convert.ToString(cmd.ExecuteScalar()); 

     return View(results); 
    } 
} 
関連する問題