【问题标题】:Exception when attempted to run MVC Music Store after including SampleData.cs in Model在模型中包含 SampleData.cs 后尝试运行 MVC 音乐商店时出现异常
【发布时间】:2012-07-18 18:13:19
【问题描述】:

当我尝试运行 http://mvcmusicstore.codeplex.com/releases/view/64379 中列出的 MVC 音乐商店应用程序时遇到异常。当我在 Models 文件夹中包含 SampleData.cs 并进行一些更改以包含此模型时,我得到一个 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

【问题讨论】:

  • 任何帮助解决这个异常???
  • @user152884 你得到的异常是什么,并将相关代码发布到你正在做的事情中。
  • 添加了代码、异常细节和堆栈跟踪
  • 我在连接字符串中输入了Data Directory 而不是DataDirectory。当我纠正我能够继续前进时。

标签: asp.net-mvc-3


【解决方案1】:

解决方案在这篇文章中 [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; }
    }
}

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 这对我有用。我在连接字符串中输入了Data Directory 而不是DataDirectory。当我纠正我能够继续前进时。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多