Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Weird behaviour in Select using LINQ #24118

Closed
boppercr opened this issue Feb 10, 2021 · 3 comments
Closed

Weird behaviour in Select using LINQ #24118

boppercr opened this issue Feb 10, 2021 · 3 comments

Comments

@boppercr
Copy link

My colleague and I encountered a weird "bug" if you will while using EF Core and while we're not sure if it's a bug or a use case that shouldn't be happening, I felt like it needed to be asked here.

Here's a little summary :

  • We have a certain class/table structure that looks like this : X can have one Y and multiple Z. Z can have Ws and other stuff that is not important.

We have a goal to duplicate part of a record of table X and all that follows. This includes an empty record of Y. We do this by fetching the data we want duplicated in from the X table with the corresponding ID and using a Select, we create a new record of X with some of the old values and some new.

The issue comes when trying to add a new record of Y. We do something like this :

Context.X.Where(x => x.Id == parameterId).Select(x => new X {
    _other properties initialization_
   newY = new Y(),
   _some more properties initialization_
}

The first time, everything is good and works fine. The issue raises when we try another time to duplicate the same record again. The new Y() will contain the values of the previous one we created, even though it's a new object. This issue won't happen if we create the Y object outside of the Select request.

Anyone has an idea what could be happening here?

I know it's fairly weird/complicated, but I'll try to answer all the questions to the best of my knowledge.

EF Core version: 2.2.2
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: e.g. .NET 2.2
Operating system: Windows 10
IDE: Visual Studio 2017 15.9.27

@roji
Copy link
Member

roji commented Feb 10, 2021

@boppercr can you please submit a minimal code sample that demonstrates exactly what you're trying to do, and the exact exception and/or incorrect behavior that you're seeing? That's really the easiest way to get your scenario across.

Aside from that, 2.2 is out of support - please try your scenario with 3.1 (which fixes a lot of issues).

@boppercr
Copy link
Author

Alright. We have an update planned soon to get straight through to 5, so we'll see there. There's no real problems remaining, but I was curious still. Here's what I can provide :

using (var dc = new Database.DatabaseService())
            {
                var decl = dc.Declaration.Where(d => d.IdDeclaration == IdDeclaration).Select(d => new Database.DroitAuteur.Declaration()
                {
                    //_Shortened for brevety_
                    DateCreation = DateTime.Now,
                    IdTypeOuvrage = d.IdTypeOuvrage,
                    IdTypeSupport = d.IdTypeSupport,
                    AnneeSession = d.AnneeSession,
                    IdCours = d.IdCours,
                    IdEmployeEffectuePour = d.IdEmployeEffectuePour,
                    Reference = d.Reference,
                    DeclarationInformation = new Database.DroitAuteur.DeclarationInformation(), //Here's where the issue happens.
                    DeclarationDoc = d.DeclarationDoc.Select(doc => new Database.DroitAuteur.DeclarationDoc()
                    {
                        Annee = doc.Annee,
                        Auteur = doc.Auteur,
                       //_Shortened for brevety_
                    }).ToList(),
                }).FirstOrDefault();
                if (decl == null) return (false, null);

                dc.Add(decl);
                dc.SaveChanges();

                return (true, decl.IdDeclaration);
            }

In example, the "DeclarationInformation" class has a couple of properties which include the navigation ones. There's "IdDeclarationInformation" that is the Primary Key and "IdDeclaration" that is a Foreign Key with a Unique Constraint on it that links it to "Declaration". At first, theses 2 values are default(int)|0 and the second time it happens, the values are the ones that were generated when the "SaveChanges" function was called. Example : {IdDeclarationInformation:810, IdDeclaration:817}

@smitpatel
Copy link
Member

Duplicate of #7983

@smitpatel smitpatel marked this as a duplicate of #7983 Feb 10, 2021
@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants