Problem with connectionstring and entityframework

▼魔方 西西 提交于 2019-12-10 18:31:03

问题


I have a database (sql 2008 mdf file), a class library project with an edmx file, created with the wizard. So the connection string is also made by the wizard. This project is on a teamfoundation server.

I can use all the wizard made objects when coding.

But when i run the program and I try to make an entityContainerName, the program crashes and gives this error:

The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid.

on this line: public TestEntities() : base("name=TestEntities", "TestEntities")

How can I solve this problem or what am I doing wrong?


回答1:


You need to copy the connection string from the class library's config file into the config file of the application that is actually running.




回答2:


First: delete your model, second: delete all the connectionstrings in your app.config.

Then recreate the model. This should set everything back to default.




回答3:


Okay if you want to use EntityFramework in a Class Library that means that your application should not be aware of the DB configurations. This is my point of view. How i solved this issue. We remove all connection string from all App.Config files. And we use instantiate our context with the connection already created.

public class ExpertDataBase : DbContext
{
    public DbSet<Operator> Operators { get; set; }

    public ExpertDataBase()
        : base(ConnectionFactory.GetCESqlConnection(),true)
    {

    }
}

public static class ConnectionFactory
{
   public static DbConnection GetCESqlConnection()
   {
       DbConnection connection = new SqlCeConnection()
           {
               ConnectionString = @"Data   Source= ",
           };
       return connection;
   }
}

This is my implementation and it works without me entering any connection string in any xml. This connectionFactory can be refactored so i can be injected somehow and use UnityContainer to resolve the connection type. Hope this helps



来源:https://stackoverflow.com/questions/2904672/problem-with-connectionstring-and-entityframework

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