运行Visual Studio 2013 ASP.NET MVC 4 实体框架(EF)5
运行我的项目后,导航到“创建”页面。我尝试将信息发布到本地mysql数据库,这个异常被抛出......
发生了'System.TypeInitializationException'类型的异常 System.Data.Entity.dll但未在用户代码中处理
附加信息:类型初始化程序 'ExtentPlaceholderCreator'抛出异常。
在我的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年没有重大变化。
答案 0 :(得分:10)
好的,我在这里找到了解决方案:
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更新有所不同。这篇文章&amp;评论也很有帮助:http://www.networksteve.com/forum/topic.php/Entity_framework_can't_make_updates_in_db_(missing_entry_point_S /?TopicId = 97401&amp; Posts = 2
p.s:我对stackoverflow很新,如果这个问题和答案应该被抛弃,而不是我引用的那个问题。