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
438
votes
10 answers

Why use ICollection and not IEnumerable or List on many-many/one-many relationships?

I see this a lot in tutorials, with navigation properties as ICollection. Is this a mandatory requirement for Entity Framework? Can I use IEnumerable? What's the main purpose of using ICollection instead of IEnumerable or even List?
Jan Carlo Viray
  • 11,856
  • 11
  • 42
  • 63
339
votes
17 answers

Validation failed for one or more entities while saving changes to SQL Server Database using Entity Framework

I want to save my Edit to Database and I am using Entity FrameWork Code-First in ASP.NET MVC 3 / C# but I am getting errors. In my Event class, I have DateTime and TimeSpan datatypes but in my database, I've got Date and time respectively. Could…
user522767
  • 4,013
  • 6
  • 21
  • 17
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
265
votes
27 answers

The model backing the context has changed since the database was created

The error message : "The model backing the 'AddressBook' context has changed since the database was created. Either manually delete/update the database, or call Database.SetInitializer with an IDatabaseInitializer instance. For example, the…
Ashish Gupta
  • 14,869
  • 20
  • 75
  • 134
138
votes
19 answers

Entity Framework DateTime and UTC

Is it possible to have Entity Framework (I am using the Code First Approach with CTP5 currently) store all DateTime values as UTC in the database? Or is there maybe a way to specify it in the mapping, for example in this one for the last_login…
Fionn
  • 10,975
  • 11
  • 54
  • 84
131
votes
19 answers

Unique Constraint in Entity Framework Code First

Question Is it possible to define a unique constraint on a property using either the fluent syntax or an attribute? If not, what are the workarounds? I have a user class with a primary key, but I would like to make sure the email address is also…
kim3er
  • 6,306
  • 4
  • 41
  • 69
111
votes
5 answers

Does Entity Framework Code First support stored procedures?

I've watched several presentations of EF Code First and haven't seen how EFCF works with stored procedures. How can I declare a method that will use some sp? Can I pass an entity to a method that calls sp without manually mapping entity properties…
frennky
  • 12,581
  • 10
  • 47
  • 63
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…
109
votes
11 answers

ALTER TABLE DROP COLUMN failed because one or more objects access this column

I am trying to do this: ALTER TABLE CompanyTransactions DROP COLUMN Created But I get this: Msg 5074, Level 16, State 1, Line 2 The object 'DF__CompanyTr__Creat__0CDAE408' is dependent on column 'Created'. Msg 4922, Level 16, State 9, Line 2 …
Casey Crookston
  • 13,016
  • 24
  • 107
  • 193
108
votes
3 answers

How to specify table name with Entity Framework Code First Fluent API

I have an Entity and I am to configure Entity Framework to map it to a database table with different name. I can easily do this with Code First DataAnnotations (DataAnnotations.Schema.TableAttribute). But due to limitations now I have to use Code…
bairog
  • 3,143
  • 6
  • 36
  • 54
102
votes
4 answers

how to use views in code first entity framework

How can I use the database view in entity framework code first,
Sagar
  • 1,727
  • 4
  • 22
  • 30
101
votes
4 answers

Nullable property to entity field, Entity Framework through Code First

Using the data annotation Required like so: [Required] public int somefield {get; set;} Will set somefield to Not Null in database, How can I set somefield to allow NULLs?, I tried setting it through SQL Server Management Studio but Entity…
chrisramon
  • 1,253
  • 2
  • 8
  • 8
95
votes
5 answers

How can I disable code first migrations

I have a code-first entity model in EF5. But I want to manage the database changes manually -- I do not want EF to modify my existing database and all its data. But when I make parallel changes in the EF mapping and in the database, EF refuses to…
Stan Hargrove
  • 951
  • 1
  • 6
  • 3
94
votes
9 answers

Entity Framework Code First Date field creation

I am using Entity Framework Code First method to create my database table. The following code creates a DATETIME column in the database, but I want to create a DATE column. [DataType(DataType.Date)] [DisplayFormatAttribute(ApplyFormatInEditMode…
sfgroups
  • 18,151
  • 28
  • 132
  • 204
86
votes
29 answers

CREATE DATABASE permission denied in database 'master' (EF code-first)

I use code-first in my project and deploy on host but I get error CREATE DATABASE permission denied in database 'master'. This is my connection string:
Mohammad
  • 1,214
  • 2
  • 9
  • 17
1
2 3
99 100