Giter Club home page Giter Club logo

sampleef's Introduction

SampleEF

I've run into the situation where I hoped EF would be smarter than me

Giving the following object:

var invoice = new Invoice
{
    State = State.Added,
    Number = "SALE-001",
    Lines =
    {
        new InvoiceLine
        {
            State = State.Added,
            Price = 10,
            Quantity = 1,
            Item = new Item { Id = 1, Description = "Product" }
        },
        new InvoiceLine
        {
            State = State.Added,
            Price = 10,
            Quantity = 1,
            DiscountPercentage = 50,
            Item = new Item { Id = 1, Description = "Product" }
        },
    }
}

Using the pattern to track state locally (e.g. with a BaseModel interface that implements a State property) as described in Programming Entity Framework: DbContext, Chapter 4.

I got stuck when setting the Entries EntityState to align with the local State. Because instantiating the items like you see in the example above, results in two entries for Item in the ChancheTracker with the same Primary Key.

When I try to update the State:

Saving or accepting changes failed because more than one entity of type 'AutomapperWithEF.Data.Domain.Item' have the same primary key value. Ensure that explicitly set primary key values are unique. Ensure that database-generated primary keys are configured correctly in the database and in the Entity Framework model. Use the Entity Designer for Database First/Model First configuration. Use the 'HasDatabaseGeneratedOption" fluent API or 'DatabaseGeneratedAttribute' for Code First configuration.

Question is: why does EF allows these two entities (actually one entity) but with a different place in memory to exists as seperate in the ChangeTracker? Shouldn't the objects be merged somehow?

sampleef's People

Contributors

svieillard 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.