Asp.net mvc 3 在模型中包含SampleData.cs后尝试运行MVC音乐商店时出现异常

Asp.net mvc 3 在模型中包含SampleData.cs后尝试运行MVC音乐商店时出现异常,asp.net-mvc-3,Asp.net Mvc 3,我尝试运行中列出的MVC音乐商店应用程序时遇到异常。当我在Models文件夹中包含SampleData.cs并进行一些更改以包含此模型时,我得到一个ArgumentException,路径中的非法字符。有类似的经历吗?有人能帮我解决这个问题吗?因为我已经被这个问题困扰了一段时间了 谢谢 有什么意见吗 具体代码如下: public class MusicStoreEntities : DbContext { public DbSet<Album> Albums { get; s

我尝试运行中列出的MVC音乐商店应用程序时遇到异常。当我在Models文件夹中包含
SampleData.cs
并进行一些更改以包含此模型时,我得到一个ArgumentException,路径中的非法字符。有类似的经历吗?有人能帮我解决这个问题吗?因为我已经被这个问题困扰了一段时间了

谢谢

有什么意见吗

具体代码如下:

public class MusicStoreEntities : DbContext
{
    public DbSet<Album> Albums { get; set; }
    public DbSet<Genre> Genres { get; set; }
}
我已在
Global.asax
中设置了数据初始值设定项

protected void Application_Start()
{
    System.Data.Entity.Database.SetInitializer(new MvcMusicStore.Models.SampleData());
} 
在我的控制器中,
StoreController.cs
我正在执行

var genres = storeDB.Genres.ToList();   
并得到以下例外

例外情况是“路径中的非法字符”

描述:执行当前web请求期间发生未处理的异常。请查看堆栈跟踪以了解有关错误的更多信息以及错误在代码中的起源

异常详细信息:System.ArgumentException:路径中存在非法字符

源错误:

public ActionResult Index()
{
    var genres = storeDB.Genres.ToList();
    return View(genres);
}
堆栈跟踪:

[ArgumentException: Illegal characters in path.]
System.IO.Path.CheckInvalidPathChars(String path) +126
System.IO.Path.NormalizePath(String path, Boolean fullCheck, Int32 maxPathLength) +145
System.IO.Path.GetFullPathInternal(String path) +46
System.IO.Path.GetFullPath(String path) +33
System.Data.SqlServerCe.SqlCeUtil.DemandForPermission(String filename, FileIOPermissionAccess permissions) +71
System.Data.SqlServerCe.SqlCeEngine.CreateDatabase() +1217
System.Data.SqlServerCe.SqlCeProviderServices.DbCreateDatabase(DbConnection connection, Nullable`1 timeOut, StoreItemCollection storeItemCollection) +288
System.Data.Objects.ObjectContext.CreateDatabase() +84
System.Data.Entity.Internal.DatabaseOperations.Create(ObjectContext objectContext) +35
System.Data.Entity.Database.Create() +70
System.Data.Entity.DropCreateDatabaseIfModelChanges`1.InitializeDatabase(TContext context) +477
System.Data.Entity.<>c__DisplayClass2`1.<SetInitializerInternal>b__0(DbContext c) +143
System.Data.Entity.Internal.<>c__DisplayClass5.<PerformDatabaseInitialization>b__3() +59
System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action) +101
System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization() +260
System.Data.Entity.Internal.LazyInternalContext.<InitializeDatabase>b__4(InternalContext c) +31
System.Data.Entity.Internal.RetryAction`1.PerformAction(TInput input) +147
System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action`1 action) +276
System.Data.Entity.Internal.LazyInternalContext.InitializeDatabase() +112
System.Data.Entity.Internal.InternalContext.Initialize() +41
System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) +34
System.Data.Entity.Internal.Linq.InternalSet`1.Initialize() +148
System.Data.Entity.Internal.Linq.InternalSet`1.GetEnumerator() +33
System.Data.Entity.Infrastructure.DbQuery`1.System.Collections.Generic.IEnumerable<TResult>.GetEnumerator() +91
System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) +315
System.Linq.Enumerable.ToList(IEnumerable`1 source) +58
MvcMusicStore.Controllers.StoreController.Index() in C:\My Projects\MvcMusicStore\MvcMusicStore\Controllers\StoreController.cs:16
lambda_method(Closure , ControllerBase , Object[] ) +96
System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +17
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +208
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +27
System.Web.Mvc.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12() +55
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +263
System.Web.Mvc.<>c__DisplayClass17.<InvokeActionMethodWithFilters>b__14() +19
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +191
System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +343
System.Web.Mvc.Controller.ExecuteCore() +116
System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +97
System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +10
System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +37
System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +21
System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +12
System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
System.Web.Mvc.<>c__DisplayClasse.<EndProcessRequest>b__d() +50
System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f) +7
System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +22
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +60
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8970061
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184
[ArgumentException:路径中的非法字符。]
System.IO.Path.CheckInvalidPathChars(字符串路径)+126
System.IO.Path.NormalizePath(字符串路径,布尔fullCheck,Int32 maxPathLength)+145
System.IO.Path.GetFullPathInternal(字符串路径)+46
System.IO.Path.GetFullPath(字符串路径)+33
System.Data.SqlServerCe.SqlCeUtil.DemandForPermission(字符串文件名,FileIOPermissionAccess权限)+71
System.Data.SqlServerCe.SqlCeEngine.CreateDatabase()+1217
System.Data.SqlServerCe.SqlCeProviderServices.DbCreateDatabase(DbConnection连接,可空`1超时,StoreItemCollection StoreItemCollection)+288
System.Data.Objects.ObjectContext.CreateDatabase()+84
System.Data.Entity.Internal.DatabaseOperations.Create(ObjectContext)+35
System.Data.Entity.Database.Create()+70
System.Data.Entity.DropCreateDatabaseIfModelChanges`1.InitializeDatabase(TContext上下文)+477
System.Data.Entity.c__uDisplayClass2`1.b__0(DbContext c)+143
System.Data.Entity.Internal.c__;u DisplayClass5.b__;u 3()+59
System.Data.Entity.Internal.InternalContext.PerforminizationAction(Action Action)+101
System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization()+260
System.Data.Entity.Internal.LazyInternalContext.b_u4(InternalContext c)+31
System.Data.Entity.Internal.RetryAction`1.PerformAction(TInput输入)+147
System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action`1 Action)+276
System.Data.Entity.Internal.LazyInternalContext.InitializeDatabase()+112
System.Data.Entity.Internal.InternalContext.Initialize()+41
System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(类型entityType)+34
System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()+148
System.Data.Entity.Internal.Linq.InternalSet`1.GetEnumerator()+33
System.Data.Entity.Infrastructure.DbQuery`1.System.Collections.Generic.IEnumerable.GetEnumerator()+91
System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)+315
System.Linq.Enumerable.ToList(IEnumerable`1源)+58
C:\My Projects\MvcMusicStore\MvcMusicStore\Controllers\StoreController.cs中的MvcMusicStore.Controllers.StoreController.Index():16
lambda_方法(闭包、控制器基、对象[])+96
System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase控制器,对象[]参数)+17
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext ControllerContext,IDictionary`2参数)+208
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext ControllerContext,ActionDescriptor ActionDescriptor,IDictionary`2参数)+27
System.Web.Mvc.c__显示类15.b__12()+55
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter筛选器、ActionExecutingContext预文本、Func`1 continuation)+263
System.Web.Mvc.c__显示类17.b__14()+19
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext ControllerContext,IList`1筛选器,ActionDescriptor ActionDescriptor,IDictionary`2参数)+191
System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext ControllerContext,String actionName)+343
System.Web.Mvc.Controller.ExecuteCore()+116
System.Web.Mvc.ControllerBase.Execute(RequestContext-RequestContext)+97
System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext RequestContext)+10
System.Web.Mvc.c__显示类b.b__5()+37
System.Web.Mvc.Async.c__显示类1.b__0()+21
System.Web.Mvc.Async.c_uuudisplayClass8`1.b_uuu7(IAsyncResult)+12
System.Web.Mvc.Async.WrappedAsyncResult`1.End()+62
System.Web.Mvc.c_uuudisplayClasse.b_uuud()+50
System.Web.Mvc.SecurityUtil.b___0(操作f)+7
System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(操作)+22
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult)+60
System.Web.Mvc.MvcHandler.System.Web.IHTTPassynchandler.EndProcessRequest(IAsyncResult结果)+9
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()+8970061
System.Web.HttpApplication.ExecuteStep(IExecutionStep步骤,布尔值&同步完成)+184

