Pregunta

I want to separate DB entity layer from DataAccessLayer.

i'll write my steps:

  1. Create empty solution 'DB_sep'
  2. create class library 'DB'
  3. Add ADO.NET Entity Data Model 'DBEntity' of my Database to 'DB' project
  4. Add console application 'App' to 'DB_sep'
  5. Add 'DB' reference to 'App' project

Now I would like to access DB objects from 'App' project , but I can't access dbContext, I'm getting Error

'No connection string named 'DBEntity' could be found in the application config file.'

I've got this lines of code:

try
{
DB.DBEntity db = new DB.DBEntity();
Customer cust = db.Customers.FirstOrDefault(c => c.ID == 2);
Console.WriteLine(cust.Name);
}
catch (Exception ex)
{

Console.WriteLine(ex.Message);
}

Is there a way to keep connection string only in 'DB' or I must put it in 'App' project?

¿Fue útil?

Solución

'No connection string named 'DBEntity' could be found in the application config file.' - seems legit enough.

Add an app.config file (e.g. with the appropriate ConnectionString declaration) to the project that actually executes.

Since targeting EF, pay attention to the specific Entity Framework Configuration Rules - i.e. here it is looking for the name DBEntity. The actual rules vary depending on exactly which version of EF is used. (The chances are pretty good that there is an app.config in the model project which can be used as a reference.)


It is also possible to specify a DbConnection (or perhaps connection string) to the appropriate constructor overload - i.e. via dependency injection - or otherwise specify an alternate database provider.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top