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.
ef-code-first
8,538
questions
655
votes
10
answers
354k
views
Code-first vs Model/Database-first [closed]
What are the pros & cons of using Entity Framework 4.1 Code-first over Model/Database-first with EDMX diagram?
I'm trying to fully understand all the approaches to building data access layer ...
423
votes
20
answers
384k
views
Introducing FOREIGN KEY constraint may cause cycles or multiple cascade paths - why?
I've been wrestling with this for a while and can't quite figure out what's happening. I have a Card entity which contains Sides (usually 2) - and both Cards and Sides have a Stage. I'm using EF ...
376
votes
2
answers
297k
views
Ignoring a class property in Entity Framework 4.1 Code First
My understanding is that the [NotMapped] attribute is not available until EF 5 which is currently in CTP so we cannot use it in production.
How can I mark properties in EF 4.1 to be ignored?
UPDATE: ...
329
votes
7
answers
153k
views
Create code first, many to many, with additional fields in association table
I have this scenario:
public class Member
{
public int MemberID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public virtual ICollection&...
326
votes
12
answers
263k
views
Unique Key constraints for multiple columns in Entity Framework
I'm using Entity Framework 5.0 Code First;
public class Entity
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public string EntityId { get; set;}
public int FirstColumn { get;...
290
votes
19
answers
254k
views
Decimal precision and scale in EF Code First
I'm experimenting with this code-first approach, but I'm find out now that a property of type System.Decimal gets mapped to a sql column of type decimal(18, 0).
How do I set the precision of the ...
280
votes
11
answers
176k
views
Entity Framework: One Database, Multiple DbContexts. Is this a bad idea? [closed]
My impression to date has been that a DbContext is meant to represent your database, and thus, if your application uses one database, you'd want only one DbContext.
However, some colleagues want to ...
270
votes
19
answers
363k
views
Entity Framework 6 Code first Default value
is there "elegant" way to give specific property a default value ?
Maybe by DataAnnotations, something like :
[DefaultValue("true")]
public bool Active { get; set; }
Thank you.
252
votes
2
answers
106k
views
What effect(s) can the virtual keyword have in Entity Framework 4.1 POCO Code First?
Does the virtual keyword has an effect when used on the properties in EF Code First?. Can someone describe all of its ramifications in different situations?
For instance, I know it can control lazy ...
201
votes
8
answers
58k
views
Getting exact error type in from DbValidationException
I have the situation where I'm initializing my model in DatabaseInitializer() for EF 4.1 and get this annoying error "Validation failed for one or more entities. See 'EntityValidationErrors' property ...
198
votes
3
answers
147k
views
How do I detach objects in Entity Framework Code First?
There is no Detach(object entity) on the DbContext.
Do I have the ability to detach objects on EF code first?
182
votes
9
answers
166k
views
StringLength vs MaxLength attributes ASP.NET MVC with Entity Framework EF Code First
What is the difference in behavior of [MaxLength] and [StringLength] attributes?
As far as I can tell (with the exception that [MaxLength] can validate the maximum length of an array) these are ...
177
votes
24
answers
301k
views
The entity type <type> is not part of the model for the current context
I am getting into the Entity Framework, but I am unsure if I am missing a critical point in the code-first approach.
I am using a generic repository pattern based on the code from https://...
173
votes
8
answers
132k
views
EF Code First "Invalid column name 'Discriminator'" but no inheritance
I have a table in my database called SEntries (see below the CREATE TABLE statement). It has a primary key, a couple of foreign keys and nothing special about it. I have many tables in my database ...
171
votes
4
answers
110k
views
ICollection<T> Vs List<T> in Entity Framework
I only watched a few webcasts before I went head first in to designing a few Entity Framework applications. I really didn't read that much documentation and I feel like I am suffering for it now.
I ...
162
votes
5
answers
170k
views
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]...
156
votes
7
answers
48k
views
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 ...
145
votes
28
answers
152k
views
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 ...
144
votes
31
answers
313k
views
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 ...
142
votes
6
answers
75k
views
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 ...
142
votes
5
answers
157k
views
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 ...
141
votes
15
answers
195k
views
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 ...
132
votes
11
answers
123k
views
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 ?
125
votes
13
answers
185k
views
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 ...
121
votes
10
answers
79k
views
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 =...
116
votes
6
answers
126k
views
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 ...
113
votes
3
answers
115k
views
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 ...
109
votes
3
answers
106k
views
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).
...
108
votes
2
answers
199k
views
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 ...
107
votes
7
answers
126k
views
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 {
[...
105
votes
2
answers
52k
views
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 ...
99
votes
4
answers
47k
views
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 ...
97
votes
1
answer
58k
views
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 ...
95
votes
8
answers
98k
views
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 ...
89
votes
4
answers
71k
views
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:
<connectionStrings>
<clear/>
<add name="ApplicationServices" connectionString="Data Source=...
88
votes
3
answers
91k
views
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 ...
88
votes
4
answers
77k
views
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 ...
81
votes
7
answers
104k
views
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 { ...
81
votes
14
answers
132k
views
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 ...
81
votes
6
answers
34k
views
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
{
...
78
votes
5
answers
60k
views
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 ...
75
votes
8
answers
81k
views
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 ...
75
votes
6
answers
109k
views
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 ...
73
votes
6
answers
41k
views
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 ...
72
votes
12
answers
98k
views
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 (...
70
votes
12
answers
93k
views
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 : ...
70
votes
8
answers
16k
views
EF: Validation failing on update when using lazy-loaded, required properties
Given this extremely simple model:
public class MyContext : BaseContext
{
public DbSet<Foo> Foos { get; set; }
public DbSet<Bar> Bars { get; set; }
}
public class Foo
{
...
69
votes
6
answers
54k
views
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 ...
68
votes
14
answers
119k
views
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
...
68
votes
5
answers
50k
views
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 ...