Questions tagged [ef-code-first]

EF Code-First is a way of using Microsoft's Entity Framework with POCO classes, as opposed to model-first or DB-first.

Entity Framework Code-First is a methodology for providing mapping between .NET CLR classes and database structure. Classes and properties may be marked up with attribute decorators (for example [Table("MyTable")] or [Column("CreatedDate")]) or the description for mapping the classes and their properties may be made through the FluentAPI method calls overriding the model creation.

Code-first also operates by convention in that with no markup or FluentAPI coding, it will attempt to connect to a default SQLServer instance to a database named for the project that contains the DBContext class (for example if creating a FoodPantryDAL project where your context will be created, it will attempt to connect to ./SQLExpress/FoodPantryDAL). If the database is not there (and the SQLServer instance is) it will generate the database according to the classes and properties that are currently defined. Tables will be named after the classes they represent, properties also. Properties named ID or [ClassName]ID will be created as a primary key. Classes which reference other classes will be given foreign key relations to those classes, etc.

Code-First may be used not only to create a new database but can all create a mapping to an existing database structure.

8514 questions
103
votes
7 answers

Calculated column in EF Code First

I need to have one column in my database calculated by database as (sum of rows) - (sum of rowsb). I'm using code-first model to create my database. Here is what I mean: public class Income { [Key] public int UserID { get; set; } …
97
votes
4 answers

EF Code First: How to get random rows

How can I build a query where I would retrieve random rows? If I were to write it in SQL then I would put an order by on newid() and chop off n number of rows from the top. Anyway to do this in EF code first? I have tried creating a query that uses…
Mel
  • 3,058
  • 4
  • 26
  • 40
97
votes
1 answer

Multiple DB Contexts in the Same DB and Application in EF 6 and Code First Migrations

I'm new to Entity Framework. I am trying to setup an MVC Application what uses EF 6. I am using Code First Migrations. I am using Areas in the app and would like to have different DbContexts in each area to break it up. I know EF 6 has ContextKey,…
95
votes
8 answers

Default value for Required fields in Entity Framework migrations?

I've added the [Required] data annotation to one of my models in an ASP.NET MVC application. After creating a migration, running the Update-Database command results in the following error: Cannot insert the value NULL into column 'Director', table …
Andriy Drozdyuk
  • 58,435
  • 50
  • 171
  • 272
88
votes
3 answers

How to store images using Entity Framework Code First CTP 5?

I'm just trying to figure out if there is a simple way to store and retrieve binary (file) data using EF Code First CTP 5? I would really like it to use the FILESTREAM type, but I'm really just looking for some way to make it work.
Max Schmeling
  • 12,363
  • 14
  • 66
  • 109
88
votes
4 answers

The connection string 'MyConnection' in the application's configuration file does not contain the required providerName attribute."

I use Entity Framework Code First, My connection string is in a configuration file:
Hodaya Shalom
  • 4,327
  • 12
  • 57
  • 111
87
votes
4 answers

EF Including Other Entities (Generic Repository pattern)

I am using the Generic Repository pattern on top of Entity Framework Code First. Everything was working fine until I needed to include more entities in a query. I got to include one entity successfully, but now I can't figure out how to include…
81
votes
7 answers

One to one optional relationship using Entity Framework Fluent API

We want to use one to one optional relationship using Entity Framework Code First. We have two entities. public class PIIUser { public int Id { get; set; } public int? LoyaltyUserDetailId { get; set; } public LoyaltyUserDetail…
80
votes
6 answers

EF codefirst : Should I initialize navigation properties?

I had seen some books(e.g programming entity framework code first Julia Lerman) define their domain classes (POCO) with no initialization of the navigation properties like: public class User { public int Id { get; set; } public string…
79
votes
14 answers

How can I force entity framework to insert identity columns?

I want to write some C# code to initialize my database with some seed data. Clearly, this is going to require the ability to be able to set the values of various Identity columns when inserting. I'm using a code-first approach. By default,…
Jez
  • 27,951
  • 32
  • 136
  • 233
78
votes
5 answers

How to disable cascade delete for link tables in EF code-first?

I want to disable cascade deletes for a link table with entity framework code-first. For example, if many users have many roles, and I try to delete a role, I want that delete to be blocked unless there are no users currently associated with that…
Jez
  • 27,951
  • 32
  • 136
  • 233
74
votes
6 answers

Entity Framework Stored Procedure Table Value Parameter

I'm trying to call a stored procedure that accepts a table value parameter. I know that this isn't directly supported in Entity Framework yet but from what I understand you can do it using the ExecuteStoreQuery command off of the ObjectContext. I…
74
votes
8 answers

How to create a table corresponding to enum in EF6 Code First?

I've followed MSDN on how to handle enumerations in Code First for EF6. It worked, as supposed to but the field in the created table that refers to the enumerator is a simple int. I'd prefer a second table to be produced, the values of which would…
Konrad Viltersten
  • 36,151
  • 76
  • 250
  • 438
71
votes
6 answers

what is the most reasonable way to find out if entity is attached to dbContext or not?

when i try to attach entity to context i get an exception An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key This is expected behaviour. But i would…
maxlego
  • 4,864
  • 3
  • 31
  • 38
70
votes
8 answers

EF: Validation failing on update when using lazy-loaded, required properties

Given this extremely simple model: public class MyContext : BaseContext { public DbSet Foos { get; set; } public DbSet Bars { get; set; } } public class Foo { public int Id { get; set; } public int Data { get; set; } …
Diego Mijelshon
  • 52,548
  • 16
  • 116
  • 154