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

Mapping composite keys using EF code first

Sql server table: SomeId PK varchar(50) not null OtherId PK int not null How should I map this in EF 6 code first? public class MyTable { [Key] public string SomeId { get; set; } [Key] public int OtherId { get; set; } } I've seen…
loyalflow
  • 14,275
  • 27
  • 107
  • 168
156
votes
7 answers

Debug code-first Entity Framework migration codes

I'm using Entity Framework code first in my website and I'm just wondering if there is any way to debug the migration codes. You know, like setting breakpoints and stuff like this. I'm using Package Manager Console to update the database using…
143
votes
31 answers

There is already an object named in the database

Update-Database failed from Package Manager Console. I've used Entity Framework 6.x and code-first approach. Error is "There is already an object named 'AboutUs' in the database." How can I solve this problem? internal sealed class Configuration …
141
votes
15 answers

How to fix the datetime2 out-of-range conversion error using DbContext and SetInitializer?

I'm using the DbContext and Code First APIs introduced with Entity Framework 4.1. The data model uses basic data types such as string and DateTime. The only data annotation I'm using in some cases is [Required], but that's not on any of the DateTime…
Alex Angas
  • 59,219
  • 41
  • 137
  • 210
140
votes
28 answers

The term 'Update-Database' is not recognized as the name of a cmdlet

I am using EF5 beta1 and while I was able to run the "Update-Database" before. Now that I shut down Visual Studio, I cannot get it to run. I get the following error: The term 'Update-Database' is not recognized as the name of a cmdlet, function,…
139
votes
4 answers

Entity framework code-first null foreign key

I have a User < Country model. A user belongs to a country, but may not belong to any (null foreign key). How do I set this up? When I try to insert a user with a null country, it tells me that it cannot be null. The model is as follows: public…
137
votes
6 answers

EF Code First foreign key without navigation property

Let's say I have the following entities: public class Parent { public int Id { get; set; } } public class Child { public int Id { get; set; } public int ParentId { get; set; } } What is the code first fluent API syntax to enforce that…
RationalGeek
  • 9,425
  • 11
  • 62
  • 90
130
votes
11 answers

How to create index in Entity Framework 6.2 with fluent configuration

Is there a way to create an index on a property/column using fluent configuration, instead of using the new IndexAttribute ?
Valo
  • 1,872
  • 2
  • 15
  • 23
120
votes
13 answers

How to delete and recreate from scratch an existing EF Code First database

I am using EF Code First with EF 5 in VS 2012. I use PM update-database command and I have a simple seed method to fill some tables with sample data. I would like to delete and recreate my x.mdb. The update history seems to be out of sync. If I…
g.pickardou
  • 32,346
  • 36
  • 123
  • 268
115
votes
10 answers

Entity Framework query slow, but same SQL in SqlQuery is fast

I'm seeing some really strange perf related to a very simple query using Entity Framework Code-First with .NET framework version 4. The LINQ2Entities query looks like this: context.MyTables.Where(m => m.SomeStringProp == stringVar); This takes…
Brian Sullivan
  • 27,513
  • 23
  • 77
  • 91
114
votes
6 answers

EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType

I am working with Entity Framework Code First and MVC 5. When I created my application with Individual User Accounts Authentication I was given an Account controller and along with it all the required classes and code that is needed to get the Indiv…
J86
  • 14,345
  • 47
  • 130
  • 228
110
votes
3 answers

composite key as foreign key

I am using Entity framework 4.1 in MVC 3 application. I have an entity where I have primary key consists of two columns ( composite key). And this is being used in another entity as foreign key. How to create the relationship ? In normal scnerios we…
DotnetSparrow
  • 27,428
  • 62
  • 183
  • 316
108
votes
2 answers

How Should I Declare Foreign Key Relationships Using Code First Entity Framework (4.1) in MVC3?

I have been searching for resources on how to declare foreign key relationships and other constraints using code first EF 4.1 without much luck. Basically I am building the data model in code and using MVC3 to query that model. Everything works via…
Guido Anselmi
  • 3,872
  • 8
  • 35
  • 58
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
105
votes
2 answers

Composite Key with EF 4.1 Code First

I am trying to figure out how to have a composite key using EF code First 4.1 RC. Currently, I am using the [Key] Data Annotation, but I am unable to specify more than one key. how would one specify a composite key? Here is my Example: public class…
bugnuker
  • 3,918
  • 7
  • 24
  • 31