Questions tagged [ef-code-first]

EF Code-First is a way of using Microsoft's Entity Framework with POCO classes, as opposed to model-first or DB-first.

Entity Framework Code-First is a methodology for providing mapping between .NET CLR classes and database structure. Classes and properties may be marked up with attribute decorators (for example [Table("MyTable")] or [Column("CreatedDate")]) or the description for mapping the classes and their properties may be made through the FluentAPI method calls overriding the model creation.

Code-first also operates by convention in that with no markup or FluentAPI coding, it will attempt to connect to a default SQLServer instance to a database named for the project that contains the DBContext class (for example if creating a FoodPantryDAL project where your context will be created, it will attempt to connect to ./SQLExpress/FoodPantryDAL). If the database is not there (and the SQLServer instance is) it will generate the database according to the classes and properties that are currently defined. Tables will be named after the classes they represent, properties also. Properties named ID or [ClassName]ID will be created as a primary key. Classes which reference other classes will be given foreign key relations to those classes, etc.

Code-First may be used not only to create a new database but can all create a mapping to an existing database structure.

8514 questions
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…
410
votes
19 answers

Introducing FOREIGN KEY constraint may cause cycles or multiple cascade paths - why?

I've been wrestling with this for a while and can't quite figure out what's happening. I have a Card entity which contains Sides (usually 2) - and both Cards and Sides have a Stage. I'm using EF Codefirst migrations and the migrations are failing…
SB2055
  • 12,272
  • 32
  • 97
  • 202
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
327
votes
7 answers

Create code first, many to many, with additional fields in association table

I have this scenario: public class Member { public int MemberID { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public virtual ICollection Comments { get; set; } } public class…
hgdean
  • 3,273
  • 3
  • 16
  • 5
312
votes
12 answers

Unique Key constraints for multiple columns in Entity Framework

I'm using Entity Framework 5.0 Code First; public class Entity { [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)] public string EntityId { get; set;} public int FirstColumn { get; set;} public int SecondColumn { get; set;} …
286
votes
19 answers

Decimal precision and scale in EF Code First

I'm experimenting with this code-first approach, but I'm find out now that a property of type System.Decimal gets mapped to a sql column of type decimal(18, 0). How do I set the precision of the database column?
Dave Van den Eynde
  • 17,020
  • 7
  • 59
  • 90
270
votes
11 answers

Entity Framework: One Database, Multiple DbContexts. Is this a bad idea?

My impression to date has been that a DbContext is meant to represent your database, and thus, if your application uses one database, you'd want only one DbContext. However, some colleagues want to break functional areas out into separate DbContext…
264
votes
18 answers

Entity Framework 6 Code first Default value

is there "elegant" way to give specific property a default value ? Maybe by DataAnnotations, something like : [DefaultValue("true")] public bool Active { get; set; } Thank you.
marino-krk
  • 2,651
  • 3
  • 13
  • 5
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
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
175
votes
24 answers

The entity type is not part of the model for the current context

I am getting into the Entity Framework, but I am unsure if I am missing a critical point in the code-first approach. I am using a generic repository pattern based on the code from https://genericunitofworkandrepositories.codeplex.com/ and have…
janhartmann
  • 14,713
  • 15
  • 82
  • 138
172
votes
8 answers

EF Code First "Invalid column name 'Discriminator'" but no inheritance

I have a table in my database called SEntries (see below the CREATE TABLE statement). It has a primary key, a couple of foreign keys and nothing special about it. I have many tables in my database similar to that one, but for some reason, this table…
Marcelo Calbucci
  • 5,845
  • 3
  • 18
  • 22
166
votes
3 answers

ICollection Vs List in Entity Framework

I only watched a few webcasts before I went head first in to designing a few Entity Framework applications. I really didn't read that much documentation and I feel like I am suffering for it now. I have been using List in my classes, and it has…
wil
  • 2,019
  • 2
  • 15
  • 14
1
2 3
99 100