none
Entity Framework Code First approach is causing issue to enable migrations RRS feed

  • Question

  • Hi,

    We are developing a Custom SharePoint REST service hosted under ISAPI folder which is using EF6.1.3 to interact with database.

    We are supporting this application inheriting from another vendor who doesn`t work for our client anymore who has done the initial setup of the EF Migrations

    There is no migrations folder at the code level and the migrations history table is also truncated at db level.

    When I want to add a new model class and try to enable-migrations its giving me an error message as "The context type <<Assembly Name>> was not found in the assembly".

    When I did some research about this issue ,some people has asked to remove the signing of the assembly.Since sharepoint projets always signs the assembly with key.snk which is autogenerated during the intial creation of the project.I went to the project properties ,signing tab at the left and unticked the signing of the assembly.

    Then I ran the Enable-Migrations command which thrown a new error message as The target context "AssemblyName.Dbcontext" is not constructible.Add a default constructor or provide an implementation of IDBContextFactory.

    I dont want to add the default constructor to my AppDbContext class inheriting from DbContext class.How can I mitigate this issue and let the code run without any issues for the EF Migration on code first way.

    Thanks in advance

    Tuesday, October 17, 2017 11:40 PM

All replies

  • Hi,

    >>I dont want to add the default constructor to my AppDbContext class inheriting from DbContext class.

    Try to create default  constructor inheriting from IDbContextFactory.

    public class MigrationsContextFactory : IDbContextFactory<MyContext>
    {
        public MyContext Create()
        {
            return new MyDBContext("connectionStringName");
        }
    }

    Following similar questions you can take a look.

    https://stackoverflow.com/questions/11395283/how-to-implement-idbcontextfactory-for-use-with-entity-framework-data-migrations

    https://stackoverflow.com/questions/15504465/entityframework-code-first-custom-connection-string-and-migrations

    Yours sincerely,

    bob


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, October 18, 2017 11:00 AM
  • Hi Bob

    I have done the same approach for now creatinga  Dbcontext Factory with create() as above

    I have added a new constructor in my AppDBContext which takes connection string as parameter.

    This is working as long as I dont sign the assembly and its getting failed when I enable the signing

    Is there any way we can make this work even after enabling the signing of the assembly?


    • Edited by Venu545 Wednesday, October 18, 2017 11:07 PM
    Wednesday, October 18, 2017 10:43 PM
  • Hi Venu545,

    >>Is there any way we can make this work even after enabling the signing of the assembly?

    As far as I understand, No, you can not alter an assembly which is signed.

    Code signing is guarantee that the code has not been altered or corrupted since it was signed.

    Best Regards,

    Bob


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, October 19, 2017 10:52 AM
  • Hi Bob,

    Thanks for the reply,However for sharepoint projects the dll is always signed.

    Are you saying its not going to work with SharePoint projects?If we want to make it work,what is the ideal way to proceed.

    Since even if I keep in the seperate class library of the same solution ,I need to sign the assembly in order to reference in my custom SharePoint REST service to include in the wsp package.

    Sunday, October 22, 2017 11:29 PM
  • There are some tricky ways to resign dlls.

    First way, ILDASM disassembles dlls to IL files and IL can reassemble IL files with SN key to new dlls. 

    The second recommended way is Mono Cecil. 

    Here is someone's code.

    https://github.com/martinkirsche/StrongNameResigner

    Monday, October 23, 2017 10:58 AM