objectcontext

How to bind EF Code First DbContext to an Asp.Net DataSource?

纵然是瞬间 提交于 2019-11-29 09:36:49
问题 I've created the following Context to be used with Entity Framework Code First : public class Context : DbContext { public DbSet<Animal> Animals { get; set; } } Now I would like to use this Context in an Asp.Net application to perform CRUD operations using a GridView . I need to create a DataSource to do the data binding. How would I go about? The ASP part would look like this: <asp:GridView runat="server" DataSourceID="animalDataSource" DataKeyNames="AnimalID" AutoGenerateColumns="false">

How do I stop Entity Framework from generating BOTH ObjectContext and dbContext

限于喜欢 提交于 2019-11-29 07:26:12
问题 Background Visual Studio 2012 NEW Model.EDMX file created in VS2012 Copied some of the EDMX xml from a previously created EDMX into the new one Problem / Question Now. The EDMX (TT transform, custom tool, whatever, etc.) is generating BOTH DbContext classes (under the Model.tt/Model.Context.tt files) and ObjectContext classes (via the Model.designer.cs file). See image below: Everything builds fine and works with the DbContext (but obviously only if I delete the Designer.cs file just before

Is it always better to use 'DbContext' instead of 'ObjectContext'?

不打扰是莪最后的温柔 提交于 2019-11-29 06:23:30
I just downloaded EntityFramework.dll v4.3 . I've found a number of questions that compare DbContext vs. ObjectContext . But most of these are from 2010, or early 2011. I'd like to read more on the subject. Specifically, are there any books on DbContext I can get my hands on? I also want to know, as of today, what are the limitations of DbContext when comparing it to its older brother the ObjectContext ? I realize that DbContext is more compact in that it exposes fewer properties. This suggests to me that I should migrate from ObjectContext . But, if I do this migration, will I give up any

IQueryable Repository with StructureMap (IoC) - How do i Implement IDisposable?

一笑奈何 提交于 2019-11-29 04:48:44
If i have the following Repository: public IQueryable<User> Users() { var db = new SqlDataContext(); return db.Users; } I understand that the connection is opened only when the query is fired: public class ServiceLayer { public IRepository repo; public ServiceLayer(IRepository injectedRepo) { this.repo = injectedRepo; } public List<User> GetUsers() { return repo.Users().ToList(); // connection opened, query fired, connection closed. (or is it??) } } If this is the case, do i still need to make my Repository implement IDisposable? The Visual Studio Code Metrics certainly think i should. I'm

GetTable equivalent for ObjectContext

点点圈 提交于 2019-11-28 11:58:57
问题 I was previously using a DataContext which had a GetTable(type) method to get tables generically. Example: context.GetTable(myObject.GetType()); Recently my team decided to switch to using ObjectContext with the Entity Framework. Is there a way to get tables by the entity name similar to DataContexts GetTable method without having to specify a specific type? It has to be generic. 回答1: There is indeed a very simple way to do this, like so: public IQueryable GetTable<T>(T entity) where T :

Provider connection string from Entity Framework

痴心易碎 提交于 2019-11-28 09:03:51
If you are using object contex data model (with EDMX file), during its creation you might want to specify the connection string inside your config file. The connection string is unfortunately not the common connection string as it contains some ...things needed for the entity connections. Example with MySql connection: <add name="MyDbEntities" connectionString="metadata=res://*/Namespace.MyDb.csdl|res://*/Namespace.MyDb.ssdl|res://*/Namespace.MyDb.msl;provider=MySql.Data.MySqlClient;provider connection string="server=172.17.17.154;User Id=user;password=password;Persist Security Info=True

C#/EF and the Repository Pattern: Where to put the ObjectContext in a solution with multiple repositories?

有些话、适合烂在心里 提交于 2019-11-28 07:51:28
I have multiple repositories in my application. Where should I put the ObjectContext? Right now, I have a reference like ObjectContext ctx; in every repository. What is the smartest and safest way to go about this? A design with multiple ObjectContext instances is only acceptable if your Repository methods commit the transaction. Otherwise, it is possible that external calls to commit the transaction may not persist everything you intend, because you will hold references to different instances of the ObjectContext . If you want to restrict the ObjectContext to a single instance, then you can

ASP.Net Entity Framework, objectcontext error

て烟熏妆下的殇ゞ 提交于 2019-11-28 07:13:38
问题 I'm building a 4 layered ASP.Net web application. The layers are: Data Layer Entity Layer Business Layer UI Layer The entity layer has my data model classes and is built from my entity data model (edmx file) in the datalayer using T4 templates (POCO). The entity layer is referenced in all other layers. My data layer has a class called SourceKeyRepository which has a function like so: public IEnumerable<SourceKey> Get(SourceKey sk) { using (dmc = new DataModelContainer()) { var query = from

Entity Framework Object Context per request in ASP.NET? [closed]

拥有回忆 提交于 2019-11-28 02:04:27
问题 Is it considered a good practice to use a single ObjectContext per request? I read these objects should be short lived and are not extremely costly to instantiate but does this make the case appealing for one of them per request? If yes, are there any patterns that properly implement this? 回答1: Yes it is an accepted approach to have ObjectContext/DbContext with lifetimes per HttpRequest. Here's a sample I have provided in another answer. Hoewever, it's better to leave these lifetime

Is it always better to use 'DbContext' instead of 'ObjectContext'?

时光怂恿深爱的人放手 提交于 2019-11-27 23:52:04
问题 I just downloaded EntityFramework.dll v4.3 . I've found a number of questions that compare DbContext vs. ObjectContext . But most of these are from 2010, or early 2011. I'd like to read more on the subject. Specifically, are there any books on DbContext I can get my hands on? I also want to know, as of today, what are the limitations of DbContext when comparing it to its older brother the ObjectContext ? I realize that DbContext is more compact in that it exposes fewer properties. This