2

Running Visual Studio 2013 ASP.NET MVC 4 Entity Framework (EF) 5

After running my project, when navigating to the 'create' page. I try to post information to local mysql database, this exception is thrown...

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.

at this location in my AuctionController.cs file

[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

This is my Database Context class file:

 namespace MvcAuction.Models
{
    public class AuctionsDataContext : System.Data.Entity.DbContext
    {
        public System.Data.Entity.DbSet<Auction> Auctions { get; set; }
    }
}

I think the issue is going wrong in my web.config file:

<?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>

I am following this tutorial: https://www.lynda.com/ASP-NET-tutorials/Persisting-data-Entity-Framework-code-first/109762/120303-4.html?autoplay=true

The teacher is using Visual Studio 2012, his works fine:D

I do not know why this is holding me up. Nor what exactly it is asking, to my knowledge there were no breaking changes from Visual Studio 2012 to 2013.

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
Slug
  • 319
  • 1
  • 2
  • 12
  • So where is the code for `ExtentPlaceholderCreator`? – Erik Philips Oct 17 '16 at 17:59
  • Possible duplicate of [An unhandled exception of type 'System.TypeInitializationException' occurred in EntityFramework.dll](http://stackoverflow.com/questions/17132698/an-unhandled-exception-of-type-system-typeinitializationexception-occurred-in) – Mostafiz Oct 17 '16 at 18:02
  • @Mostafiz - I have tried those solutions, I only have one configSections and it is directly underneath the configuration tag, also, my MVC and ET versions match what my web.config file specifies. – Slug Oct 17 '16 at 18:36
  • @ErikPhilips - I don't know what ExtentPlaceholderCreator is referring to, I did not create that code. Am I missing something? – Slug Oct 17 '16 at 18:40
  • Thank you both for your quick responses! – Slug Oct 17 '16 at 21:11

1 Answers1

17

Ok I found the solution here:

Entity Framework cannot update database

First of all, if I would have post the entire stack trace it would have been more helpful:

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: 

Notice the last InnerException: System.EntryPointNotFoundException. After researching this error (I didn't see it earlier) I found this code to add to my web.config file and it worked!

 <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>

I believe it has something to do with an sql update breaking something. This article & comments were also helpful: 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: I am very new to stackoverflow, if this question and answer should be discarded in favor of the one I referenced that's fine.

Community
  • 1
  • 1
Slug
  • 319
  • 1
  • 2
  • 12
  • Thanks a lot!! that fixed my issue. but i haven't changed anything with my sql server or iis and it was working fine all the time for may be 3 years now. Why does this issue suddenly pop out? any reasons ? – Shankar Jan 17 '18 at 15:22
  • Thank you so much. It easily saves a few hours time. This happened to me after I moved an MVC app from one hosting service to another despite the SQL server and the code remain exactly the same. I guess it has something to do with the .Net environment. – Hong Jun 01 '18 at 21:40
  • Thank you! Spot on for my issue as well! – Silmar Dec 12 '18 at 21:06
  • Thank You Thank You! Worked for me as well! – wakgtech Nov 19 '19 at 22:19