运行 Visual Studio 2013 ASP.NET MVC 4 Entity Framework (EF) 5
运行我的项目后,导航到“创建”页面时。我尝试将信息发布到本地mysql数据库,抛出这个异常...
An exception of type 'System.TypeInitializationException' occurred in System.Data.Entity.dll but was not handled in user code
Additional information: The type initializer for 'ExtentPlaceholderCreator' threw an exception.
在我的 AuctionController.cs 文件中的这个位置
[HttpPost]
public ActionResult Create([Bind(Exclude="CurrentPrice")]Models.Auction auction)
{
if (ModelState.IsValid)
{
// Save to the database
var db = new MvcAuction.Models.AuctionsDataContext();
db.Auctions.Add(auction);
db.SaveChanges();
return RedirectToAction("Index");
}
return Create();
}
Click here to see screenshot of error
这是我的数据库上下文类文件:
namespace MvcAuction.Models
{
public class AuctionsDataContext : System.Data.Entity.DbContext
{
public System.Data.Entity.DbSet<Auction> Auctions { get; set; }
}
}
我认为问题出在我的 web.config 文件中:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-MvcAuction-20121126103244;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-MvcAuction-20121126103244.mdf" providerName="System.Data.SqlClient" />
<add name="MvcAuction.Models.AuctionsDatacontext" connectionString="Data Source=(LocalDb)\v11.0;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\MvcAuction.Models.AuctionsDatacontext.mdf" providerName="System.Data.SqlClient" />
</connectionStrings>
老师用的是Visual Studio 2012,作品不错:D
我不知道为什么这会阻碍我。也不是它到底在问什么,据我所知,从 Visual Studio 2012 到 2013 没有重大变化。
最佳答案
好的,我在这里找到了解决方案:
Entity Framework cannot update database
首先,如果我能发布整个堆栈跟踪,那会更有帮助:
System.TypeInitializationException was unhandled by user code
HResult=-2146233036
Message=The type initializer for 'ExtentPlaceholderCreator' threw an exception.
Source=System.Data.Entity
TypeName=ExtentPlaceholderCreator
StackTrace:
at System.Data.Mapping.Update.Internal.Propagator.ExtentPlaceholderCreator.GetPropagatorResultForPrimitiveType(PrimitiveType primitiveType, PropagatorResult& result)
at System.Data.Mapping.Update.Internal.Propagator.ExtentPlaceholderCreator.Visit(EdmMember node)
at System.Data.Mapping.Update.Internal.Propagator.ExtentPlaceholderCreator.CreateEntitySetPlaceholder(EntitySet entitySet)
at System.Data.Mapping.Update.Internal.Propagator.ExtentPlaceholderCreator.CreatePlaceholder(EntitySetBase extent, UpdateTranslator parent)
at System.Data.Mapping.Update.Internal.Propagator.Visit(DbScanExpression node)
at System.Data.Common.CommandTrees.DbScanExpression.Accept[TResultType](DbExpressionVisitor`1 visitor)
at System.Data.Mapping.Update.Internal.UpdateExpressionVisitor`1.Visit(DbExpression expression)
at System.Data.Mapping.Update.Internal.Propagator.Visit(DbProjectExpression node)
at System.Data.Common.CommandTrees.DbProjectExpression.Accept[TResultType](DbExpressionVisitor`1 visitor)
at System.Data.Mapping.Update.Internal.Propagator.Propagate(UpdateTranslator parent, EntitySet table, DbQueryCommandTree umView)
at System.Data.Mapping.Update.Internal.UpdateTranslator.<ProduceDynamicCommands>d__44.MoveNext()
at System.Linq.Enumerable.<ConcatIterator>d__58`1.MoveNext()
at System.Data.Mapping.Update.Internal.UpdateCommandOrderer..ctor(IEnumerable`1 commands, UpdateTranslator translator)
at System.Data.Mapping.Update.Internal.UpdateTranslator.ProduceCommands()
at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter)
at System.Data.EntityClient.EntityAdapter.Update(IEntityStateManager entityCache)
at System.Data.Objects.ObjectContext.SaveChanges(SaveOptions options)
at System.Data.Entity.Internal.InternalContext.SaveChanges()
at System.Data.Entity.Internal.LazyInternalContext.SaveChanges()
at System.Data.Entity.DbContext.SaveChanges()
at MvcAuction.Controllers.AuctionsController.Create(Auction auction) in c:\Users\ZOHNPJ\Documents\Ex_Files_ASP.NET_MVC4_EssT\Ex_Files_ASP.NET_MVC4_EssT\Exercise Files\06_04\MvcAuction\MvcAuction\Controllers\AuctionsController.cs:line 80
at lambda_method(Closure , ControllerBase , Object[] )
at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.InvokeSynchronousActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass42.<BeginInvokeSynchronousActionMethod>b__41()
at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End()
at System.Web.Mvc.Async.AsyncResultWrapper.End[TResult](IAsyncResult asyncResult, Object tag)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass37.<>c__DisplayClass39.<BeginInvokeActionMethodWithFilters>b__33()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49()
InnerException: System.Reflection.TargetInvocationException
HResult=-2146232828
Message=Exception has been thrown by the target of an invocation.
Source=mscorlib
StackTrace:
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Data.SqlClient.SqlSpatialServices.GeometryFromText(String geometryText)
at System.Data.Spatial.DbGeometry.FromText(String wellKnownText)
at System.Data.Mapping.Update.Internal.Propagator.ExtentPlaceholderCreator.InitializeTypeDefaultMap()
at System.Data.Mapping.Update.Internal.Propagator.ExtentPlaceholderCreator..cctor()
InnerException: System.EntryPointNotFoundException
HResult=-2146233053
Message=Unable to find an entry point named 'SetClrFeatureSwitchMap' in DLL 'SqlServerSpatial110.dll'.
Source=Microsoft.SqlServer.Types
TypeName=""
StackTrace:
at Microsoft.SqlServer.Types.GLNativeMethods.SetClrFeatureSwitchMap(Int32 clrFeatureSwitchMap)
at Microsoft.SqlServer.Types.SqlGeometry.IsValidExpensive()
at Microsoft.SqlServer.Types.SqlGeometry..ctor(GeoData g, Int32 srid)
at Microsoft.SqlServer.Types.SqlGeometry.Construct(GeoData g, Int32 srid)
at Microsoft.SqlServer.Types.SqlGeometry.GeometryFromText(OpenGisType type, SqlChars text, Int32 srid)
at Microsoft.SqlServer.Types.SqlGeometry.STGeomFromText(SqlChars geometryTaggedText, Int32 srid)
at Microsoft.SqlServer.Types.SqlGeometry.Parse(SqlString s)
InnerException:
注意最后一个 InnerException:System.EntryPointNotFoundException。在研究了这个错误(我之前没有看到它)之后,我发现将这段代码添加到我的 web.config 文件中并且它起作用了!
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Microsoft.SqlServer.Types" publicKeyToken="89845dcd8080cc91" />
<bindingRedirect oldVersion="1.0.0.0-11.0.0.0" newVersion="10.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
我相信它与 sql 更新破坏某些东西有关。这篇文章和评论也很有帮助:http://www.networksteve.com/forum/topic.php/Entity_framework_can 't_make_updates_in_db_(missing_entry_point_S/?TopicId=97401&Posts=2
p.s:我是 stackoverflow 的新手,如果应该放弃这个问题和答案以支持我引用的那个很好。
关于c# - TypeInitializationException 发生在 'System.Data.Entity.dll' - 类型初始值设定项 'ExtentPlaceholderCreator',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40092708/