【问题标题】:petapoco insert problempetapoco 插入问题
【发布时间】:2011-08-27 14:05:44
【问题描述】:

我有一个这样定义的类:

public class Location
{
    public Location()
    {
        Meetings = new List<Meeting>();
    }

    public virtual int ID { get; private set; }
    public virtual string Name { get; set; }

    public virtual ICollection<Meeting> Meetings { get; set; }

}

为此,数据库表只是带有 ID 和 Name 属性的“位置”。

其他一些“会议”表有一个指向该表的外键。这超出了我在本示例中尝试使用的范围,但我认为它导致 PetaPoco 失败……

我正在尝试使用 PetaPoco 将一个新位置插入到数据库中,如下所示:

    public int AddLocation(string name)
    {
        var newLocation = new Location{Name = name};
        var db = new PetaPoco.Database(_connectionString);
        db.Insert("locations", "ID", newLocation);
        return newLocation.ID;
    }

它会抛出这样的错误:

{"对象类型不存在映射 System.Collections.Generic.List`1[[NHRepoTemplate.sampleUsage.sampleModel.Meeting, NHRepoTemplate,版本=1.0.0.0, 文化=中立,PublicKeyToken=null]] 到已知的托管提供商本地 类型。”}

在我看来,子集合的存在导致 PetaPoco 无法进行插入,但是......必须有一种方法告诉它“忽略”它,对吧?

【问题讨论】:

  • 添加 Jon 提到的属性是正确的。然后,Meeting 类应该有一个 LocationId 属性,您可以使用它来引用回这个位置

标签: orm petapoco


【解决方案1】:

尝试将其放在您的 Meetings 属性上:

[PetaPoco.Ignore]

【讨论】:

    【解决方案2】:

    如果您在 petapoco 类上方使用 [ExplicitColumns] 属性,则所有不具有 [Column] 属性的属性都将被忽略

    【讨论】:

      猜你喜欢
      • 2012-06-13
      • 2012-01-31
      • 2011-09-29
      • 2023-03-24
      • 1970-01-01
      • 2013-12-20
      • 1970-01-01
      相关资源
      最近更新 更多