Petapoco插入问题

5

我有一个如下所示的类定义:

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

}

这个与 IT 技术有关的翻译内容是:“这个数据库表只有一个名为“locations”的 ID 和 Name 属性。另外一个名为“meetings”的表将其作为外键,但这超出了我正在尝试解决的范围,我认为这可能导致 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,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null]]无法映射到已知的托管提供程序本机类型。"}

我认为子集合的存在导致PetaPoco无法进行插入操作,但是...一定有办法告诉它“忽略”这个问题,对吗?


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

6

尝试将以下内容添加到您的 Meetings 属性中:

[PetaPoco.Ignore]

0
如果你在petapoco类的上方使用[ExplicitColumns]属性,所有没有[Column]属性的属性都将被忽略。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接