Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5979637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:41:15+00:00 2026-05-22T21:41:15+00:00

I have the following POCO’s: [PetaPoco.TableName(Customer)] [PetaPoco.PrimaryKey(ID)] public class User { [Required(ErrorMessage = Please

  • 0

I have the following POCO’s:

[PetaPoco.TableName("Customer")]
    [PetaPoco.PrimaryKey("ID")]
    public class User
    {
        [Required(ErrorMessage = "Please enter a Forename")]
        [PetaPoco.Column("Name")]
        public string Forename { get; set; }

    }

    public class Product
    {
        public int Id { get { return 1; } }
        public string Name { get { return "TBMaster Licence"; } }
        public decimal Price { get { return 358.00M; } }  //Ex VAT

        [Required(ErrorMessage = "Please enter a valid Machine ID")]
        public string MachineId { get; set; }

        public int ExpiryElement { get; set; }

        public DateTime ExpiryDate
        {
            get
            {
                if (ExpiryElement == 0)
                {
                    return new DateTime(1900, 1, 1);
                }
                else if (ExpiryElement == 1)
                {
                    return DateTime.Now.AddYears(1);
                }
                else if (ExpiryElement == 2)
                {
                    return DateTime.Now.AddYears(2);
                }
                else if (ExpiryElement == 3)
                {
                    return DateTime.MaxValue;
                }
                else
                {
                    return new DateTime(1900, 1, 1);
                }
            }
        }

        public bool BloodTestEnabled { get; set; }
        public string LicenceKey { get; set; }

        public SelectList LicenceOptions
        {
            get;
            set;
        }
    }

    [PetaPoco.TableName("Order")]
    [PetaPoco.PrimaryKey("OrderID")]
    public class Order
    {
        [PetaPoco.Column("TxCode")]
        public string VendorTxCode { get; set; }

        public User WebsiteUser { get; set; }

        public List<Product> Products { get; set; }
    }

I have the below code that tries to do an insert in the respective tables:

        var user = new User();
        user.Forename = "fred";

        var product = new Product();
        product.ExpiryElement = 2;
        product.MachineId = "1234";

        var order = new Order();
        order.VendorTxCode = "1111";
        order.WebsiteUser = user;
        List<Product> prods = new List<Product>();
        prods.Add(product);
        order.Products = prods;


        var db = new PetaPoco.Database("TBMasterDB");

        db.Insert(user);
        db.Insert(order);

The insert on the user works fine but the insert on the order doesn’t. How do you do an insert when you have this relationship in your POCO’s? This is a simplified approach because where I am trying to do the inserts I only have the Order POCO so how can I insert into the user, order and products table?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T21:41:16+00:00Added an answer on May 22, 2026 at 9:41 pm

    After looking at Gareth Elms’ blog post I see I can do it like so:

     db.Insert("Order", "OrderID", new { TxCode = order.VendorTxCode, CustomerID = order.WebsiteUser.ID });
    

    Essentially you mapping again in the Insert method. Not very pretty but it does the trick. Hope there is a tidier way of doing this in the future

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following POCO: Public Class T1 <Required()> <MaxLength(128)> <Key(), Column(Order:=0)> Property K1
I have the following Entity Framework POCO classes: public class Customer { public int
I have the following enum and POCO class public enum Gender { Male, Female,
I have a following POCO class public class Account { [Key,DatabaseGenerated(DatabaseGenerationOption.Identity)] public string AccountId
Say I have the following POCO classes: public class AuditableModel { public int ID
Say I have the following POCO classes: public class Parent { public int ID
I have the following poco class: public class Category : IDisplayName { private ICollection<Category>
I have following classes. class A { public: void fun(); } class B: public
Given the following POCO classes: public class Certification { public int Id { get;
I have the following class: public class Foo { public int Id { get;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.