entity-framework

EF4 Mapping one-to-many on existing database without navigation

I'm using ModelBuilder to map an existing database to POCOs. I have courses, students, and meetings. Here are the tables CREATE TABLE Courses ( CourseID int, Name string) CREATE TABLE Students( StudentID int, Name string) CREATE TABLE Courses_Students ( CourseID int, StudentID int) CREATE TABLE Meetings ( ...

Derived Associations in .Net Entity Framework

I want to have a single table that represents a person and have a number of other tables (such as Student/Teacher) use the Person table to store information related to a person. Unfortunately the entity framework doesn't seem to like it when I try to add an association between the Student or Teacher class and I don't understand why. Th...

How do I write a Func<T, bool> to return the entity with the most children?

I have a repository accessing my Entity Framework. I have a method that looks like this: public TEntity FindOne(Expression<Func<TEntity, bool>> criteria) { var query = _queryAll.Where(criteria); return query.FirstOrDefault(); } I have 2 Entities that have a one to many relation. Lets call them Courses and Students. A Course c...

Reading, incrementing and writing back a counter which is unique across multiple users (in EF 4)

I have a database table and a corresponding entity class (POCO with change tracking Proxy) with a member which acts as a counter: class MyEntity { public virtual int ID { get; set; } public virtual int Counter { get; set; } } There is a function in my web application which must fetch this entity by ID, read the Counter (which ...

Using Entity Framework, how do I add a record to the mapping table in a Many to Many relationship

I have the following tables (condensed for readability): Call ID CallerName CallerTime Categories ID CategoryName CallCategories CallID CategoryID When I modeled these in the Entity Framework the mapping talbe "CallCategories" was left out. I am stumped on how to get a record added to this table via the EF. I have gotten this far...

Data Access using ASP.NET, what is the best?

I have been asking my self this question from the time i started workin in the software development field, What is the best way to access data? Which gives the best performance? Which is the best for maintainability? There are lots of solutions to deal with database in the Asp.Net web app, Entity framework 4.0 Classes generator usin...

How can I serialize entities from an ASP.NET MVC site to XML ?

I am working on an ASP.NET MVC (in c#) site which connects to a database and takes advantage of the entities framework. I'd like people to be able to modify the information in the database from the site (since it seems to me to be much easier to display and validate data via an ASP.NET MVC site than a Desktop app while being much easier ...

EF 4.0 Entity does not pick up new values after insert (select entity after insert)

I am using Entity Framework 4.0 POCO entity I have mapped custom stored procedure on insert PROCEDURE [dbo].[usp_MyTable_Insert] ( @Value1 char(1), @Value2 varchar(5), @Value3 varchar(20) .... ) AS BEGIN TRANSACTION INSERT INTO "dbo"."MyTable" ( "Value1", "Value2", "Val...

Entity Framework Insert multiple foreign keys

Patient.DivHospitalID (FK) DivHospital.HospitalID (FK) Hospital.HospitalID (PK) I need to insert into DivHospital the Hospital and link/insert into Patient the DivHospital. Patient tp = new Patient(); DivHospital dh = new DivHospital(); dh.HospitalReference.EntityKey = new EntityKey("transportPagerEntities.Hospital", "hospitalID...

How to get to the primary key of a self tracking entity?

I am trying to create a generic method that will retrieve an item by its id: public T GetByID(int id) { return (T) context.GetObjectByKey( new System.Data.EntityKey(context.DefaultContainerName + "." + context.CreateObjectSet<T>().EntitySet.Name, "ProductID", id)); } Basically I am able to infer ...

Anybody have a clever way to treat a DTO as more of an OO class?

I have a set of DataContracts that are serialzed through WCF. Please note this is a very simplified example. [DataContract] public class MyData { [DataMember] public List<int> MyList { get; set; } } I would like to use object oriented design so that the server and client aren't creating any unnecessary...

TargetInvocationException using .Net 4 dynamic data with Entity Framework 4

I created a simple class library, dropped in an Entity Data Model and dragged in a bunch of tables from my database. In the same solution, I created a Dynamic Data Entities Web Application. Within that project, I added a reference to the project my EDM is in. I imported the namespace in the Global.asax.cs file, and registered my dataC...

How do I disable validation in a Silverlight 4 application?

update How do I disable validation in a Silverlight 4 application? It looks like this is something not unique to RIA Services (as my original question below implies). I observe this when I bind my datagrid to a list of POCOs and I trigger a validation error in my grid (e.g. typing in a non-numeric in a cell bound to a numeric property)...

Max count on group by - Entity Framework

Hi, I have following two tables : 1.) Articles - [ArticleID] 2.) ArticleComments - [CommentID], [ArticleID] I want to retrieve ArticleID with maximum no. of comments e.g. ArticleID - 2 TotalNoOfComments - 15 How do I do it in Entity Framework ? Kindly help :-| I access ArticleComments collection like following : article.ArticleCo...

inserting to a table with an identity column

i am trying to insert a row to a table with an identity column and three "normal" columns using entity framework in wpf. however i got this error message: Cannot insert explicit value for identity column in table 'MyTable' when IDENTITY_INSERT is set to OFF here is my code snippet, trying to add to the user table who has name, surname, a...

Does Entity Framework 4 support Batch Inserts?

I found this similar question here, but this is really old. Was it implemented in the latest version? ...

Entity Framework Concurrency

I am developing application that works with sql server, I read and update data from (by) multiple sources. Because of that, there is a problem that one updates already updated data.... How can I deal with that issue? (I know EF has built in Concurrency mode but if I understand right, if my design creates and disposes contextobject e...

Cast to a property reference to a subclass in Entity framework (TPT)

Hello, I've got the following scheme: (not really the code, just to get the idea) class Person; class Employee : Person; class Company { public Person ContactPerson { ...} public EntityReference<ContactPerson> ContactPersonReference {....} } Employee type got it's own table in the database (Table-per-Type inheritance). Lets...

Entity Framework - how to raise OnChanging for any property?

In a WPF / EF4.0 / MVVM app I have a View that edits a Customer entity. What is the best way to set a property "bool IsCustomerInEditMode" in my CustomerViewModel short of acting upon OnChanging/OnChanged partial methods for every single individual property of the Entity? As far as I know there's no OnEntityChanging method... Thanks! E...

Way to make EF include method generate inner joins?

Using EF4. I have a situation where a linq to EF query with explicit joins won't work due to a many-to-many table. I'm not going to break the database design to suit EF, so I want to use the include method. However, that always seems to generate left outer joins and I need inner joins (simple context.Table1s.Include("Table2") where tab...