实体框架复合关键问题

本文关键字:问题 复合 框架 实体 | 更新日期: 2023-09-27 18:09:33

当尝试在实体框架中使用复合键时,我遇到了这个问题

附加信息:属性表达式't => new<>f__AnonymousType0无效。表达式应该表示ac#: 't => t.MyProperty'Net: 'Function(t) t.MyProperty'。当指定多个属性时,使用匿名类型:c#: 't =>VB. new {t.MyProperty1, t.MyProperty2}函数(t) New With {t.MyProperty1, t.MyProperty2}'.

代码

 // Primary Key
 this.HasKey(t => new { t.ID, t.ID2, t.Version.Major, t.Version.Minor,  t.Version.Revision, t.Version.Build });

模型
  public int ID { get; set; }
  public short ID2 { get; set; }
  private VersionType m_Version;
  public VersionType Version
  {
     get
     {
        return m_Version;
     }
     set
     {
        SetProperty(ref m_Version, value, () => this.Version);
     }
  }

modelBuilder.ComplexType<VersionType>()

我认为这与在属性(Major等)之前声明Version类有关。

如果是这样,有什么办法可以解决这个问题吗?

欢呼

实体框架复合关键问题

我想这可能会帮助你从之前的问题大致相同。https://stackoverflow.com/a/5683788/78551查看他们如何定义模型和fluent api配置。