2017-08-16 93 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)DbContext,直至您瞭解EF的工作原理和用途。

當你學習,你可以使用原始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); 
    } 
} 
相關問題