No constructor with a connection string in Entity Framework datacontext

我是研究僧i 提交于 2019-12-04 17:24:47

问题


I am using Entity Framework 5.0 for my project. I looked on the internet and I saw that for the entity framework data context there was another constructor that had a string parameter for the connection string.

On my generated data context I don't have such a constructor. I looked into the base DbContext and it has such a constructor.

Was the code generated wrong? I generated the code from a database. Could this be the cause?

Turns out that I can edit the code generation template file to add the new constructor. Now I have added the new constructor. The file is a MyDataContext.tt file under your edmx model. There you have c# code mixed with template code. You can copy the no argument constructor from there and paste it bellow. Then you can change it and add a string argument to it and pass that argument to the DbContext constructor like this : base(myString).


回答1:


You can add one as needed.

Check the generated file and add an overloaded constructor.

public YourContext(string connectionStr)
        : base(connectionStr)
    {


    }

Probably better to define this in a partial class though, as every generation will require you to manually add it each time.



来源:https://stackoverflow.com/questions/14182091/no-constructor-with-a-connection-string-in-entity-framework-datacontext

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!