Search code examples
sqlitexamarincheck-constraintssqlite-netcheck-constraint

sqlite.net code first: add check constraint


Given an sqlite database, and a code first approach, how can I add a check constraint to a table? I can't use sql, so is there an attribute or so I can use, like [PrimaryKey]?

I'm hoping for something like

public class XXX
{
[PrimaryKey, Check(()=>Id==1)]
public int Id {get;set;}
}

In fact, what I really want, is to constrain the table so that it can contain at most one row. If there's any other way do achieve this, I'd be happy too.

Is there any way to do this?

Lots of Greetings!

Volker


Solution

  • No you can't do this without using sql.

    If you want the table to only contain one row you could add a read only property with a unique constraint and a constant value - when you save a second record it would fail because of the unique constraint.