gpt4 book ai didi

c# - SQL CE 4 和 EF 6 代码优先不支持默认值

转载 作者:太空宇宙 更新时间:2023-11-03 23:42:33 24 4
gpt4 key购买 nike

我有以下代码优先模型:

  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; }
}

像这样使用时:

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

我收到一个异常,显示“不支持默认值”。此异常从 DmlSqlGenerator.GenerateInsertSql 中抛出并向上传播。

最初,我得到了具有更复杂架构的异常,但我能够将其归结为这一点。这是 SQL CE 的限制吗?我怎样才能绕过它并拥有一个主项和两组从属项,每个从属项都有标量值?

最佳答案

这是 CE 和 EF 的已知问题,但 here is a link to the MSDN forums描述问题和解决方案。

它的要点不是只有键的实体,无论是唯一的主键还是主键和外键。添加标量列可以消除异常。

关于c# - SQL CE 4 和 EF 6 代码优先不支持默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28536110/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com