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
21
votes
5 answers

DDD with EF Code First - how to put them together?

I am learning DDD development for few days, and i start to like it. I (think i) understand the principle of DDD, where your main focus is on business objects, where you have aggregates, aggregates roots, repositories just for aggregates roots and so…
Catalin
  • 11,503
  • 19
  • 74
  • 147
21
votes
3 answers

Unique Constraint with Entity Framework using Code First Migrations

I am doing entity framework (v 5) code first migrations, on an MVC 4 application. I would like to add a unique constraint at the database level. I know this can be done when creating the table, but I already have a…
Daryn
  • 3,394
  • 5
  • 30
  • 41
20
votes
2 answers

Loading Nested Entities / Collections with Entity Framework

I am trying to Eagerly load all the related entities or collection of Entity in one call. My Entities Looks like: Class Person { public virtual long Id { get; set; } public virtual string FirstName { get; set; } public virtual string…
20
votes
8 answers

Can I access the discriminator value in TPH mapping with Entity Framework 4 CTP5

Using Entity Framework 4 CTP5 Code First and this example Is it possible to access the discriminator value? I would like to use it in a projection like context.BillingDetails.Select(x => new { Number = x.Number, DiscrimitatorValue = /* how do I get…
David Glenn
  • 24,412
  • 19
  • 74
  • 94
19
votes
3 answers

Error storing Image in SQL CE 4.0 with ASP.NET MVC 3 and Entity Framework 4.1 Code First

I'm trying to store/save an image in an SQL Compact Edition (CE) database. I declare the field in my Student model as: [Column(TypeName = "image")] public byte[] Photo { get; set; } The database is created with the image data type for the Photo…
19
votes
5 answers

EF Code First - Globally set varchar mapping over nvarchar

I have what should be an easy question but I have been unable to find the answer myself. I am using EF4 CTP-5 Code First Model with hand generated POCOs. It is processing string comparisons in generated SQL as WHERE N'Value' = Object.Property I…
VulgarBinary
  • 3,520
  • 4
  • 20
  • 54
19
votes
4 answers

how to annotate a parent-child relationship with Code-First

When using the CTP 5 of Entity Framework code-first library (as announced here) I'm trying to create a class that maps to a very simple hierarchy table. Here's the SQL that builds the table: CREATE TABLE [dbo].[People] ( Id uniqueidentifier not…
Ralph Shillington
  • 20,718
  • 23
  • 91
  • 154
19
votes
1 answer

Entity Framework Core Code-First: Cascade delete on a many-to-many relationship

I'm working on an ASP.NET MVC 6 project with Entity-Framework Core (version "EntityFramework.Core": "7.0.0-rc1-final") backed by a SQL Server 2012 express DB. I need to model a many-to-many relationship between a Person entity and an Address entity.…
19
votes
2 answers

Entity Framework Code-First Execute Scalar-Valued Functions

How can I execute a scalar function using code first? Below is what I have tried but only the query itself is being returned, not the return value. using (var dbContext = new FTTRContext()) { queryResult = …
user3701347
  • 193
  • 1
  • 1
  • 4
18
votes
4 answers

Entity Framework Database.SetInitializer simply not working

I am having this kind of "mysterious" issue here. I am currently using Entity Framework 4.1 Code First approach with my ASP.NET MVC 3 application, it worked great, until yesterday... Something really bad happened that caused my…
18
votes
2 answers

EF Migrations: RenameColumn in Automatic Migrations?

If you rename a property, the Visual Studio IDE is smart enough to help you refactor to rename that property throughout your code. If that rename is on a model used in EF Migrations, and you run a standard automatic migration like: update-database…
17
votes
1 answer

EF 4.1 Code First - Determine What Properties Have Changed

I'm using Entity Framework 4.1 Code First. Is there a built-in way to get a list of what properties have changed since the entity was loaded from the database? I know code first detects that an object was changed, but is there a way to get exactly…
Dylan Vester
  • 2,686
  • 4
  • 29
  • 40
17
votes
2 answers

Multiple foreign keys pointing to same table in Entity Framework 4.1 code first

I'm stuck at trying to write the Entity Framework 4.1 code first model for the following DB relationship. Here is a visual of the relationship. dbo.[Companies] can have either Seller or Debtor as Company Types. dbo.[SellerDebtors] defines the…
sf.
  • 24,512
  • 13
  • 53
  • 58
17
votes
2 answers

IDbSet and Exposing Include method via Extension Method

I am using the Code-First approach with EF and I wanted to use IDbSet instead of DbSet so I could do Unit Testing with mocks. My problem is I use the Include() method for eager loading where necessary but Include() is not exposed via IDbSet. I saw…
enamrik
  • 2,292
  • 2
  • 27
  • 42
17
votes
2 answers

EF4 code-first: defining object relationships, foreign keys

< RANT_MODE > EF code-first approach is meant to save lots of time but for the time being I've only seen toy examples and spent hours trying to understand how I can make it generate the db I want. But still hoping that Eureka moment :-) < RANT_MODE…
Dav
  • 1,102
  • 3
  • 10
  • 22