entity-framework-4

What is the difference between Lazy Loading and Load()

In Entity Framework 4, what is the difference between Lazy Loading, and using Load() method? Edit: I have added, two 'if' statements: Lazy Loading: var query = from c in context.Contacts select c; foreach ( var contact in query ) { if ( contact.ID == 5 ) Console.WriteLine( contact.Addresses.City ); } Load() method: con...

Creating own POCO classes wont work. ERROR Schema specified is not valid

When I try to create my own POCO classes I get this error. This is only when I got a list of some kind or acsosiation like in this case the Author got Books. But it works great when I use the T4. I kinda like to create my own classes because then I could add my AddBook() to it.. so I highly appreciate if anybody know why.. Schema speci...

Entity Framework - how can I change connection string to be relative?

Hi, Can someone confirm how to change the auto-generated connection string for an entity framework application so that it is relative? That is so it will work for anyone who downloads and installs the application. That is, currently the connection string auto-generated for me has an absolute path in it. See below for an example: <a...

Common Data Properties In ASP.NET MVC 2 Data Objects

Okay, I've been struggling with this for a while now. I have a standard MVC2 project and use EF4 to access data objects. Many of my data objects (and therefore database tables) have two common fields: UpdateUserAccountID and UpdateDate. In each of my controllers, I am currently setting this fields manually within each controller on the...

do I need to dispose datacontext of Linq To Sql

Will there be a problem of having datacontext object in a shared variable for a website (instead of instantiating it again and again). What I mean is, is the db connection opened for ever as long as datacontext is in memory? Will there be a performance hit if I instantiate it everytime I need. How about the same case for context objec...

Composition issues in entity framework 4: The entity is currently read-only

Using WCF RIA Services and entity framework 4. I have 3 DTOs: School, State, District. The state DTO has a District property with composition. And the School DTO has a State property with composition and a District association. The idea, is that when we create/update a school, we also allow the user to enter the state and district (w...

Code First adding to collections? How to use Code First with repositories? Advice me please

EDIT: this happen only on larger scale projects with repositories. Is there anybody using EF4 with code first approach and using repositories? please advice me Hi. Im currently working with EF4 Code First Classes. In my test project I got two classes, Author and Book (author got books). What I'm trying to do is that I HAve a AddBook in ...

Entity Framework - How do I join with an OR condition on several keys

joining tables on two columns is easy from t1 in table1 join t2 in table2 on new { KEY1 = t1.TB1Key1, KEY2 = t1.TB1Key2 } equals new { KEY1 = t2.TB2Key1, KEY2 = t2.TB2Key2 } select new { t1 , t2} but what if i want an OR condition? the SQL will look something like this: select * from table1 t1 inner join table2 t2 on t1.TB1...

Objectcontext and foreignkey-relationships in multi-tier webapp

Hi, I am creating a webapp using entity framework 4. As far as I can tell from extensive googling, it is best practice to create and kill the objectcontext when it is being used, and not let it live too long. So in my datalayer, I am doing something like: using (var context = new MyDAO()) { MY CODE } creating and killing the context ri...

Dynamic LINQ API with .NET Framework 4

Is Dynamic LINQ API (released with the CSharpSamples), still the way to go, or is there a different / alternative approach with .NET 4 (and EF 4)? ...

Entity Framework LINQ projection into custom type results in missing data

I have a many to many relationship between Contractors and SafetyCouncils. They are joined by a bridge table ContractorsSafetyCouncils which consists of ContractorId and SafetyCouncilId. These 2 columns form a composite key. This relationship is mapped correctly in EF4. The Contractor entity has the property: public virtual ICollection<...

Entity Framework 4: Does it make sense to create a single diagram for all entities?

I wrote a few assumptions regarding Entity Framework, then a few questions (so please correct where I am wrong). I am trying to use POCOs with EF 4. My assumptions: Only one data context can exist for an EF diagram. Data Contexts can refer to more than one entity. If you have two data sources, say MS SQL server and Oracle, EF require...

Entity Framework/Linq to SQL: Skip & Take

Just curious as to how Skip & Take are supposed to work. I'm getting the results I want to see on the client side, but when I hook up the AnjLab SQL Profiler and look at the SQL that is being executed it looks as though it is querying for and returning the entire set of rows to the client. Is it really returning all the rows then sortin...

Make sure Entity framework always reads from database?

Hi! I have this applikation that is actually two applications, a webapplication and a console application. The console application is used as a scheduled task on the windows machine and is executed 3 times a day to to some recurring work. Both application uses the same Model and repository that is placed in a seperate projekt (class lib...

Is there a way, using LINQ/EF, to get the top most item in a parent/child hierarchy?

I have a class called Structure: public class Structure { public int StructureId { get; set; } public Structure Parent { get; set; } } As you can see, Structure has a parent Structure. There can be an indefinite number of structures within this hierarchy. Is there any way, using LINQ (with Entity Framework), to get the top-mo...

Ways to Maintain Data History in SQL Server 2008 Database

For a long time, we've wanted to create a case management system where no history is ever lost. When a change is made, we want to record that change, but have the ability to go back to any point in time and see what the record looked like. I wanted to pose this question to the Stack Overflow community to see what are some ways of doing t...

EntityCollection Clear() and Remove() methods

What is the right way to delete all of the collection items of an EF entity? In the code below, DocumentItems is the collection of related document items for a document. This code proceedes on Clear() but fails on SaveChanges() because related items are connected to their document via FK and FK is mandatory. So I guess they somehow remai...

Query Entity Framework object and child object with single query

I have two Entity Framework objects with a one-to-many relationship: widget(parent) and widgetNail(child) Widget has a text column: Title WidgetNail has a text column: Description I would like to build a query that will return a list of Widgets that match one of two criteria: A text string is found in the Widget title, or The same t...

Entity Framework Code First Doesn't Generate Database

I created a db Context class and added a connection string in my web.config file as instructed in Scott Guthrie's Code First Development with Entity Framework 4. I am running it from a test method. I received several database errors running the tests, but when I finally cleaned up the classes so the test succeeded, I still had no datab...

Advice on POCO Validation with ASP.NET MVC/Entity Framework

Hi Guys, Here's the scenario: ASP.NET MVC2 Web Application Entity Framework 4 (Pure POCO's, Custom Data Context) Repository Pattern Unit of Work Pattern Dependency Injection Service Layer mediating Controller -> Repository So basically, all the cool stuff. :) Flow of events for a basic UI operation ("Adding a Post"): Controller c...