Entity Framework Core

The seed entity for entity type 'X' cannot be added because a non-zero value is required for property 'Id'

In Entity Framework Core, primary key properties are converted to identity columns if the database provider is SQL Server. But when we try to seed a database table with an identity column, the corresponding property value should be provided, as the migration scripts are generated by Entity Framework without connecting to the database.

Tags:  Troubleshooting

Data Annotations in EF Core

.NET provides a set of data annotation attributes that can be used to define constraints to the tables and the columns in the underlying database and override  Entity Framework Core conventions.

Conventions in Entity Framework Core

Conventions are the default rules that control how a model will be mapped to a database schema. Entity Framework core follows these default rules when it is not overridden using Data Annotation Attributes and Fluent API.

Entity Framework Core Migrations

In real-world applications, data models change when new features are added to the application. The model changes have to be propagated to the database to keep the database in sync with the model. Entity Framework Core migrations enable us to incrementally update the changes in the models to the database while preserving the existing data there.

Entity Framework Core DbContext

In simple terms, DbContext is a class in Entity Framework Core that you can inherit in your application and use to access the database and do all the operations there. DbContext can manage database connections, create tables/models and manage their relationships, perform CRUD (create read, update, and delete)operations, track changes, manage transactions, provide first-level caching of objects from the data source, etc.

Installation of Entity Framework Core

Entity Framework Core is not a part of .NET Core. It is available as a Nuget Package that can be added to your project in many ways based on your development environment and the available tools. 

Introduction to Entity Framework Core (EF Core)

Entity Framework Core (EF Core) is an open-source ORM(Object Relational Mapper) framework for .NET applications. An ORM is a framework connecting objects in your application with the tables and fields in a database where the application stores data. Entity Framework Core enables .NET developers to work with a database using .NET objects. It is extensible, lightweight, and supports cross-platform development as it is a part of Microsoft’s .NET Core framework.

Showing 2 of 2