An Entity Framework Code First class corresponding to a table with a foreign key typically has two fields for the foreign key. The foreign key as represented in the database and a C# reference. [ForeignKey("BrandId")] public Brand Brand { get; set; } [ForeignKey("Brand")] public int BrandId { get; set; }[ForeignKey("BrandId")] public Brand Brand {… Continue reading EF Code First Navigation Properties and Foreign Keys
Tag: Entity Framework
EF Migrations Command Reference
Entity Framework Migrations are handled from the package manager console in Visual Studio. The usage is shown in various tutorials, but I haven’t found a complete list of the commands available and their usage, so I created my own. There are four available main commands. Enable-Migrations: Enables Code First Migrations in a project. Add-Migration: Scaffolds a migration… Continue reading EF Migrations Command Reference
EF Code First Change Tracking
Change tracking is a central concept for every Object-Relational Mapper, including Entity Framework. When doing updates to objects the normal work flow with Entity Framework has three steps. Retrieve data from the database. Update some properties on some objects. Save the updates to the database. In the third step Entity Framework has to find out… Continue reading EF Code First Change Tracking
Prevent EF Migrations from Creating or Changing the Database
One of the features of Entity Framework Code First is to automatically create the database on first access. It is a convenient approach in many cases. Unfortunately the applications I work with does not fall into one of those cases. The applications I develop typically run on a dedicated web server, with the database hosted… Continue reading Prevent EF Migrations from Creating or Changing the Database
EF Migrations and a Merge Conflict
To put a bit more stress on EF Migrations I’ll simulate a scenario that should be quite common in a multi developer environment. Developer A and B gets the latest code from the repository. Developer A adds a column and checks in a migration. Developer B adds another column and makes everything ready for checkin.… Continue reading EF Migrations and a Merge Conflict