4

I am trying to use the SimpleRepository feature in Subsonic3 - first of all, I must say a big thanks to RobC - Subsonic really rocks, and I can't wait to see additional updates to the SimpleRepository. I am a big fan of the migration approach (developer/class driven rather than starting with the DB).

I have had a look at the post here: Parent and Child object in SimpleRepository but I am still a bit confused.

If I have got these classes defined:

public class Permit {

        public int PermitID {get; set;}
        public string Number { get; set; }
        public DateTime? DateIssued { get; set; }
        public Product product { get; set; }
    }

    public class Product
    {
        public int ProductID { get; set; }
        public string Value { get; set; }

    }

and then I want to save the data for a Permit, what should I be doing? Should I have defined a ProductID in the Permit class, and then programatically link them up? Or should the below code work?

 var repo = new SimpleRepository("ECPermit", SimpleRepositoryOptions.RunMigrations);
            var permit = new Permit();
            var product = new Product();

            permit.Number = "apermit";
            permit.DateAdded = DateTime.Now;

            product.Value = "this is a product";
            repo.Add(permit);
            permit.product = product;
            repo.Add(product);

This is creating the Permit and Product table, but no links between them. What am I doing wrong? Thanks

4

1 回答 1

1

您需要注意的是,关系是通过填充外键值创建的。因此,您在示例中所做的是创建许可证并保存它,然后设置许可证的 ProductID(但不保存此信息),然后保存产品。如果您按以下方式重新排序代码,则将正确设置 ProductID:

 var repo = new SimpleRepository("ECPermit", SimpleRepositoryOptions.RunMigrations);
 var permit = new Permit();
 var product = new Product();

 product.Value = "this is a product";
 repo.Add(product);

 permit.Number = "apermit";
 permit.DateAdded = DateTime.Now;    
 permit.ProductId = product.Id;
 repo.Add(permit);
于 2009-09-17T11:18:11.690 回答