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
45
votes
2 answers

Code First Migration with Connection Strings

So I've managed to get Code First running and it works great. Since I am still developing the application the structure of the Database isn't finalize and so I need to implement migrations. I followed the Official Blog Post and got that…
Dragonseer
  • 2,874
  • 7
  • 29
  • 42
45
votes
3 answers

Entity Framework auto incrementing field, that isn't the Id

I know this isn't the most ideal solution, but I need to add an auto incrementing field to one of my EF Code First objects. This column id NOT the Id, which is a guid. Is there anyway for me to define the auto incrementing field in code, or would…
JamesStuddart
  • 2,581
  • 3
  • 27
  • 45
43
votes
3 answers

How do I map TimeSpan with greater than 24 hours to SQL server Code First?

I am trying to map a TimeSpan Code First property to SQL server. Code First seems to be creating it as a Time(7) in SQL. However TimeSpan in .Net can handle longer periods than 24 hours and I need to store longer than 24 hour for event length. What…
GraemeMiller
  • 11,973
  • 8
  • 57
  • 111
42
votes
10 answers

EF 4.1 Code First error - The entity type SomeType is not part of the model for the current context

While working with EF code first I get error given below at different times: The entity type SomeType is not part of the model for the current context. What are the possible causes of this error?
rovsen
  • 4,932
  • 5
  • 38
  • 60
42
votes
10 answers

How to configure ProviderManifestToken for EF Code First

I have a asp.net MVC3 project using EF code-first. For my unit testing I have been using SQL Server CE 4.0 and SQL Server 2008 Express. Both have worked perfectly with EF generating my database as expected. However, when I run my application outside…
trevorc
  • 3,023
  • 4
  • 31
  • 49
41
votes
4 answers

Entity Framework loading child collection with sort order

I have two tables a parent and a child table. The child table has a column sortorder (a numeric value). Because of the missing support of the EF to persist a IList inclusive the sort order without exposing the sortorder (see: Entity Framework…
X181
  • 753
  • 1
  • 5
  • 12
40
votes
1 answer

SQL 'time' type in Entity Framework Code First

I'm trying to create a 'time(7)' column in a table with Entity Framework Code First. This is my Entity: public class ShiftDetail { public long Id { get; set; } [Required] public int DayOfWeek { get; set; } [Required] …
39
votes
3 answers

How to disable migration in Entity Framework 4.3.1?

Is there any way to disable migration in Entity Framework 4.3.1? I removed the migrations folder from the project and the generated tables in my database, but it doesn't work! How can you remove the migration?
amiry jd
  • 27,021
  • 30
  • 116
  • 215
39
votes
2 answers

using Guid as PK with EF4 Code First

I have this class and table: public class Foo { public Guid Id {get;set;} public string Name {get;set;} } create table Foo ( id uniqueidentifier primary KEY DEFAULT (newsequentialid()), name nvarchar(255) ) the problem is that when i try to…
Omu
  • 69,856
  • 92
  • 277
  • 407
39
votes
1 answer

SQL Server Express connection string for Entity Framework Code First

I am working in Visual Web Developer 2010 Express, and using the Entity Framework code-first CTP. I am able to do this with the new SQL Server CE but I am unable to find a connection string to work with SQL Server Express. This one, using the SQL…
35
votes
3 answers

Using Entity Framework Code First CTP5, how do I create a primary key column that are INTs and are not identity columns

Using Entity Framework Code First CTP5, how do I create a primary key column that are INTs and are not identity columns Preferably not using attributes.
ajma
  • 12,106
  • 12
  • 71
  • 90
35
votes
2 answers

How do I use Entity Framework in Code First Drop-Create mode?

I'm using Entity Framework v4. I have followed the instructions in the Nerd Dinner tutorial. I'm currently in development mode (not released to any higher environments) and would like for tables to be recreated on each new deployment, since the…
Ryan Langton
  • 6,294
  • 15
  • 53
  • 103
34
votes
4 answers

Tool to convert Entity Framework EDMX to Code First

Is there a tool to convert an edmx into code-first? I know there was talk of one appearing in a CTP a while back, but I can't find any updates relating to this. There's a guy on the MSDN forums who has written his own (not available yet), but…
SturmUndDrang
  • 1,876
  • 5
  • 27
  • 47
33
votes
7 answers

Adding CreatedDate to an entity using Entity Framework 5 Code First

I am trying to add a CreatedDate property to entities in my Model and am using EF5 Code First. I want this date to not be changed once set, I want it to be a UTC date. I do NOT want to use a constructor, as I have many entities in my model that I…
33
votes
6 answers

Specify ON DELETE NO ACTION in ASP.NET MVC 4 C# Code First

How do I specify ON DELETE NO ACTION Foreign Key Constraint in my model designs? At present, I have: public class Status { [Required] public int StatusId { get; set; } [Required] [DisplayName("Status")] public string Name { get;…
Gravy
  • 12,264
  • 26
  • 124
  • 193