Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create DataContext from Entity Framework connection string?

I'm trying to make this call in my code:

string conn = ConfigurationManager.ConnectionStrings["MyDBEntities"].ConnectionString;
DataContext context = new DataContext(conn);
Table<MyApp.Entities.Employee> myTable = context.GetTable<MyApp.Entities.Employee>();

Here's my connection strings:

<connectionStrings>
  <add name="MyDBEntities" connectionString="metadata=res://*/Entities.MyDB.csdl|res://*/Entities.MyDB.ssdl|res://*/Entities.MyDB.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=STEVEN-PC;Initial Catalog=MyDB;Integrated Security=True;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />
  <add name="MyDB" connectionString="Server=STEVEN-PC;Database=MyDB;Trusted_Connection=yes;" />
</connectionStrings>

I get an error when creating the DataContext: Keyword not supported: 'metadata'.

If I use the second connection string, I get an error when trying to get the table: Server Error in '/' Application. The type 'MyApp.Entities.Employee' is not mapped as a Table.

What am I doing wrong here?

like image 798
Steven Avatar asked May 16 '11 02:05

Steven


2 Answers

You are mixing and matching LINQ-to-SQL and LINQ-to-Entites; the two are not compatible.

When you create entity models with LINQ-to-Entities, it creates an object derived from ObjectContext which would have the IQueryable<T> implementations that you would use for the base of your query. This ObjectContext also will have constructors that take the appropriate metadata to map the entity models to the database; this is why Entity Framework connection strings require Metadata references.

When you try and use LINQ-to-SQL, you can pass it a regular database connection to the DataContext class (or a derived class). The DataContext handles the mapping of your objects to the database differently than the Entity Framework; it relies on attributes on the models to map to tables/columns (using the TableAttribute and ColumnAttribute attributes respectively). These attributes are not present when you created entities using the Entity Framework.

Note: You can use XML mapping files (a different sort than what is used in the Entity Framework) with LINQ-to-SQL, but it's not commonly used.

That said, the easiest approach would be to choose one technology stack (LINQ-to-SQL or LINQ-to-Entities) and stick with that.

like image 198
casperOne Avatar answered Sep 26 '22 06:09

casperOne


If you want to use Entity Framework you should use ObjectContext, not DataContext as this is a base class from Linq-To-Sql.

When you create ADO.NET Entity Data Model , Visual Studio generates (after you complete a generate model from database wizard or use the designer), a class that is derived from ObjectContext that has a default connection string (that you choose at the wizard). Here you can see a nice tutorial from ADO.NET team how to start using the EF.

You are not supposed to use ObjectContext directly, at least not without manually creating the metadata files and pointing to them in your connection string (Never seen DataContext class being used directly, so if I'm wrong someone correct me) , as the wizard I mentioned above creates all sorts of mapping data - to map SQL tables/views/other stuff to Entity classes.

if you want to supply your own connection to the class you can do it programmatically with EntityConnectionStringBuilder.

This is an example how to use EntityConnectionStringBuilder it from MSDN

Edit : I mistakenly wrote about DataContext as if it was EF base class for designer generated code. It is as casperOne stated a base class for Linq-To-Sql classes.

Changed my answer to reflect his comment

like image 24
Michael Avatar answered Sep 24 '22 06:09

Michael