【发布时间】: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 属性,您可以使用它来引用回这个位置