当我阅读本教程时,由于我添加到Web.Config文件中的行中的一个输入错误,我出现了这个错误。确保您的
节点与以下内容完全相同:

  <connectionStrings>
    <add name="MusicStoreEntities"
         connectionString="Data Source=|DataDirectory|MvcMusicStore.sdf"
         providerName="System.Data.SqlServerCe.4.0" />
  </connectionStrings>

答案就在这篇文章上 [http://mvcmusicstore.codeplex.com/discussions/359070][1]

确保MusicStoreEntities将DbContext实现为:

namespace MvcMusicStore.Models
{
    public class MusicStoreEntities : DbContext
    {
        public DbSet<Album> Albums { get; set; }
        public DbSet<Genre> Genres { get; set; }
    }
}
名称空间MvcMusicStore.Models
{
公共类MusicStoreEntities:DbContext
{
公共数据库集相册{get;set;}
公共数据库集类型{get;set;}
}
}

解决此异常有什么帮助吗?@user152884您得到的异常是什么,并将相关代码发布到您正在执行的操作中。添加了代码、异常详细信息和堆栈跟踪,键入
数据目录
,而不是
namespace MvcMusicStore.Models
{
    public class MusicStoreEntities : DbContext
    {
        public DbSet<Album> Albums { get; set; }
        public DbSet<Genre> Genres { get; set; }
    }
}