1

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?

4
  • 1
    this might be helpfull social.msdn.microsoft.com/Forums/en-US/… Commented Feb 16, 2015 at 8:30
  • Can you please turn your comment into an answer? Commented Feb 16, 2015 at 9:52
  • I have done that. Before accepting it you can update the answer what exactly you needed to solve the issue so instead of link the actual answer might help in future. Commented Feb 16, 2015 at 9:58
  • 1
    Hope the edit is okay, I've expanded on what the solution is in the post itself. Thanks, your answer was an immense help! Commented Feb 16, 2015 at 10:05

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

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.