Is it possible to generate a DataContext from an Entity Framework connection string?

asp.net c# entity-framework

Question

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?

1
6
5/16/2011 2:15:47 AM

Accepted Answer

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

5
5/16/2011 6:37:57 AM

Popular Answer

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.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow