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
59
votes
3 answers

Entity Framework (EF) Code First Cascade Delete for One-to-Zero-or-One relationship

Following the "Code First Modeling" section of the Pluralsight "Getting Started with Entity Framework 5" course by Julie Lerman, I created two POCO classes with a one-to-zero-or-one relationship: a parent (User) and an optional child…
59
votes
4 answers

Improve navigation property names when reverse engineering a database

I'm using Entity Framework 5 with Visual Studio with Entity Framework Power Tools Beta 2 to reverse engineer moderately sized databases (~100 tables). Unfortunately, the navigation properties do not have meaningful names. For example, if there are…
marapet
  • 54,856
  • 12
  • 170
  • 184
58
votes
5 answers

EF 6.1 Unique Nullable Index

In EF 6.1 using Code First you can create Indexes using Attributes in your Entities or using the fluent API along the lines of: Property(x => x.PropertyName) .IsOptional() .HasMaxLength(450) …
MrEdmundo
  • 5,105
  • 13
  • 46
  • 58
57
votes
13 answers

Ambiguous references with the exact same namespace

The RC class is not linked to a database, it is a simple class. The class is at only one place and is not partial. The Aérochem.Domain dll project compiles just fine. Note: If I select one of the two identical namespaces in the quick fix menu, it…
Mathieu
  • 4,449
  • 7
  • 41
  • 60
56
votes
5 answers

Can I change the default schema name in entity framework 4.3 code-first?

Currently I am deploying my application to a shared hosting environment and code-first with migrations has been working great except for one minor hiccup. Everytime I want to push the site I have to use the "Update-Database -script" option because I…
CatDadCode
  • 58,507
  • 61
  • 212
  • 318
56
votes
2 answers

WithOptionalDependent vs WithOptionalPrinciple - Definitive Answer?

I thought it might be helpful to get a definitive answer on when to use WithOptionalDependent and when to use WithOptionalPrincipal. The help for the two functions is a little unclear, and I find myself digging through multiple Stack Overflow…
Tim
  • 14,999
  • 1
  • 45
  • 68
56
votes
1 answer

MapKey vs HasForeignKey Difference - Fluent Api

What is actually the difference between: this.HasRequired(a => a.Something) .WithMany() .Map(a => a.MapKey("SomethingId")); and this.HasRequired(a => a.Something) .WithMany() .HasForeignKey(a => a.SomethingId);
parliament
  • 21,544
  • 38
  • 148
  • 238
55
votes
3 answers

How do I singularize my tables in EF Code First?

I prefer using singular nouns when naming my database tables. In EF code first however, the generated tables always are plural. My DbSets are pluralized which I believe is where EF is generating the names but I don't want to singularize these…
trevorc
  • 3,023
  • 4
  • 31
  • 49
55
votes
5 answers

Entity Framework, Code First and Full Text Search

I realize that a lot of questions have been asked relating to full text search and Entity Framework, but I hope this question is a bit different. I am using Entity Framework, Code First and need to do a full text search. When I need to perform the…
Eric
  • 1,945
  • 3
  • 23
  • 33
54
votes
16 answers

An error occurred while saving entities that do not expose foreign key properties for their relationships

I have a simple code in Entity Framework (EF) v4.1 code first: PasmISOContext db = new PasmISOContext(); var user = new User(); user.CreationDate = DateTime.Now; user.LastActivityDate = DateTime.Now; user.LastLoginDate =…
user278618
  • 19,306
  • 42
  • 126
  • 196
53
votes
4 answers

CodeFirst EF4.1 MVC Against legacy database - Multiplicity conflicts

No matter which way I mix it, it gives me errors. I have a feeling I'm missing something obvious as I keep getting these errors. One or more validation errors were detected during model generation: System.Data.Edm.EdmAssociationType: :…
David C
  • 2,766
  • 6
  • 29
  • 44
53
votes
12 answers

How do I specify that a property should generate a TEXT column rather than an nvarchar(4000)

I'm working with the Code First feature of Entity Framework and I'm trying to figure out how I can specify the column data types that should be created when the database is auto-generated. I have a simple model: public class Article { public int…
Mark Bell
  • 28,985
  • 26
  • 118
  • 145
53
votes
5 answers

Get return value from stored procedure

I'm using Entity Framework 5 with the Code First approach. I need to read the return value from a stored procedure; I am already reading output parameters and sending input parameters, but I don't know how to read the return value. Is it…
53
votes
1 answer

Non-static method requires a target. Entity Framework 5 Code First

I am getting the error "Non-static method requires a target." when I run the following query: var allPartners = DbContext.User .Include(u => u.Businesses) .Where(u => u.Businesses.Any(x => x.Id…
ilivewithian
  • 19,476
  • 19
  • 103
  • 165
53
votes
5 answers

How do I set a column in SQL Server to varchar(max) using ASP.net EF Codefirst Data Annotations?

I've been searching around the web trying to figure out the right syntax to have Entity Framework Code First create my table with a column: varchar(max). This is what I have. By default this creates varchar(128). How do I create varchar(max)? I have…
Maddhacker24
  • 1,841
  • 7
  • 26
  • 32