petapoco插入问题

发布于 2024-11-07 12:49:08 字数 1027 浏览 4 评论 0原文

我有一个类定义如下:

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 无法执行插入操作,但是......必须有一种方法告诉它“忽略”这一点,对吗?

I have a class defined like so:

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

}

And the database table for this is just “locations” with an ID and a Name property.

Some other table “meetings” has a foreign key back to this table. And it is beyond the scope of what I’m trying to work with in this example, yet I think it is causing PetaPoco to fail…

I’m trying to use PetaPoco to insert a new location into the database like this:

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

And it is throwing an error like so:

{"No mapping exists from object type
System.Collections.Generic.List`1[[NHRepoTemplate.sampleUsage.sampleModel.Meeting,
NHRepoTemplate, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=null]]
to a known managed provider native
type."}

It seems to me like the existence of the child collection causes PetaPoco to not be able to do the insert, but... there must be a way to tell it to "ignore" that, right?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

与风相奔跑 2024-11-14 12:49:08

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

[PetaPoco.Ignore]

Try putting this over your Meetings property:

[PetaPoco.Ignore]
总以为 2024-11-14 12:49:08

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

if you use [ExplicitColumns] attribute above your petapoco class, all properties that do not have the [Column] attribute will be ignored

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文