locked
Problem to throw exception RRS feed

Answers

  • Hi,

    when you want it to point to some other database, then you should change the bold part of the connection string:

    connectionString="metadata=res://*/EntityData.SalesModel.csdl|res://*/EntityData.SalesModel.ssdl|res://*/EntityData.SalesModel.msl;provider=System.Data.SqlClient;provider connection string="data source=.\;initial catalog=SalesSystem;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework""

    That is the connection string to the database that is used. (You see the connection string=$quot; in front of it. The " is encoded as " So it is something like a "connection string inside a connection string".

    With kind regards,

    Konrad

    Friday, April 22, 2016 10:12 AM

All replies

  • Hi HuaMin Chen,

    it is preferred to post the problem details inside the forum. Links to external resources are seen as risk by some people.

    The UnintentionalCodeFirstException can be a sign that you used the wrong connection string. You need to use the connection string that starts with "metadata=".

    (So the Description of UnintentionalCodeFirstException is talking about the wrong context: "Thrown when a context is generated from the DbContext templates in Database First or Model First mode and is then used in Code First mode.")

    With kind regards,

    Konrad

    Thursday, April 21, 2016 7:53 AM
  • Okay from the link I see it is caused by Entity Framework. What is the code that throw this Exception?

    At the same time I believe there is Inner Exception that you can check too.

    chanmm


    chanmm

    Thursday, April 21, 2016 8:33 AM
  • Thanks all.

    What should I further show, to the current error I've got? It is already one "throw ..." line, to which I am not sure how I can further catch any Stacktrace of innerexception, to such line.


    Many Thanks & Best Regards, Hua Min

    Friday, April 22, 2016 3:52 AM
  • Hi,

    I can give you a few hints regarding exception inspection in this post. But please be aware that there is no (and cannot be) an inner exception in your case! I will also give you reasons for that later.

    a) When you debug your application, you could tell the debugger when it should stop. It is called Exception Settings and in Visual Studio you can open that window with Ctrl + Alt + E.

    The Common Language Runtime Exceptions can be checked completly. Then the debugger will stop whenever an Exception in managed code is thrown.

    That way you will see all  thrown exception and you can go through any exception handling with the debugger.

    ==> Maybe that are already your settings, because the debugger stopped where the exception was thrown! So there might be nothing to change at all!

    b) Looking at the exception details. When you have the situation as on the picture:
    You see the popup has some Actions at the bottom. One is "View Detail...". When you click on that, the popup will be gone and will be replaced with some other window. You will be in the known debugger. You can see the exception in the new popup but you can also simply close it and then use he debugger to inspect all variables (and there will be a local variable $exception - maybe the name is a little bit different - didn't check it now.)

    You could also try the Action "Copy exception detail to clipboard" but I never used that in the past so I don't know what information will be included.

    I hope I am not mistaken right now. I miss the debugger windows in the background but that might be because the picture is just showing a small part of your Visual Studio. But it might also be that your situation is different to the situation I know so that this hint does not work.

    c) Why is the no inner exception?
    When you check the code, then you see the throw line. So we see that the created exception is a simple UnintentionalCodeFirstException which was created by the parameterless constructor. So it has no message and no inner exception. (Exceptions are nothing magic. It is as with all objects: If you want to have data in it, then you have to put it in first! So e.g. when you have one exception and you find out, that the exception cannot be handled, then you can create a new exception e.g.
    throw new Exception("Some message", ex); // the exception ex will be the inner exception.

    So my guess is still:
    You want to use code first. So it is important that you have all the meta data for the database ready. But instead of this metadata you directly connect to a database. At least that was a problem that I had some time ago (But I have to confess that I completely stopped using the entity framework in the past and I just did a few tests with the new version .... So I wouldn't see myself as an expert regarding entity framework.

    With kind regards,

    Konrad

    Friday, April 22, 2016 4:45 AM
  • Here is the details of the Exception

    System.Data.Entity.Infrastructure.UnintentionalCodeFirstException was unhandled by user code
      HResult=-2146233079
      Message=The context is being used in Code First mode with code that was generated from an EDMX file for either Database First or Model First development.  This will not work correctly. To fix this problem do not remove the line of code that throws this exception. If you wish to use Database First or Model First, then make sure that the Entity Framework connection string is included in the app.config or web.config of the start-up project. If you are creating your own DbConnection, then make sure that it is an EntityConnection and not some other type of DbConnection, and that you pass it to one of the base DbContext constructors that take a DbConnection. To learn more about Code First, Database First, and Model First see the Entity Framework documentation here: http://go.microsoft.com/fwlink/?LinkId=394715
      Source=wpf_EntityFramework
      StackTrace:
           at wpf_EntityFramework.EntityData.SalesContext.OnModelCreating(DbModelBuilder modelBuilder) in C:\dp12\wpf_EntityFramework 160414\wpf_EntityFramework\EntityData\SalesModel.Context.cs:line 46
           at System.Data.Entity.Internal.LazyInternalContext.CreateModelBuilder()
           at System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)
           at System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input)
      InnerException: 
    Where to identify the reason of the problem?



    Many Thanks & Best Regards, Hua Min


    Friday, April 22, 2016 6:22 AM
  • Hi HuaMin,

    so what are you trying to do when you get that exception? How did you use the Entity Framework? Code first? Database first?
    What are your connection strings that you are using or do you have your own code to establish the connection?

    It might be as simple as I first thought - a wrong connection string. But of course that is just one possible cause of multiple options.

    With kind regards,

    Konrad

    Friday, April 22, 2016 6:34 AM
  • How to identify the real reason of the problem?

    Many Thanks & Best Regards, Hua Min

    Friday, April 22, 2016 7:40 AM
  • You should know, what you are trying to do. And you got a long message that should help you:

    The context is being used in Code First mode with code that was generated from an EDMX file for either Database First or Model First development.  This will not work correctly. To fix this problem do not remove the line of code that throws this exception. If you wish to use Database First or Model First, then make sure that the Entity Framework connection string is included in the app.config or web.config of the start-up project. If you are creating your own DbConnection, then make sure that it is an EntityConnection and not some other type of DbConnection, and that you pass it to one of the base DbContext constructors that take a DbConnection. To learn more about Code First, Database First, and Model First see the Entity Framework documentation here: http://go.microsoft.com/fwlink/?LinkId=394715

    You could try to lead us a little bit when we try to help by answering our questions, too.

    With kind regards,

    Konrad

    Friday, April 22, 2016 7:59 AM
  • I'm verifying against the download from this, and I've already created the relevant schema.

    Only one change is that I put this connection

    <add name="SalesContext" connectionString="Data Source=192.168.168.50\SS2012EXPR;Initial Catalog=SalesSystem;Integrated Security=False;User ID=ws_login2;Password=????" providerName="System.Data.SqlClient"/>
    instead. How to identify the reason of this?


    Many Thanks & Best Regards, Hua Min

    Friday, April 22, 2016 8:38 AM
  • Hi HuaMin,

    that leads me to my first response: When you have CodeFirst you cannot use a Data Source= connection string. Instead you need the connection string including the meta information.

    At http://stackoverflow.com/questions/16406991/entity-framework-unintentionalcodefirstexception you can find even some code that creates the correct connection string of the normal one.

    With kind regards,

    Konrad

    Friday, April 22, 2016 8:48 AM
  • Thanks.
    To the original connection
    <add name="SalesContext" connectionString="metadata=res://*/EntityData.SalesModel.csdl|res://*/EntityData.SalesModel.ssdl|res://*/EntityData.SalesModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.\;initial catalog=SalesSystem;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />



    of the relevant example, I do not know how I can adjust the connection to ensure it is pointing to the schema I've created locally.

    Many Thanks & Best Regards, Hua Min


    Friday, April 22, 2016 8:53 AM
  • Hi,

    when you work with code first, then you have to use this complex connection string. 

    What do you try to change? Just point to some other database or server? Then you see the data source inside this connection string?

    And the link to the code to create the required connection string from a normal database connection string could also be useful.

    With kind regards,

    Konrad

    Friday, April 22, 2016 9:28 AM
  • Yes, I create SalesSystem schema on the local database. What to adjust to the original connection, as I also encounter other issue, by using the original connection?

    Many Thanks & Best Regards, Hua Min


    Friday, April 22, 2016 9:32 AM
  • Hi,

    when you want it to point to some other database, then you should change the bold part of the connection string:

    connectionString="metadata=res://*/EntityData.SalesModel.csdl|res://*/EntityData.SalesModel.ssdl|res://*/EntityData.SalesModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.\;initial catalog=SalesSystem;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;"

    That is the connection string to the database that is used. (You see the connection string=$quot; in front of it. The " is encoded as &quot; So it is something like a "connection string inside a connection string".

    With kind regards,

    Konrad

    Friday, April 22, 2016 10:12 AM