简体   繁体   English

SQL CE 4和EF 6代码优先不支持缺省值

[英]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. DmlSqlGenerator.GenerateInsertSql抛出此异常DmlSqlGenerator.GenerateInsertSql传播。

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? 这是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? 我怎样才能绕过它并拥有一个包含两组依赖项的主项,每个项都有标量值?

This is known issue with CE and EF but here is a link to the MSDN forums that describes the issue and the solution. 这是CE和EF的已知问题,但这里是MSDN论坛的链接 ,描述了问题和解决方案。

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. 添加标量列可以消除异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM