Skip to content

IgnoreAttribute not working #586

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

Closed
blimkemann opened this issue Jul 26, 2017 · 9 comments
Closed

IgnoreAttribute not working #586

blimkemann opened this issue Jul 26, 2017 · 9 comments
Assignees
Labels

Comments

@blimkemann
Copy link

blimkemann commented Jul 26, 2017

I have a base class that includes [Ignore] on a couple of properties. I am calling CreateTable on a derived class and the ignored properties in the base class are not being ignored.

Example:

public class BaseClass
{
  [Ignore]
  public string ToIgnore
  {
    get;
    set;
  }
}

public class TableClass : BaseClass
{
  public string Name { get; set; }
}

Calling CreateTable<TableClass> creates a table with Name AND ToIgnore!

This is in a NetStandard project.

A fix involves modifying the TableMapping method as follows:

var cols = new List&lt;Column&gt; ();
			foreach (var p in props) {
				var ignore = p.GetCustomAttribute&lt;IgnoreAttribute&gt;(); //p.IsDefined(typeof(IgnoreAttribute),true);
				if (p.CanWrite && ignore==null) { //!ignore) {
					cols.Add (new Column (p, createFlags));
				}
			}
@praeclarum
Copy link
Owner

Oh thanks for catching that. Have to check the tests to see why this wasn't caught.

I thought p.IsDefined(typeof(IgnoreAttribute),true); was enough...

@praeclarum
Copy link
Owner

I can't repro the bug -- IsDefined(..., true) works.

Which version of .NET are you running this on?

@blimkemann
Copy link
Author

blimkemann commented Jul 29, 2017 via email

@hvaughan3
Copy link

@praeclarum @blimkemann
Possibly related to my post here?

@blimkemann
Copy link
Author

blimkemann commented Aug 1, 2017 via email

@praeclarum
Copy link
Owner

Here is the test code I wrote: https://github.com/praeclarum/sqlite-net/blob/master/tests/IgnoreTest.cs#L95-L126

		public class BaseClass
		{
			[Ignore]
			public string ToIgnore {
				get;
				set;
			}
		}

		public class TableClass : BaseClass
		{
			public string Name { get; set; }
		}

		[Test]
		public void BaseIgnores ()
		{
			var db = new TestDb ();
			db.CreateTable<TableClass> ();

			var o = new TableClass {
				ToIgnore = "Hello",
				Name = "World",
			};

			db.Insert (o);

			var oo = db.Table<TableClass> ().First ();

			Assert.AreEqual (null, oo.ToIgnore);
			Assert.AreEqual ("World", oo.Name);
		}

The test passes. Is your code different?

@praeclarum praeclarum self-assigned this Aug 1, 2017
@praeclarum praeclarum added the Bug label Aug 1, 2017
@blimkemann
Copy link
Author

blimkemann commented Aug 1, 2017 via email

@hvaughan3
Copy link

hvaughan3 commented Aug 1, 2017

A test case for my specific issue would look like this (though I have not actually tried running the following code myself):

        public class BaseClass
	{
		public List<string> ToIgnore {
			get;
			set;
		}
	}

            //EDIT: Adding new keyword after praeclarum's comment
	public class TableClass : BaseClass
	{
                [Ignore]
		public new List<string> ToIgnore {
			get;
			set;
		}
	}

When it tries to create TableClass table it will ignore the [Ignore] attribute in the derived class.

@praeclarum
Copy link
Owner

OK, that's a bit different than the test (you would need the new keyword FYI). I'll try that out.

github-actions bot pushed a commit to Reddevildragg-UPM-Forks/sqlite-net that referenced this issue Nov 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants