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.

4
  • 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 Commented Sep 8, 2014 at 17:54
  • 1
    public static void ConvertNullToStringEmpty<T>(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); } } } Commented Sep 8, 2014 at 17:55
  • I don't think there is an attribute for this. Why don't you set the value in the constructor? Commented Sep 8, 2014 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; } } Commented Sep 8, 2014 at 18:21

2 Answers 2

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; }
}
Sign up to request clarification or add additional context in comments.

5 Comments

I am using PCL Blanck App(Xamarin.Forms Portable), i can't write [Default(value:true)] because it is not available in this solution.
Which version SQLite.Net has this attribute?
@Geograph It's been on oysteinkrog/SQLite.Net-PCL since version 3.0.1.
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.
Doesn't work on this praeclarum's sqllite-net at github.com/praeclarum/sqlite-net
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;
}

1 Comment

doesnt work for already existing records

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.