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

How to create a view using EF code-first POCO

That simple. I need to create a View using Code First. I found nothing about this on google nor SO. Is there a way to accomplish this? I need that view to be created and queried using linq, so it's not a solution to create it using an script on…
Chuck Norris
  • 963
  • 1
  • 9
  • 17
51
votes
5 answers

Set decimal(16, 3) for a column in Code First Approach in EF4.3

How can I do this : private decimal _SnachCount; [Required] [DataType("decimal(16 ,3)")] public decimal SnachCount { get { return _SnachCount; } set { _SnachCount = value; } } private decimal _MinimumStock; [Required] [DataType("decimal(16…
Ali Foroughi
  • 4,540
  • 7
  • 42
  • 66
51
votes
6 answers

One DbContext per request in ASP.NET MVC (without IOC container)

Apologies if this has already been answered, but how do you guarantee one Entity Framework DbContext per request if you are not using an IOC container? (The answers I've come across so far deal with IOC container solutions.) It seems like most…
51
votes
6 answers

Database in use error with Entity Framework 4 Code First

I have an MVC3 and EF 4 Code First application, which is configured to change the DB when the model changes, by setting the DB Initializer to a DropCreateDatabaseIfModelChanges, where TocratesDb is my derived DbContext. I have now made a…
ProfK
  • 49,207
  • 121
  • 399
  • 775
50
votes
2 answers

What does WebActivator do?

This code was generated for me after added entity framework code-first for SQL Server CE using NuGet. They did no changes to any other file. The file SQLCEEntityFramework.cs was created and placed in App_Start folder. Does this mean it automatically…
Shawn Mclean
  • 56,733
  • 95
  • 279
  • 406
50
votes
3 answers

Get a list of elements by their ID in entity framework

How can I get all elements that are in another list by ID? I have List roles; I'd like to get all roles from the database that are in this list by their Id. I'm using code-first. I did this and it threw an error: var roles = db.Roles.Where(r =>…
Shawn Mclean
  • 56,733
  • 95
  • 279
  • 406
49
votes
2 answers

Entity Framework 6 with SQLite 3 Code First - Won't create tables

Using latest versions of EF6 and SQLite from NuGet. I finally got the app.config file to work after some useful posts on Stackoverflow. Now the problem is that the tables are not being created although the database is. My app.config:
49
votes
6 answers

Delete .mdf file from app_data causes exception cannot attach the file as database

I am building a web application using VS 2012 MVC4 code first. In order to recreate the .mdf file after I changed the model, I manually deleted the file from the app_data directory in VS. I have done this a few times before without any problem. Now…
Shlo
  • 1,036
  • 2
  • 10
  • 23
48
votes
8 answers

No mapping exists from object type System.Collections.Generic.List when executing stored proc with parameters in EF 4.3

Lately I've been working on stored procedure and encountered 1 strange problem. First, I was able to successfully call a stored procedure from the database via: IList XXXList = _context.Database.SqlQuery("spXXX").ToList(); But…
czetsuya
  • 4,773
  • 13
  • 53
  • 99
48
votes
4 answers

What is the syntax for self referencing foreign keys in EF Code First?

I am trying to reference a foreign key from SpouseId to Id in the Contact table. What is the syntax for doing this? I can't seem to find an example. Thanks. I have a class like this: public class Contact { public int Id {get;set;} public…
trevorc
  • 3,023
  • 4
  • 31
  • 49
48
votes
2 answers

Defining multiple Foreign Key for the Same table in Entity Framework Code First

I have two entities in my MVC application and I populated the database with Entity Framework 6 Code First approach. There are two city id in the Student entity; one of them for BirthCity, the other for WorkingCity. When I define the foreign keys as…
48
votes
3 answers

EF5 Code First - Changing A Column Type With Migrations

I am new to EF5 Code First and I'm tinkering with a proof-of-concept before embarking on a project at work. I have initially created a model that looked something like public class Person { public int Id { get; set; } public string FirstName {…
Nick
  • 4,115
  • 10
  • 45
  • 57
47
votes
5 answers

Where can I find the console or debug output from code executed in the package manager window?

I'm using EntityFramework code first with migrations. From the package manager console, I'm running "update-database". This executes Configuration.Seed(context) which I have overridden. protected override void Seed(WebContext context) { …
46
votes
2 answers

EF Core Add Migration Debugging

How can I step into OnModelCreating with a breakpoint and see if my logic is wrong or if the ModelBuilder is doing something I'm not expecting? I've seen lots of posts on how to debug the actual migration, but nothing on how to watch how the model…
46
votes
7 answers

How to set default value for POCO's in EF CF?

In Entity Framework Code First approach, how do you set a default value for a property in the POCO's EntityConfiguration class? public class Person { public int Id { get; set; } public string Name { get; set; } public DateTime CreatedOn…
Andy
  • 5,287
  • 2
  • 41
  • 45