entity-framework

Changed property value when selecting in EF4

I need to change the value of a property when I query the database using EF4. I have a company code that gets returned and I need to translate it to another company code, if needed. So, there is a stored procedure that is used to do this currently. Here's the old select statement. SELECT companyName, TranslateCompanyCode(companyCode) as...

Efficient Multiple Deletion in Entity Framework 4

Given a set of entity ids, how can you efficiently delete the entities to which to ids represent, without first selecting the entity? Here is some code, I am using now, but EF profiler is complaining at me for running N+1 queries: var ids = GetSelectedIds(); foreach (var id in ids) db.Workshops.DeleteObject(db.Workshop...

EF4 - Map 1 Table to 2 Objects

In my previous question, I asked how I would design the database schema where 1 table (Comments) would reference 2 tables (Question and Answer). I was given the response to use one table for Question and Answer (SO uses Posts). Now I am in Entity Framework 4. How can I set it up so that this one table (Posts) maps to 2 objects (Ques...

Entities Framework 4 Code First: Inheritance

I am currently trialing EF4 code-first. My POCO classes inherit from an Audit class that contains CreatedBy, CreatedOn, UpdatedBy, UpdatedOn. I was hoping the framework would include the Audit properties in my Action table when creating my database, however this doesn't appear to be the case. Does anyone know how to enable this without o...

Many to many ordered by count of relationship (Entity Framework, Linq)

i have a table structure like this... When I import this into entity framework it looks like this... What I need to do is build a query from LINQ that will return a list of every unique store, populated with a list of people who like that store. (easy, right?) THE CATCH: I need to filter the list to the person's list of friends th...

Defining a 1 to many relationship in Entity Framework

Hi, I'm trying to create a 1:m relationship using Entity Framework (.net 4.0) and am getting the following error: App_Code.Model.msl(36,6) : error 3007: Problem in mapping fragments starting at lines 6, 36:Column(s) [ProductId] are being mapped in both fragments to different conceptual side properties. What i have is a Products table...

How to solve Optimistic Concurrency Updates in c# .NET N-tier applications?

Hy everyone. In c# .net VS 2008 I'm developing an N-tier CRM framework solution and when it's done I want to share it. The architecture is based on: Data Access Layer, Entity Framework, Bussines Logic Layer, WCF and finally the presentation layer (win forms). Somewhere I had read, that more than 2 tier layers are problematic, beacus...

How can I model non-matching Foreign Keys

I'm trying to use EF to model an existing SQL database. The DB is multi-tenant by having a clientID column in every single table (I cannot change this). I have table structure like the following. Table 'ItemID' columns: ClientID (pk) ItemID (pk) ItemName Table 'Items' columns: ClientID (PK) ItemID (PK) [FK to ItemID.ItemID] Versi...

Linq to entities - how to select entities with a where condition on their entitycollection ?

Hi all, I found several times people asking for the same question but it seems that the answer was never satisfying altough it should be pretty easy (in theory). Here is my question : I have an entity called "Company" inside which I have an entityCollection "Employees" (one to many). I need to retrieve all Companies and for each of the...

How do I find out the actual SQL that this statement generates?

I an using VS2010, .NET4 and EF4. I would like to see the actual SQL that is generated when this is run. Also, what is this the best way to write this statement? Here is my code: var cklContactItems = from a in dbTestCenterViews.appvuChecklistExports where a.MarketChecklistID == MCLID ...

Entity Framework, TPT Inheritance and Multi-Language support

Hello I have this DB Structure: Service Id int PK CategoryId FK SomeOtherFields LocalizedService LocalizedServiceId PK LanguageId FK Name Description As you can see, I have a base table called Service, and a LocalizedService table in which I'm writing multilingual data, such as name and description which should be trans...

LINQ to EF - Why is this query pulling back all the data THEN counting?

Using EF 3.5 - why does the first query appear to generate a SELECT COUNT(*)... whilst the second appears to pull back all the data before performing the where? var model = new SageEntities(); Func<nltranm, bool> marked_as_extracted = n => n.history_ref != null; // SELECT COUNT(*) ? var reco...

Code First CTP: Multiple PKs or FKs

Hello, In some instances, we may have PK/FK references duplicated, so entity A and entity B are in a PK/FK relationship, but 3 times. So entity A would have 3 FK collections and entity B would have 3 entity references. How does that work with the code-first template? Do you follow the naming convention of Entity Framework model/datab...

Self-tracking entities original values

Self-tracking entities do not save original properties values. So all properties are marked as modified when you call ApplyChanges. But I need to log the properties which actually changed. I tried to use ApplyOriginalValues, but in this case AcceptChanges throws an exception as finds entities duplicates. Are there any ways to make STE ke...

How to perform a bulk delete with the Entity Framework?

How do I delete multiple entities without a loop? Currently, I have: Dim itemsToDelete As List(Of Item) = (From t In _entities.Item _ Where t.Column = columnValue).ToList For Each item In itemsToDelete _entities.DeleteObject(item) Next _entities.SaveChanges() ...

Is it a bad practice using model classes in controller in mvc?

Hi, I wanted to compare with best practices when working with an ORM or database tables in asp.net mvc. One of the major questions I have is should I instantiate the model classes directly in controller..not query the database but just use the model class to store the values. For e.g. If I am using entity framework as model...then is i...

How to Create a Enitity Framework Project?

OK, so I feel dumb for asking this... but I am stumped. I can't seem to create a project/unit for using the ADO.NET Entity Framework. I looked for a project type that looked like it would match, but no go (closest I could find was ASP.NET Dynamic Data Entities Web Application). I also tried to just add a unit (like you do for Linq-to...

Entity Framework 4.0: .Equals compare to find dups but ignore keys

I'm using Entity Framework 4.0. I want to be able to find duplicate records in an EntitySet. The records will have the same data but different primary keys. When I do a .Equals I get that the records are not equal. I know I can override .Equals but I've got over 20 related entities each with a lot of fields. Is there a compare metho...

Model-First database generator freezes my Visual Studio

I'm new to the Entity Framework, and I know this is not suppose to happen, but maybe it is a known issue in the community so I thought I'd ask. I created my model with 2 entities. When I wanted to create the database tables Visual Studio froze on me. I ran the script myself but then I needed to modify an attribute so it'd take null valu...

What's the best way to create custom sorting (for data like "07/2010") in EF 4.0?

I have some table that contains annual sequence data in column like "1/2010", "2/2010" or "13/2010". I want to order data from this table by this column. So I should create order value for sorting like that the following SQL expression. -- This code should work if amount of data is less than 1000 record per year. CAST(SUBSTRING(ReceiveI...