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="Data Source=STEVEN-PC;Initial Catalog=MyDB;Integrated Security=True;MultipleActiveResultSets=True"" 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?
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.
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
Just in case, this is what I did. I don't have connections in the Web.config since I need a DropDownList to select the connection.
string connDev = @"metadata=res://*/MyModel.csdl|res://*/MyModel.ssdl|res://*/MyModel.msl;provider=System.Data.SqlClient;provider connection string=""Server=MyDevServer;Database=MyDB;Integrated Security=True""";
EntityConnection ec = new EntityConnection(connDev);
MyDBContext db = new MyDBContext(ec);
var people = db.People.ToList();
The connectionstring is already defined in the EntityModel, so you can try to use the default connection as follows:
using (context = new DataContext())
{
var myTable = context.GetTable<MyApp.Entities.Employee>();
}
You can also try the following:
MyApp.Entities.Employee myTable = context.GetTable<MyApp.Entities.Employee>();
EDIT: GetTable<T>() will return type T, so the syntax above will be correct.
If you want to override the connection string, use the second connection string from your web.config file (MyDB)
精彩评论