Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Single Row Table in Entity Framework 6

I couldn't find a solution for this problem but the title makes clear what I want.

Is it possible to create a single row table (I only need to store a boolean value in a table)? And how can I configure this constraint with Fluent API?

like image 354
rmnblm Avatar asked Jul 08 '15 20:07

rmnblm


1 Answers

you could make one of the columns as primary and also allow only one value. unfortunately fluent api currently doenst support default value

public class StatusIdentifer
{
  [DefaultValue("1")]
  [Key]
  public int id {get; set}; //set can be removed here?
  public bool status {get:set;} //your boolean value
}

the trick is not to expose any set methods for id.

at database level you can still break the paradigm. The answer here tells how to create a check constraint

public void InitializeDatabase(MyRepository context) {
            if (!context.Database.Exists() || !context.Database.ModelMatchesDatabase()) {
                context.Database.DeleteIfExists();
                context.Database.Create();

                context.ObjectContext.ExecuteStoreCommand("CREATE UNIQUE CONSTRAINT...");
                context.ObjectContext.ExecuteStoreCommand("CREATE INDEX...");
                context.ObjectContext.ExecuteStoreCommand("ETC...");
            }
        }
like image 111
Krishna Avatar answered Nov 11 '22 08:11

Krishna