Coder Social home page Coder Social logo

tomashubelbauer / ef-seed-ids Goto Github PK

View Code? Open in Web Editor NEW
0.0 3.0 0.0 10 KB

A repro showing EF to auto-generating a successive ID after seeding the DB with hardcoded IDs.

C# 100.00%
ef-core ef-code-first entity-framework entity-framework-core

ef-seed-ids's Introduction

EF IDs Atop Seeded IDs

This repository demonstrates that how EF Core will continue assigning IDs in sequence after a database has been seeded with hardcoded IDs.

This used not to work but has since been implemented and is shown in the current codebase of this repository.

This used to be a pain point for me and indeed this not working is a nice addition, however I have since adopted a practice of instead of hardcoding IDs in the seed using .NET object references so that the change tracker is free to assign any IDs it likes to the entities, because the relations are given by the references that get passed around which are stable within the unit of work.

As an example of this, instead of:

dbContext.Users.Add(new User { Id = 1, Role = "admin", Name = "Tomas Hubelbauer" });
dbContext.Documents.Add(new Document { Id = 1, OwnerUserId = 1, Content = "empty doc" });

One would write this:

var tomasHubelbauerUser = new User { Role = "admin", Name = "Tomas Hubelbauer" };
var tomasHubelbauerEmptyDocument = new Document { OwnerUser = tomasHubelbauerUser, Content = "empty doc" };
dbContext.Users.Add(tomasHubelbauerUser);
dbContext.Documents.Add(tomasHubelbauerEmptyDocument);

There can even only be a single Add because the nagivation property will make sure the related entity gets persisted while saving the one which gets added explicitly.

This repository uses SQLite to demonstrate (because inlike LocalDB) it is cross-platform and in-memory is not relational. It was set up this way:

dotnet new console
dotnet add package Microsoft.EntityFrameworkCore.Sqlite
# Set up the DB context and entity model classes
dotnet run

To-Do

ef-seed-ids's People

Contributors

tomashubelbauer avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.