jonwagner / Insight.Database

Fast, lightweight .NET micro-ORM
Other
861 stars 145 forks source link

ColumnAttribute maps only the last property when used in multiples properties with the same column name #391

Closed canro91 closed 5 years ago

canro91 commented 5 years ago

Describe the bug

When the Column attribute is used with the same column name in two different properties, Insight Database only maps the last property with the attribute.

Steps to reproduce

        class Data
        {
            [Column("Name")]
            public string BeerName { get; set; }

            [Column("Name")]
            public string Alias { get; set; }
        }

        [Test]
        public void Column()
        {
            var connectionString = "Data Source=.;Initial Catalog=InsightTest;Integrated Security=True";
            var connection = new SqlConnection(connectionString);
            Data data = connection.QuerySql<Data>("SELECT Name FROM Beer WHERE Name = 'ABeerName'").First();

            Assert.AreEqual("ABeerName", data.Alias);
            Assert.AreEqual("ABeerName", data.BeerName);
        }

Expected behavior

Insight Database should map all the properties with the custom mapping for the given column, no matter if the Column attribute is used with the same column name more than once. A workaround is to define a property as an accessor for the previous one. But, is it an intended behaviour for the Column attribute?

jonwagner commented 5 years ago

Having a duplicate column mapping should result in an error.

Although your read scenario has some applications, in the write scenario, a duplicate mapping would result in undefined behavior.

If you need to bind multiple properties to a data column, you should be explicit and use an accessor property as you have noted.

canro91 commented 5 years ago

Thanks for your answer, jonwagner. It would be great if that note could be added to the wiki just to make things clear.

Jaxelr commented 5 years ago

Closing.