Questions tagged [code-first]

Code-first is a software implementation approach that favors programming against an API over other approaches that may rely more heavily on visual tools or require the presence of some external source that is inspected to generate program behavior, structure, or data.

Code-First is a software implementation approach that favors programming (typically imperative, though not necessarily) against an API over other approaches, which may include

  • Model-First. An approach where a model is created using some sort of editor or designer (typically through a highly visual, interactive user interface). Examples include a forms editor for creating user interfaces, a UML modeling tool for generating code or database schemas, or a wizard to generate XML configuration information.
  • External Source -First. A general approach that requires some source of information used to generate program behavior or structure. Either an interactive or automated tool may be used. Examples of external sources include XML schemas, and configuration files. Database-First could be considered a specialization in which a database is used to generate program entities.

Code-first emphasizes

  • Solving a particular set of problems using code before (or at least mostly instead of) using other approaches
  • Minimal (relative to other approaches) tooling support
  • Language, framework and API rather than tools and techniques for working with them

Code-first may imply the use of "convention over configuration" and the API may be of the form of a "fluent interface". Code-first and other approaches may be used together if supported by the provider, vendor or open source project.

2395 questions
84
votes
5 answers

How to remove child one to many related records in EF code first database?

Well, I have one-to-many related model: public class Parent { public int Id { get; set; } public string Name { get; set; } public ICollection Children { get; set; } } public class Child { public int Id { get; set; } …
Dmytro
  • 16,668
  • 27
  • 80
  • 130
83
votes
5 answers

Entity Framework - Is there a way to automatically eager-load child entities without Include()?

Is there a way to decorate your POCO classes to automatically eager-load child entities without having to use Include() every time you load them? Say I have a Class Car, with Complex-typed Properties for Wheels, Doors, Engine, Bumper, Windows,…
66
votes
4 answers

Entity Framework Code First - No Detach() method on DbContext

I'm wondering why there is no Detach method on the DbContext object like there is for ObjectContext.  I can only assume this omission was intentional, but I have a hard time figuring out why.  I need to be able to detach and re-attach entities (for…
Brian Sullivan
  • 27,513
  • 23
  • 77
  • 91
63
votes
3 answers

How to ignore a property when using Entity Framework Code First

Entity Framework Code First will auto-create a table in the database base based on the Model. Is there an attribute that will avoid this?
Dozer
  • 5,025
  • 11
  • 36
  • 52
62
votes
12 answers

How can I get my database to seed using Entity Framework CodeFirst?

The database is created successfully (as are the tables) but is not seeded. I have spent several hours and read tons of articles but have not been able to get it. Any suggestions? On a side note, is it possible to call the initializer without having…
Alec
  • 1,646
  • 3
  • 19
  • 35
61
votes
3 answers

C# Code-First migration, up/down?

Started to use the add-migration command in the package manager console to generate the migrations for my model. My question is, the up and down method. I assume that the purpose of the down method is to remove all dependencies and drop the tables…
Patrick
  • 5,442
  • 9
  • 53
  • 104
60
votes
3 answers

Why is my DbContext DbSet null?

I created a new Entity Frameworks Code First app and the DbSet (People) is returning null. public class Person { public int Id { get; set; } public string Name { get; set; } } public class Repository : DbContext { public DbSet
Jamey McElveen
  • 18,135
  • 25
  • 89
  • 129
53
votes
4 answers

ASP.NET MVC3 and Entity Framework Code first architecture

My previous question made me think again about layers, repository, dependency injection and architectural stuff like this. My architecture now looks like this: I am using EF code first, so I just made POCO classes, and context. That creates db and…
Damb
  • 14,410
  • 6
  • 47
  • 49
52
votes
6 answers

Find a specified generic DbSet in a DbContext dynamically when I have an entity

I have following classes and DbContext: public class Order : BaseEntity { public Number {get; set;} } public class Product : BaseEntity; { public Name {get; set;} } public class Context : DbContext { .... public DbSet Orders…
Masoud
  • 8,020
  • 12
  • 62
  • 123
51
votes
6 answers

Database in use error with Entity Framework 4 Code First

I have an MVC3 and EF 4 Code First application, which is configured to change the DB when the model changes, by setting the DB Initializer to a DropCreateDatabaseIfModelChanges, where TocratesDb is my derived DbContext. I have now made a…
ProfK
  • 49,207
  • 121
  • 399
  • 775
51
votes
7 answers

How do I remove underscore of foreign key fields in code first by convention

I've got multiple classes (including TPT) in my project. Each POCO has a BaseClass, which has a GUID (called GlobalKey) as primary key. First I used DataAnnotations to create correct foreign keys. But then I've got problems synchronizing the…
50
votes
7 answers

Can you create sql views / stored procedure using Entity Framework 4.1 Code first approach

Entity Framework 4.1 Code First works great creating tables and relationships. Is it possible to create sql views or stored procedure using Code first approach? Any pointers regarding this will be highly appreciated. Thanks a lot!
47
votes
9 answers

How to run Seed() method of Configuration class of migrations

I have 2 questions: 1) How can I run Seed() method from the package-manager console without updating-database model? 2) Is there a way how to call Seed() method in the code? Thx for any advice.
Maris
  • 4,608
  • 6
  • 39
  • 68
46
votes
5 answers

Self-referencing many-to-many recursive relationship code first Entity Framework

I can't seem to make this work at all class Member { public virtual IList Friends { get; set; } [Key] public int MemberId { get; set; } public string Name{ get; set; } } I tried adding Mappings but in vain. Is there a way to…
Korayem
  • 12,108
  • 5
  • 69
  • 56
46
votes
3 answers

Fluent API, many-to-many in Entity Framework Core

I've searched stackoverflow for a proper solution on generating a many-to-many relationship, using EF Core, Code first and Fluent API. A simple scenario would be: public class Person { public Person() { Clubs = new HashSet(); …
Anonymous
  • 1,303
  • 4
  • 19
  • 31