Slu*_*lug 1 c# asp.net-mvc entity-framework
运行Visual Studio 2013 ASP.NET MVC 4实体框架(EF)5
运行我的项目后,导航到"创建"页面.我尝试将信息发布到本地mysql数据库,抛出此异常...
System.Data.Entity.dll中出现"System.TypeInitializationException"类型的异常,但未在用户代码中处理
附加信息:'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();
}
Run Code Online (Sandbox Code Playgroud)
这是我的数据库上下文类文件:
namespace MvcAuction.Models
{
public class AuctionsDataContext : System.Data.Entity.DbContext
{
public System.Data.Entity.DbSet<Auction> Auctions { get; set; }
}
}
Run Code Online (Sandbox Code Playgroud)
我认为我的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>
Run Code Online (Sandbox Code Playgroud)
我正在关注本教程:https://www.lynda.com/ASP-NET-tutorials/Persisting-data-Entity-Framework-code-first/109762/120303-4.html?autoplay=true
老师正在使用Visual Studio 2012,他的作品很好:D
我不知道为什么这会让我失望.根据我的知识,从Visual Studio 2012到2013还没有任何重大变化.
Slu*_*lug 11
好的我在这里找到了解决方案:
首先,如果我要发布整个堆栈跟踪,那会更有帮助:
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:
Run Code Online (Sandbox Code Playgroud)
注意最后一次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>
Run Code Online (Sandbox Code Playgroud)
我相信它与sql更新有所不同.这篇文章和评论也很有帮助:http://www.networksteve.com/forum/topic.php/Entity_framework_can't_make_updates_in_db_(missing_entry_point_S /?TopicId = 97401&Posts = 2
ps:我是stackoverflow的新手,如果这个问题和答案应该被丢弃,而不是我引用的那个问题.