Skip to main content

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
Filter by
Sorted by
Tagged with
656 votes
10 answers
356k 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 ...
Jakub Konecki's user avatar
427 votes
20 answers
395k 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 ...
SB2055's user avatar
  • 12.6k
376 votes
2 answers
302k 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: ...
Raheel Khan's user avatar
  • 14.4k
329 votes
7 answers
154k 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&...
hgdean's user avatar
  • 3,293
327 votes
12 answers
267k 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;...
Bassam Alugili's user avatar
291 votes
20 answers
259k 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 ...
Dave Van den Eynde's user avatar
281 votes
11 answers
180k 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 ...
Josh Schultz's user avatar
  • 8,130
271 votes
19 answers
370k 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.
marino-krk's user avatar
  • 2,721
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 ...
Scott Stafford's user avatar
202 votes
3 answers
150k 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?
Shawn Mclean's user avatar
  • 57.2k
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 ...
Naz's user avatar
  • 5,144
184 votes
9 answers
170k 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 ...
Nick Goloborodko's user avatar
178 votes
24 answers
304k 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://...
janhartmann's user avatar
  • 14.9k
173 votes
8 answers
134k 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 ...
Marcelo Calbucci's user avatar
172 votes
4 answers
113k 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 ...
wil's user avatar
  • 2,079
161 votes
5 answers
176k 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]...
loyalflow's user avatar
  • 14.7k
157 votes
7 answers
49k 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 ...
Daniel's user avatar
  • 3,346
146 votes
28 answers
157k 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 ...
user1265146's user avatar
  • 2,055
146 votes
6 answers
78k 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 ...
RationalGeek's user avatar
  • 9,551
145 votes
32 answers
320k 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 ...
x19's user avatar
  • 8,651
143 votes
5 answers
160k 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 ...
Shawn Mclean's user avatar
  • 57.2k
141 votes
15 answers
196k views

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

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 ...
Alex Angas's user avatar
  • 59.7k
133 votes
11 answers
125k 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 ?
Valo's user avatar
  • 1,991
126 votes
13 answers
191k 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 ...
g.pickardou's user avatar
  • 34.7k
123 votes
10 answers
82k 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 =...
Brian Sullivan's user avatar
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 ...
J86's user avatar
  • 15k
115 votes
3 answers
117k 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 ...
DotnetSparrow's user avatar
109 votes
3 answers
109k 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). ...
bairog's user avatar
  • 3,271
108 votes
2 answers
204k 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 ...
Guido Anselmi's user avatar
107 votes
7 answers
128k 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 { [...
CodeDemen's user avatar
  • 1,931
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 ...
bugnuker's user avatar
  • 3,948
100 votes
4 answers
48k 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 ...
Mel's user avatar
  • 3,088
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 ...
Lrayh's user avatar
  • 1,291
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 ...
Andriy Drozdyuk's user avatar
89 votes
4 answers
72k 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=...
Hodaya Shalom's user avatar
88 votes
3 answers
92k 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 ...
Max Schmeling's user avatar
88 votes
4 answers
79k 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 ...
Kassem's user avatar
  • 8,206
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 { ...
Ilkay Ilknur's user avatar
81 votes
14 answers
134k 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 ...
Jez's user avatar
  • 29.2k
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 { ...
Iman Mahmoudinasab's user avatar
78 votes
5 answers
61k 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 ...
Jez's user avatar
  • 29.2k
75 votes
8 answers
82k 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 ...
Konrad Viltersten's user avatar
75 votes
6 answers
110k 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 ...
Nick Olsen's user avatar
  • 6,349
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 ...
maxlego's user avatar
  • 4,894
72 votes
12 answers
96k 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 : ...
Sauron's user avatar
  • 2,296
72 votes
12 answers
99k 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 (...
Saber Amani's user avatar
  • 6,479
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 { ...
Diego Mijelshon's user avatar
69 votes
6 answers
55k 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 ...
devdigital's user avatar
  • 34.3k
68 votes
14 answers
124k 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 ...
Shiva's user avatar
  • 20.8k
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 ...
matt_dev's user avatar
  • 5,226

1
2 3 4 5
171