How can l use Entity Framework without App.config

白昼怎懂夜的黑 提交于 2019-11-29 15:10:00
marc_s

You're not mentioning what approach you're using (database-first, model-first, code-first) - but basically, in the end, you need to define a string variable and assign it a valid EF connection string

string myConnectionString = "...(define a valid EF connection string here)......";

Example for database-first approach:

string myConnectionString = @"metadata=.\Model1.csdl|.\Model1.ssdl|.\Model1.msl;provider=System.Data.SqlClient;provider connection string="";data source=.;initial catalog=test;integrated security=True;multipleactiveresultsets=True;App=EntityFramework""";

and then use that to create your ObjectContext (database- and model-first) or DbContext (code-first)

using(ObjectContext ctx = new ObjectContext(myConnectionString))
{
    // do your EF magic here.....
}

But quite honestly - I think this is a really bad idea since this makes it impossible for you to move your application to another machine - no one else can install and run this, since the connection string is hard-coded into your C# code..... the whole point of having config files is so that you can change / adapt things like connection strings so that they are not tied to a single machine/location but can be adapted to the particular needs of a given user / customer....

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