1

I declare following class in c#

[Table("Employee")]
public class Employee
{
    [PrimaryKey,AutoIncrement]
    public int EmployeeId { get; set; } 
    public DateTime DateOfJoining { get; set; }
    public string Address{ get; set; }
}

and i invoke this method to create equivalent table in my SQLite database

public async Task CreateTable()
{
   SQLiteAsyncConnection conn = new SQLiteAsyncConnection(path);
   await conn.CreateTableAsync<Employee>();
}

So it creates a table in SQLite as follows

[EmployeeId] int,
[DateOfJoining] [datetime],
[CallType] [varchar]

I wanted To create a a column, which is bit

[IsActive] [bit]

For this I tried

public bool IsActive { get; set; } 

and

public Boolean IsActive { get; set; }

Both these properties result in a column which is an integer

[IsActive] integer

So how should I declare my IsActive property to get a column with bit as datatype.

I have one more question, If i declare property and specify it as not null

[NotNull]
public bool Address{ get; set; }

Then it gives me an error when I invoke CreateTable() saying, "No default value specified for Not Null attribute".

I tried to initialise this property in constructor, but it didnt work.

How do I go about these issues? Please Help

1 Answer 1

2

As far I see in SQLite Docs there is no bit datatype for SQLite

and in SQLite.Net all kind of byte/boolean/ints get mapped to integer: see this line

for the NotNull Error, let me guess:

  • You have already some entries in your Table
  • You now add a new Column with NotNull-Attribute
  • SQLite tries to alter the tablet and it crashes, because multiple entries where the new column has now null values

I think this is the only situation where to have to alter table in this order:

  • Add Column without NotNull
  • Add some column values
  • only once all table entries has the value for this Column --> now you can add the NotNull Attribute

If your Table is empty then the NotNull Parameter would work right from the start.


Edit easier Solution:

  • Add Column without NotNull
ALTER TABLE Employee ADD COLUMN IsActive integer default 0; 
  • now you can add the NotNull Attribute and call CreateTable again
[NotNull]
public bool Address{ get; set; }

await conn.CreateTableAsync<Employee>();
Sign up to request clarification or add additional context in comments.

6 Comments

So the finl answer is "No, thats not possible". Hats off to your research on this. And +1 for the explaination about not null. Thank you.
With reference to your last point , How can i add NotNull Attribute after I fill values in all columns??? In short, how will I add NotNull attribute to a property, programatically?
So you already added the column and filled the values? well then it is quite easy you could just add the NotNull Attribute to your Column and call CreateTableAsync again, this time it should work without an error
But how to add NotNull attribute programatically to an already existing property??? Something like "IsActive .Attributes.Add('NotNull')"??
Or you want me to have another similar class with NotNull attribute set to IsActive property, & call the CreateTable() method for this new class??
|

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.