12

I know it's a stupid question, but I could not find the answer anywhere. How to set a default value for a column in sqlite.net model? Here is my model class:

public class ItemTaxes
{
    [PrimaryKey]
    public string Sku { get; set;}
    public bool IsTaxable { get; set;}// How to set IsTaxable's default value to true?
    public decimal PriceTaxExclusive { get; set;}
}

I wanna set default value of Not Null column IsTaxable to true, how should I achieve that? Btw I do not want use raw sql statement, i.e. conn.execute();

Thank you.

Morteza Soleimani
  • 2,652
  • 3
  • 25
  • 44
macio.Jun
  • 9,647
  • 1
  • 45
  • 41
  • 1
    what do you want to set the default values to.. you can create a class or method that would set the values of ItemTaxes fields to anything you want using somthing like this `PropertyInfo[] properties = clsObject.GetType().GetProperties();` and then do a foreach loop so for example if the values default to null you could set them to string.Empty for example doing the follwoing – MethodMan Sep 08 '14 at 17:54
  • 1
    public static void ConvertNullToStringEmpty(this T clsObject) where T : class { PropertyInfo[] properties = clsObject.GetType().GetProperties();//typeof(T).GetProperties(); foreach (var info in properties) { // if a string and null, set to String.Empty if (info.PropertyType == typeof(string) && info.GetValue(clsObject, null) == null) { info.SetValue(clsObject, String.Empty, null); } } } – MethodMan Sep 08 '14 at 17:55
  • I don't think there is an attribute for this. Why don't you set the value in the constructor? – Postlagerkarte Sep 08 '14 at 17:59
  • Vielen Dank fuer beide @DJ KRAZE and Postlagerkarte, especially DJ KRAZE. I end up with this solution: private bool? _isTaxable; public bool IsTaxable { get { if (_isTaxable == null) { _isTaxable = true; } return _isTaxable; } set { _isTaxable = value; } } – macio.Jun Sep 08 '14 at 18:21

2 Answers2

11

A little late, but for those who got here looking for an answer:

public class ItemTaxes
{
    [NotNull, Default(value: true)]
    public bool IsTaxable { get; set; }
}
Gabriel Rainha
  • 1,713
  • 1
  • 21
  • 34
  • 10
    I am using PCL Blanck App(Xamarin.Forms Portable), i can't write [Default(value:true)] because it is not available in this solution. – Ashish-BeJovial Sep 02 '16 at 09:11
  • Which version SQLite.Net has this attribute? – Geograph Jan 11 '17 at 07:02
  • @Geograph It's been on [oysteinkrog/SQLite.Net-PCL](https://www.nuget.org/packages/SQLite.Net-PCL/3.1.1) since [version 3.0.1](https://github.com/oysteinkrog/SQLite.Net-PCL/commit/15f120cce60a2e1e5fce806f7e752fb1638a430a). – Gabriel Rainha Jan 11 '17 at 16:34
  • If I do this, my column gets set to the string value "True", since it simply uses the string representation of the default value (and yes, you can store strings in integer columns in SQLite). Seems rather pointless with this behavior. – Elte Hupkes Feb 02 '17 at 13:29
  • 1
    Doesn't work on this praeclarum's sqllite-net at https://github.com/praeclarum/sqlite-net – Scott R. Frost May 18 '18 at 20:22
6

If the Default attribute isn't available in the version of SQLite-net that you're using, you can use autoproperties to assign a default like you normally would.

public class ItemTaxes
{
    public bool IsTaxable { get; set; } = true;
}
Aurast
  • 3,189
  • 15
  • 24