0

I have automated test solution, which (lets say) contains 3 projects:

  1. DataAccessProject (which take data from db and thus can be called as producer, and it is my StartUpFile)
  2. PageObjectStorage
  3. Scenario and steps project What i am tring to do is to make 1. project use EF, without notifying any other projects that EF even exists in solution. I want (e.g.) my 3rd project to be able to call something like DataAccessProject.SomeMethod.GiveMeSomeUsers(), so DataAccessProject will take records from entity, do magic and simply return info to compare with values from page object.

What i am struggling with is: Firstly i was getting this error:

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

I have read some article on stack and added connection string node (i would like to avoid it), and now i am getting this error:

Errors: TestModel.ssdl(2,2) : error 0152: No Entity Framework provider found for the ADO.NET provider with invariant name 'System.Data.SqlClient'. Make sure the provider is registered in the 'entityFramework' section of the application config file.

So, my question is, what am i doing wrong? How can i install EF without adding references to EF in every project (i believe it is what EF wants from me now).

Related .config nodes:

<entityFramework>
  <defaultConnectionFactory         type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory,     EntityFramework">
    <parameters>
      <parameter value="mssqllocaldb" />
    </parameters>
  </defaultConnectionFactory>
  <providers>
    <provider invariantName="System.Data.SqlClient"     type="System.Data.Entity.SqlServer.SqlProviderServices,     EntityFramework.SqlServer" />
  </providers>
</entityFramework>
<connectionStrings>
  <add name="test1Entities"     connectionString="metadata=res://*/TestModel.csdl|res://*/TestModel.ssdl|res://*/TestModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=MSSQL-TEST;initial catalog=testCATALOG;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;"         <providerName="System.Data.EntityClient" />
</connectionStrings>

For now i am calling this method from my 3 proejct

public static void TestMyEF()
{
    using (var ctx = new test1Entities())
    {
        var abc = ctx.someTable.FirstOrDefault();
    }
}

Context ctor:

public test1Entities()
: base("name=test1Entities")
{
}

P.s. i was using DB first approach (Create .edxm from exisiting DB), and i am a bit worried, cause my .config node contains <parameter value="mssqllocaldb" />, DB is not local.

Vadim.K
  • 89
  • 12
  • Pass the connection string directly into constructor rather than the name of the configuration entry. That will get rid of your first error. In any projects where you actually use the context - you will need to add references to EF libs. – Vidmantas Blazevicius May 02 '18 at 09:20
  • You can use the predicates(of generic types) to do your work on calling the type of functions you want to perform on your entity-framework along with that you need to expose the models (entity framework) to the calling application. – vikscool May 02 '18 at 09:24
  • @vikscool That is the point, the solution already is kinda big and messy, so i dont want any other project interact with context or entities, i want my DA project to closed to other project as much as possible. So exposing model to other project (note, they will ask for return in native types or custom classes, not entities) seems kinda pointless (i mean EF will be pointless cause it will only create more mess in solution). – Vadim.K May 02 '18 at 09:34
  • @VidmantasBlazevicius What if i am not going to use context directly, e.g. project1 wants project2 to scan context and return some strings, should porject1 have reference to EF? – Vadim.K May 02 '18 at 09:36
  • 1
    @Vadim.K in that case can you use a `common library project` to be used between the two projects, lets just assume a `resultClass` with an object for result and what ever response you want assign that to that result object and then let the other project have access to the common result class only and then access the value from it. – vikscool May 02 '18 at 09:39
  • 1
    If you create your own abstraction layer with methods like `GetSomething()` then you won't need to reference EF libs, but then you need to reference the project where those abstraction layers are. – Vidmantas Blazevicius May 02 '18 at 09:40
  • @vikscool, hi i was trying to pass connection string `metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string="data source=MSSQL-TEST-QA;initial catalog=test1_ei_ei0;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"` and i am getting `Keyword not supported: 'data source'.` error. – Vadim.K May 02 '18 at 10:30
  • 1
    @Vadim.K please refer to this [link](https://stackoverflow.com/a/18241434/2417602) – vikscool May 02 '18 at 11:00
  • @vikscool yeap, i have already fixed this issue (just advice from another post about replacing &quote with quotes was misleading, had to replace the with `\"`), and i even managed to get this working, unfortunately, for this i had to install EF for calling projects, other vice i am getting error - `No Entity Framework provider found for the ADO.NET provider with invariant name` – Vadim.K May 02 '18 at 11:03
  • @vikscool incredibly stupid, but it works https://stackoverflow.com/a/19130718/6634405 – Vadim.K May 02 '18 at 11:21
  • @vikscool can you post your answer as post, so i can mark it question as answered? – Vadim.K May 02 '18 at 11:31

1 Answers1

1

Create a common class library for result for example as:

public class MyResultClass
{
    public readonly object Value;
    public readonly Exception ResultException;
    //add more properties if needed

    public MyResultClass(object value)
    {
        this.ResultException = null;
        this.Value = value;
    }

    public MyResultClass(Exception ex)
    {
        this.ResultException = ex;
    }
}

then use the reference of this class in your projects so you don't have the dependency of the entity-framework and then you can use the json (Serialization and De-Serialization) in order to transformation data from your DAP(DataAccessProject) to client(calling project).

vikscool
  • 1,293
  • 1
  • 10
  • 24