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

Duplicate variable names when batching inserts with only default values #4095

Closed
mikary opened this issue Dec 15, 2015 · 1 comment
Closed
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Milestone

Comments

@mikary
Copy link
Contributor

mikary commented Dec 15, 2015

When attempting to add multiple instances with default values to the context, a System.Data.SqlClient.SqlException is thrown on SaveChanges with the message "The variable name '@generated5' has already been declared. Variable names must be unique within a query batch or stored procedure."

Reproduction

    public class Program
    {
        public void Main(string[] args)
        {
            using (var Sql = new TestDataModel())
            {
                Sql.Database.EnsureDeleted();
                Sql.Database.EnsureCreated();

                var pictures = new List<Picture>();

                for (var index = 0; index < 15; index++)
                {
                    var picture = new Picture();
                    Sql.Pictures.Add(picture);
                    pictures.Add(picture);
                }

                for (var index = 0; index < 5; index++)
                {
                    var package = new PicturePackage
                    {
                        Pictures = new List<Picture>
                        {
                            pictures[3 * index],
                            pictures[3 * index + 1],
                            pictures[3 * index + 2]
                        }
                    };

                    Sql.PicturePackages.Add(package);
                }

                Sql.SaveChanges();
            }
        }
    }

    // Sample database with object relations.
    public class TestDataModel : DbContext
    {
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer(
                new SqlConnectionStringBuilder
                {
                    DataSource = @"(localdb)\MSSQLLocalDB",
                    MultipleActiveResultSets = true,
                    InitialCatalog = "BatchingDefaults",
                    IntegratedSecurity = true,
                    ConnectTimeout = 30
                }.ConnectionString);
        }

        public virtual DbSet<Picture> Pictures { get; set; }
        public virtual DbSet<PicturePackage> PicturePackages { get; set; }
    }

    public class Picture
    {
        public int PackageId { get; set; }

        [Required]
        [ForeignKey(nameof(PackageId))]
        public PicturePackage Package { get; set; }

        [Key]
        public int Id { get; set; }

    }

    public class PicturePackage
    {
        [InverseProperty(nameof(Picture.Package))]
        public virtual ICollection<Picture> Pictures { get; set; }

        [Key]
        public int Id { get; set; }
    }

Generated SQL

SET NOCOUNT ON;
DECLARE @generated5 TABLE ([Id] int);
INSERT INTO [PicturePackage]
OUTPUT INSERTED.[Id]
INTO @generated5
DEFAULT VALUES;
SELECT [Id] FROM @generated5;
DECLARE @generated5 TABLE ([Id] int);
INSERT INTO [PicturePackage]
OUTPUT INSERTED.[Id]
INTO @generated5
DEFAULT VALUES;
SELECT [Id] FROM @generated5;
DECLARE @generated5 TABLE ([Id] int);
INSERT INTO [PicturePackage]
OUTPUT INSERTED.[Id]
INTO @generated5
DEFAULT VALUES;
SELECT [Id] FROM @generated5;
DECLARE @generated5 TABLE ([Id] int);
INSERT INTO [PicturePackage]
OUTPUT INSERTED.[Id]
INTO @generated5
DEFAULT VALUES;
SELECT [Id] FROM @generated5;
DECLARE @generated5 TABLE ([Id] int);
INSERT INTO [PicturePackage]
OUTPUT INSERTED.[Id]
INTO @generated5
DEFAULT VALUES;
SELECT [Id] FROM @generated5;
@AndriySvyryd
Copy link
Member

Fixed in 71c1769

@AndriySvyryd AndriySvyryd removed this from the 1.0.0-rc2 milestone Apr 1, 2016
@AndriySvyryd AndriySvyryd removed their assignment Apr 1, 2016
@AndriySvyryd AndriySvyryd added this to the 1.0.0-rc2 milestone Apr 1, 2016
@ajcvickers ajcvickers modified the milestones: 1.0.0-rc2, 1.0.0 Oct 15, 2022
@ajcvickers ajcvickers added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Oct 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Projects
None yet
Development

No branches or pull requests

3 participants