Questions tagged [entity-framework-4.1]

This tag is for the ADO.NET Entity Framework 4.1, the first release of EF to ship separately from the .NET framework. It builds upon the core Entity Framework included in .NET 4.0 and was released to the public in March 2011.

This tag is for the ADO.NET Entity Framework 4.1, the first release of Entity Framework to ship separately from the .NET framework. It builds upon the core Entity Framework included in .NET 4.0 and was released to the public in March 2011.

See for more information.

2904 questions
874
votes
29 answers

Validation failed for one or more entities. See 'EntityValidationErrors' property for more details

I am having this error when seeding my database with code first approach. Validation failed for one or more entities. See 'EntityValidationErrors' property for more details. To be honest I don't know how to check the content of the validation…
Luis Valencia
  • 32,619
  • 93
  • 286
  • 506
654
votes
10 answers

Code-first vs Model/Database-first

What are the pros & cons of using Entity Framework 4.1 Code-first over Model/Database-first with EDMX diagram? I'm trying to fully understand all the approaches to building data access layer using EF 4.1. I'm using Repository pattern and IoC. I know…
369
votes
2 answers

Ignoring a class property in Entity Framework 4.1 Code First

My understanding is that the [NotMapped] attribute is not available until EF 5 which is currently in CTP so we cannot use it in production. How can I mark properties in EF 4.1 to be ignored? UPDATE: I noticed something else strange. I got the…
Raheel Khan
  • 14,205
  • 13
  • 80
  • 168
313
votes
7 answers

Entity Framework Code First - two Foreign Keys from same table

I've just started using EF code first, so I'm a total beginner in this topic. I wanted to create relations between Teams and Matches: 1 match = 2 teams (home, guest) and result. I thought it's easy to create such a model, so I started…
Jarek
  • 5,885
  • 6
  • 41
  • 55
250
votes
2 answers

What effect(s) can the virtual keyword have in Entity Framework 4.1 POCO Code First?

Does the virtual keyword has an effect when used on the properties in EF Code First?. Can someone describe all of its ramifications in different situations? For instance, I know it can control lazy loading -- if you use the virtual keyword on an…
Scott Stafford
  • 43,764
  • 28
  • 129
  • 177
247
votes
14 answers

How to pass parameters to the DbContext.Database.ExecuteSqlCommand method?

Let's just suppose I have a valid need for directly executing a sql command in Entity Framework. I am having trouble figuring out how to use parameters in my sql statement. The following example (not my real example) doesn't work. var firstName =…
jessegavin
  • 74,067
  • 28
  • 136
  • 164
217
votes
21 answers

The relationship could not be changed because one or more of the foreign-key properties is non-nullable

I am getting this error when I GetById() on an entity and then set the collection of child entities to my new list which comes from the MVC view. The operation failed: The relationship could not be changed because one or more of the…
jaffa
  • 26,770
  • 50
  • 178
  • 289
199
votes
8 answers

Getting exact error type in from DbValidationException

I have the situation where I'm initializing my model in DatabaseInitializer() for EF 4.1 and get this annoying error "Validation failed for one or more entities. See 'EntityValidationErrors' property for more details." So, I go to this…
Naz
  • 5,104
  • 8
  • 39
  • 63
192
votes
3 answers

How do I detach objects in Entity Framework Code First?

There is no Detach(object entity) on the DbContext. Do I have the ability to detach objects on EF code first?
Shawn Mclean
  • 56,733
  • 95
  • 279
  • 406
177
votes
9 answers

StringLength vs MaxLength attributes ASP.NET MVC with Entity Framework EF Code First

What is the difference in behavior of [MaxLength] and [StringLength] attributes? As far as I can tell (with the exception that [MaxLength] can validate the maximum length of an array) these are identical and somewhat redundant?
Nick Goloborodko
  • 2,883
  • 3
  • 21
  • 38
176
votes
2 answers

Entity Framework Join 3 Tables

I'm trying to join three tables but I can't understand the method... I completed join 2 tables var entryPoint = dbContext.tbl_EntryPoint .Join(dbContext.tbl_Entry, c => c.EID, cm => cm.EID, …
176
votes
4 answers

How to include a child object's child object in Entity Framework 5

I am using Entity Framework 5 code first and ASP.NET MVC 3. I am struggling to get a child object's child object to populate. Below are my classes.. Application class; public class Application { // Partial list of properties public…
144
votes
3 answers

Generic Repository With EF 4.1 what is the point

As i dig deeper in to the DbContext, DbSet and associated interfaces, I am wondering why you would need to implement a separate "Generic" Repository around these implementations? It looks like DbContext and IDbSet do everything you need and include…
Code Jammr
  • 1,867
  • 3
  • 14
  • 18
139
votes
4 answers

Entity framework code-first null foreign key

I have a User < Country model. A user belongs to a country, but may not belong to any (null foreign key). How do I set this up? When I try to insert a user with a null country, it tells me that it cannot be null. The model is as follows: public…
110
votes
5 answers

in entity framework code first, how to use KeyAttribute on multiple columns

I'm creating a POCO model to use with entity framework code first CTP5. I'm using the decoration to make a property map to a PK column. But how can I define a PK on more then one column, and specifically, how can I control order of the…
1
2 3
99 100