Single Row Table in Entity Framework 6

c# entity-framework entity-framework-6 sql

Question

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?

1
6
7/8/2015 8:09:07 PM

Popular Answer

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...");
            }
        }
1
5/23/2017 12:22:47 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow