asp.net-mvc-3 - 在模型中包含 SampleData.cs 后尝试运行 MVC Music Store 时出现异常

标签 asp.net-mvc-3

当我尝试运行 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());
} 

在我的 Controller 中,StoreController.cs我正在做

var genres = storeDB.Genres.ToList();   

并得到以下异常。

Exception is "Illegal characters in path."

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentException: Illegal characters in path.

Source Error:

public ActionResult Index()
{
    var genres = storeDB.Genres.ToList();
    return View(genres);
}

Stack Trace:

[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

最佳答案

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

关于asp.net-mvc-3 - 在模型中包含 SampleData.cs 后尝试运行 MVC Music Store 时出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11547732/

相关文章:

asp.net-mvc-3 - 如何只重新加载页面的一部分

asp.net - 在 web.config 中指定相对路径

c# - MVC 3 : What's the most common way of asynchronously loading form fields based on previous choices?

asp.net-mvc-3 - 即使文件不存在,MVC3/T4 自定义脚手架也会给出文件存在错误

c# - MvcContrib TestHelper 在使用 AssertViewRendered 时给出一个奇怪的错误

asp.net-mvc - 如何在 Url.Action 中发送多个参数?

c# - 在 html 文本框 mvc 中使用短日期字符串

views - 如何在设计器 View 中打开 .cshtml?

asp.net-mvc-3 - 使用Azure ACS时如何解决 "Key not valid for use in specified state"错误?

c# - 如何编码 Time > 8 :15