views:

393

answers:

2

Hi, 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: http://stackoverflow.com/questions/1378292/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

+1  A: 

What you need to be aware of here is that the relationships are created by populating foreign keyvalues. So what you're doing in your example is creating a permit and saving it then setting the ProductID of the permit (but not saving this information), then saving the product. If you reorder your code as follows the ProductID will be set correctly:

 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);
Adam
+1. We're just beginning to evaluate whether we want to use SubSonic. Once of my co-workers asked how it handles related tables, joins, etc. Your A J's question, and Adam's answer tell me that it works, at least.
David Stratton
I suspect permit.Product = product.Id; should actually be permit.ProductId = product.Id;
Tom Bushell
A: 

Hi Adam, thanks for replying but it doesn't work.

When the table for Permit is created, there is no 'field' at all created to link to the Product. e.g. in the example above for the definition for Permit, where I have got a field

Product product

there is no equivalent which is created in the Permit table. Am I missing something here? I am using Subsonic 3.0.0.3

To my knowledge, this feature isn't in Subsonic yet, but it's planned to add it in. In the meantime, the only other ORM tool I found that can do top-down with automatic database schema generation is Fluent NHibernate.
Daniel T.
Sorry I wasn't clear in my original answer and should have pointed out that you need to specify the ProductId, not the product. I've edited my answer to reflect this.
Adam