Default values not supported in SQL CE 4 and EF 6 code-first

I have the following code-first model:

  public class TestContext : DbContext
  {
    public TestContext() : base("Data Source=Test.db") { }
    public DbSet<Item> Items { get { return this.Set<Item>(); } }
  }

  public class Item
  {
    public Int32 Id { get; set; }

    public virtual ICollection<SubItem1> SubItems1 { get; set; }
    public virtual ICollection<SubItem2> SubItems2 { get; set; }
  }
  public class SubItem1
  {
    public Int32 Id { get; set; }
    public Int32 ItemId { get; set; }
    public Item Item { get; set; }
    public String Test { get; set; }
  }
  public class SubItem2
  {
    public Int32 Id { get; set; }
    public Int32 ItemId { get; set; }
    public Item Item { get; set; }
    public Int32 TestCode { get; set; }
  }

When used like this:

  using (var context = new TestContext())
  {
    context.Items.Add(new Item());
    context.SaveChanges();
  }

I get an an exception which says "Default values not supported". This exception is thrown from DmlSqlGenerator.GenerateInsertSql and propagated up.

Originally I got the exception with much more complex schema, but I was able to boil it down to this. Is this a limitation of SQL CE? How can I get around it and have a principal item with two sets of dependent items each of which have scalar values?

Upvotes: 1

Views: 649

Answers (1)

Mahesh
Mahesh

Reputation: 8892

This is known issue with CE and EF but here is a link to the MSDN forums that describes the issue and the solution.

The gist of it is not to have an entity with only keys, be it a sole primary key or primary key and foreign keys. Adding a scalar column gets rid of the exception.

Upvotes: 3

Related Questions