2

当我尝试运行http://mvcmusicstore.codeplex.com/releases/view/64379中列出的 MVC Music Store 应用程序时出现异常。一旦我包含SampleData.cs在 Models 文件夹中并进行一些更改以包含此模型,我就会收到 ArgumentException,路径中的非法字符。有没有类似的经历?有人可以帮助解决这个问题,因为我现在被这个问题困扰了一段时间。

谢谢

请输入任何内容

确切的代码如下,

public class MusicStoreEntities : DbContext
{
    public DbSet<Album> Albums { get; set; }
    public DbSet<Genre> Genres { get; set; }
}

我将SampleDat.cs文件添加到我的模型中。这是一个巨大的文件,我在这里只列出了其中的一部分。

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

 namespace MvcMusicStore.Models
 {
     public class SampleData : DropCreateDatabaseIfModelChanges<MusicStoreEntities>
     {
        protected override void Seed(MusicStoreEntities context)
        {
            var genres = new List<Genre>
            {
                new Genre { Name = "Rock" },
                new Genre { Name = "Jazz" }
            };

            var artists = new List<Artist>
            {
                new Artist { Name = "Aaron Copland & London Symphony Orchestra" },
                new Artist { Name = "Aaron Goldberg" }
            };

            new List<Album>
            {
                new Album { Title = "The Best Of Men At Work", 
                            Genre = genres.Single(g => g.Name == "Rock"), 
                            Price = 8.99M,
                            Artist = artists.Single(a => a.Name == "Men At Work"),
                            AlbumArtUrl = "/Content/Images/placeholder.gif"
                          },
                new Album { Title = "A Copland Celebration, Vol. I",
                            Genre = genres.Single(g => g.Name == "Classical"),
                            Price = 8.99M,
                            Artist = artists.Single(a => a.Name == "Aaron Copland & London Symphony Orchestra"),
                            AlbumArtUrl = "/Content/Images/placeholder.gif"
                          }
            }.ForEach(a => context.Albums.Add(a));
        }
    }
}

我在 中设置了一个数据初始化器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
4

2 回答 2

2

解决方案在这篇文章 [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; }
    }
}
于 2013-04-08T08:39:09.873 回答
1

当我阅读本教程时,由于添加到 Web.Config 文件中的行中有错字而出现此错误。确保您的<connectionString>节点看起来完全像这样:

  <connectionStrings>
    <add name="MusicStoreEntities"
         connectionString="Data Source=|DataDirectory|MvcMusicStore.sdf"
         providerName="System.Data.SqlServerCe.4.0" />
  </connectionStrings>
于 2012-11-29T20:07:21.303 回答