sapiens / SqlFu

Fast and versatile .net core data mapper/micro-orm
Other
229 stars 50 forks source link

Insert causes exception due to ignored column #21

Closed kuechlerm closed 10 years ago

kuechlerm commented 11 years ago

If I define a table like so:

[Table("Foos")]
public class Foo
{
  public int Id { get; set; }
  public string Text { get; set; }

  [ColumnOptions(Ignore = true)]
  public bool IgnoreMe { get; set; }
}

The table gets created properly by the DatabaseTools, but doing a

 db.Insert(new Foo { Text = "Hello" });

results in System.Data.SqlClient.SqlException : Invalid column name 'IgnoreMe'.

Any ideas?

sapiens commented 11 years ago

It works properly, [ColumnOptions] is for table creation only. For other purposes the attribute to use is [QueryOnly]

kuechlerm commented 11 years ago

Tried that, but then you are not able to query, because the IgnoreMe-Column is included in the query.

sapiens commented 11 years ago

So why are you adding properties that we'll never get used ? That POCO should be a DTO to get/move data from the db.

kuechlerm commented 11 years ago

I use them, I just don't want to persist them. I wonder why there is an option to "Ignore" properties that makes the DTO useless. Don't get me wrong, I really like SqlFu and I think you have some great features, but this one led me into a trap.

sapiens commented 11 years ago

I've put the Ignore property on a create table because someone might need it in a edge case scenario. [QueryOnly] is to avoid the property being used with inserts/updates helpers. SqlFu is a collection of data mapping related tools, you can use one object to create a table and other to query it or to map a db result to. It doesn't aim to be a real ORM.

ohiodoug commented 11 years ago

Like sapiens said, it isn't the best practice to add additional properties to a domain model, but there are always exceptions. However, you can get around your particular issue in sqlfu by changing IgnoreMe to a public member variable instead of a property.