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
32
votes
3 answers

Understanding ForeignKey attribute in entity framework code first

See the following post for some background: Entity framework one to zero or one relationship without navigation property I had always thought that ForeignKey was used to show which property in a class held the ForeignKey that determined the…
32
votes
4 answers

Code First Migrations and Stored Procedures

I have just created a database and done my first migration (just a simple table add). Now I want to add some stored procedures which I have just added by writing the sql and executing it in Management Studio. But I would like to include these…
user517406
  • 13,623
  • 29
  • 80
  • 120
30
votes
2 answers

EF Code First Parent-Child insertions with identity columns

i have the following model. class Parent { int ParentId (identity column) { get; set; } string ParentName { get; set; } virtual ICollection Children { get; set; } } class Child { int ChildId (identity column) { get; set; } …
user396491
  • 941
  • 2
  • 12
  • 12
29
votes
3 answers

Using MySql with Entity Framework 4 and the Code-First Development CTP

I thought I'd experiment a bit with Scott Guthrie's latest post on code-first dev with Entity Framework 4. Instead of using Sql Server, I'm trying to use MySql. Here are the relevant parts of my web.config (this is an Asp.Net MVC 2…
Dusda
  • 3,347
  • 5
  • 37
  • 58
28
votes
2 answers

EF Code First Readonly column

I am using EF Code first with database first approach. "with Database.SetInitializer(null);" My table has two columns createddate and amendddate. They are managed by SQL Server using triggers. The idea is that when data entry happens then these…
activebiz
  • 6,000
  • 9
  • 41
  • 64
28
votes
7 answers

EF4 Code-First causes InvalidOperationException

I'm having an issue when trying to run my project each time it builds. It seems the initializer runs, but when it comes to the first query - it dies with the following InvalidOperationException. This operation requires a connection to the 'master'…
Amadiere
  • 11,313
  • 6
  • 41
  • 47
27
votes
6 answers

Entity Framework CTP 4 - Code First Custom Database Initializer

I would like to implement a custom database initialization strategy so that I can generate the database schema and apply it to an EXISTING EMPTY SQL database using a supplied User ID and Password. Unfortunately the built-in strategies don’t provide…
Mark
  • 6,051
  • 2
  • 26
  • 25
26
votes
9 answers

Oracle.ManagedDataAccess.EntityFramework - ORA-01918: user 'dbo' does not exist

I am trying to implemente code First Migrations with Oracle.ManagedDataAccess 6.121.1.0 provider, but with no success at all. As I am receiving a ORA-code, I am assuming that the connection are been opened successfully. But the Migrations are…
25
votes
2 answers

EF Update-Database Error: Value cannot be null Parameter name: type

I try to execute command update-database in PMC and always get this error msg. I know theres another article basically has the same error but i tried every answer and nothing works. System.ArgumentNullException: Value cannot be null. Parameter name:…
somniloquist_
  • 275
  • 3
  • 6
24
votes
1 answer

Entity Framework - The foreign key component … is not a declared property on type

I have the following Model public class FilanthropyEvent : EntityBase, IDeleteable { public int Id { get; set; } public string Name { get; set; } public DateTime EventDate { get; set; } public string Description { get; set; } …
MrBliz
  • 5,830
  • 15
  • 57
  • 81
22
votes
13 answers

Column names in each table must be unique. Column name 'StripeRecipientId' in table 'dbo.Foos' is specified more than once

I have a model class named Foo that has, among others, these properties. public string StripeRecipientId { get; set; } public override bool HasProvidedBillingInformation { get { // return…
Water Cooler v2
  • 32,724
  • 54
  • 166
  • 336
21
votes
3 answers

Code First vs. Database First

I created an Entity Framework model based on an existing database, then generated the POCO entities from the model. The connection string in my web.config isn't Entity Framework, it's just the standard connection string (it's missing the CSDL, SSDL,…
grefly
  • 1,181
  • 2
  • 12
  • 28
21
votes
5 answers

EF code first: How to delete a row from an entity's Collection while following DDD?

So here's the scenario: DDD states that you use a repository to get the aggregate root, then use that to add/remove to any collections it has. Adding is simple, you simple call .Add(Item item) on the Collection you wish to add to. A new row is added…
edgarian
  • 871
  • 7
  • 18
21
votes
8 answers

Returning a DataTable using Entity Framework ExecuteStoreQuery

I am working with a system that has many stored procedures that need to be displayed. Creating entities for each of my objects is not practical. Is it possible and how would I return a DataTable using ExecuteStoreQuery ? public…
detroitpro
  • 3,853
  • 4
  • 35
  • 64
21
votes
3 answers

Force EF 4.1 Code First to See an Attached entity as Modified

All the examples I've found refer to a class called ObjectContext, which doesn't appear to exist in CTP5. I must stress at this point, CTP5 is my first exposure to the Entity Framework. I have a disconnected POCO that I have attached to my…
kim3er
  • 6,306
  • 4
  • 41
  • 69