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
45
votes
2 answers

Code First Migration with Connection Strings

So I've managed to get Code First running and it works great. Since I am still developing the application the structure of the Database isn't finalize and so I need to implement migrations. I followed the Official Blog Post and got that…
Dragonseer
  • 2,874
  • 7
  • 29
  • 42
45
votes
2 answers

Mapping Columns in Entity Framework Code First

I'm having trouble trying to map my EF 4.1 Code First model to a database. Everything works fine when the database match the code exactly, but when I try and map when the columns differ in name then I am running into issues. I was following a…
Dismissile
  • 32,564
  • 38
  • 174
  • 263
45
votes
3 answers

Mapping a foreign key with a custom column name

I'm using Entity Framework 4.3 code-first with Oracle. I'm getting the following error: System.InvalidOperationException : The ForeignKeyAttribute on property 'WidgetSequence' on type 'WidgetDistributor.WidgetEntity' is not valid. The foreign key…
Byron Sommardahl
  • 12,743
  • 15
  • 74
  • 131
45
votes
3 answers

Entity Framework auto incrementing field, that isn't the Id

I know this isn't the most ideal solution, but I need to add an auto incrementing field to one of my EF Code First objects. This column id NOT the Id, which is a guid. Is there anyway for me to define the auto incrementing field in code, or would…
JamesStuddart
  • 2,581
  • 3
  • 27
  • 45
44
votes
5 answers

Code first DbMigrator causes error when building from different machines

We have a project under SCM. When I build it from my machine and publish to a remote server via msdeploy, everything works fine. When my colleague tries the same thing with the same project, freshly pulled from SCM, on the remote server entity…
spender
  • 117,338
  • 33
  • 229
  • 351
44
votes
3 answers

Entity Framework Code-first default data in database

How do I handle situations in which I need pre-existing data before the app is started or right after the database is generated. For example, I have a list of countries in which I'd like to load into the database after code-first generates it. How…
44
votes
3 answers

How to add new table to existing database code first

Originally I used EF 6 code first to create a new database and two new tables. The code is: public class TestingContext : DbContext, IDisposable { public DbSet CallDataRecords { get; set; } public DbSet Attempts {…
user1108948
44
votes
2 answers

EF Code First Fluent API specifying the Foreign Key property

I have a class AgentBalance with an association to Agent, thus: public class AgentBalance { ... public int AgentId { get; set; } public virtual Agent Agent { get; set; } } AgentId is detected as the FK for the Agent relationship by…
Richard Pawson
  • 1,494
  • 1
  • 10
  • 15
44
votes
1 answer

Problems creating a Foreign-Key relationship on Entity Framework

i'm having trouble configuring a foreign key relationship in my Entity Framework fluent Api: Here is the head of the report: public class Testata { public Testata() { Details = new List(); } public virtual int IDTEST { get; set;…
user2541621
  • 461
  • 1
  • 4
  • 3
44
votes
2 answers

LIKE query with Entity Framework

Possible Duplicate: How to do SQL Like % in Linq? Like Operator in Entity Framework? I'm doing a query like this: var matches = from m in db.Customers where m.Name == key select m; But I don't need m.Name to be exactly equal…
user1537004
43
votes
2 answers

Cannot enable migrations for Entity Framework in class library

I just got on board with EF 5 and am using their code-first migrations tool but I seem to get an error when I try to enable migrations. I type Enable-Migrations into the package manager console and then it says No classes deriving from DbContext…
43
votes
3 answers

How do I map TimeSpan with greater than 24 hours to SQL server Code First?

I am trying to map a TimeSpan Code First property to SQL server. Code First seems to be creating it as a Time(7) in SQL. However TimeSpan in .Net can handle longer periods than 24 hours and I need to store longer than 24 hour for event length. What…
GraemeMiller
  • 11,973
  • 8
  • 57
  • 111
43
votes
3 answers

Confusion over EF Auto Migrations and seeding - seeding every program start

I recently changed an application from using the following for dev: DropCreateDatabaseIfModelChanges To using: public class MyDbMigrationsConfiguration: DbMigrationsConfiguration { public MyDbMigrationsConfiguration() …
41
votes
4 answers

Entity Framework loading child collection with sort order

I have two tables a parent and a child table. The child table has a column sortorder (a numeric value). Because of the missing support of the EF to persist a IList inclusive the sort order without exposing the sortorder (see: Entity Framework…
X181
  • 753
  • 1
  • 5
  • 12
41
votes
4 answers

EF Model First or Code First Approach?

I know this question has been asked numerous times before as I’ve read quite a few posts on the topic about the pros and cons etc but I still can’t decide which approach is right for me. I’m very new to web programming and come from a SQL DB Admin /…