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
70
votes
12 answers

EF CodeFirst: Either the parameter @objname is ambiguous or the claimed @objtype (COLUMN) is wrong

I've a table named EducationTypes and an Entity named EducationType, I renamed one of entity properties, now I'm frequently getting Either the parameter @objname is ambiguous or the claimed @objtype (COLUMN) is wrong. How can I solve this issue? The…
69
votes
5 answers

Using Entity Framework (code first) migrations in production

I'm just looking into using EF migrations for our project, and in particular for performing schema changes in production between releases. I have seen mentioned that there is an API to perform these migrations at run-time using the DbMigration…
68
votes
5 answers

Should I enable or disable dynamic proxies with entity framework 4.1 and MVC3?

Could someone offer up some advice or point out some blogs/articles that could help with making this decision? The proxies seem very foreign to me and I'm hesitant to use them. I like the ability to control Lazy Loading by using virtual properties…
67
votes
12 answers

How to Seed Users and Roles with Code First Migration using Identity ASP.NET Core

I have created a new clean asp.net 5 project (rc1-final). Using Identity Authentication I just have the ApplicationDbContext.cs with the following code: public class ApplicationDbContext : IdentityDbContext { protected override…
Sauron
  • 2,156
  • 5
  • 17
  • 20
66
votes
15 answers

Entity Framework Code First Fluent Api: Adding Indexes to columns

I'm running EF 4.2 CF and want to create indexes on certain columns in my POCO objects. As an example lets say we have this employee class: public class Employee { public int EmployeeID { get; set; } public string EmployeeCode { get; set; } …
66
votes
13 answers

EF Core Error - No project was found. Change the current working directory or use the --project option

I am using Visual Studio 2015 and dotnet core and trying to develop an EF Core Code First project using Sqlite and this documentation / tutorial, which also uses Sqlite => NET Core - New Database When I try to add an initial migration from the…
65
votes
3 answers

How do I eagerly Include the child and grandchild elements of an entity in Entity Framework Code First?

Imagine three entities (Customer, Book, Author) related like this: A Customer has many Books A Book has one Author I use that data to print a report like this: Customer: Peter Book: To Kill a Mockingbird - Author: Harper Lee Book: A Tale of Two…
adolfojp
  • 2,961
  • 4
  • 24
  • 21
64
votes
2 answers

Mocking or faking DbEntityEntry or creating a new DbEntityEntry

Following on the heels of my other question about mocking DbContext.Set I've got another question about mocking EF Code First. I now have a method for my update that looks like: if (entity == null) throw new…
taylonr
  • 10,732
  • 5
  • 37
  • 66
63
votes
5 answers

Unique key with EF code first

I have a following model in my project public class Category { public Guid ID { get; set; } [Required(ErrorMessage = "Title cannot be empty")] public string Title { get; set; } } and I'm trying to make Title as unique key, I googled…
Prashant Cholachagudda
  • 13,012
  • 23
  • 97
  • 162
62
votes
2 answers

What is the difference between DbSet<> and virtual DbSet<>?

In Entity Framework Code First, when I declare entities I have to use DbSet<> type of properties for that. For example: public DbSet Products { get; set; } public DbSet Customers { get; set; } Recently I've met DbSet<> declared…
magos
  • 3,371
  • 4
  • 29
  • 54
62
votes
4 answers

How to sync model after using Code First from Database using Entity Framework 6.1 and MVC 5?

Assumptions Using EF 6.1, MVC 5, VS 2013, C# I have an existing database model designed in Toad DM for SQL Server and it's very important keep it always updated Steps and Notes Using ADO.NET Entity Data Model I chose Code First from Database (new…
62
votes
3 answers

Entity framework, problems updating related objects

I am currently working on a project using the latest version of Entity Framework and I have come across an issue which I can not seem to solve. When it comes to updating existing objects, I can fairly easily update the object properties ok, until…
Thewads
  • 4,953
  • 11
  • 55
  • 71
61
votes
8 answers

How to specify database name in Code First?

How do I tell EF what to name the database and where to put it? If there is no connection string in the Web.Config, it tries to put it in the local SQLEXPRESS Server, but I want to put it out on a known SQL Server and name it what I want. Any…
Sam
  • 15,336
  • 25
  • 85
  • 148
61
votes
1 answer

Many-to-many mapping table

From examples that I have seen online and in a Programming Entity Framework CodeFirst book, when you have a collection on both classes EF would create a mapping table such as MembersRecipes and the primary key from each class would link to this…
Pricey
  • 5,799
  • 12
  • 60
  • 84
60
votes
3 answers

Why is my DbContext DbSet null?

I created a new Entity Frameworks Code First app and the DbSet (People) is returning null. public class Person { public int Id { get; set; } public string Name { get; set; } } public class Repository : DbContext { public DbSet
Jamey McElveen
  • 18,135
  • 25
  • 89
  • 129