One to Many Parent, Child Relationship - coding basics

One to Many Parent, Child Relationship - coding basics

Old forum URL: forums.lhotka.net/forums/t/8125.aspx


FCazabon posted on Thursday, December 10, 2009

Hi,

I am trying to learn CSLA.NET by creating a simple application and adapting what I have learned from the Expert C# 2008 Business Objects book. I am not understanding how to create the Data Access side of an object, so I hope someone can explain it to me.

My application is very simple Membership database. A Member joins the club and her membership status can change as time passes. I need to maintain the history of this membership. The different types of membership are user defined. So, my Database is a very simple three tables like this:

1. Member (which has Id, Name and personal details)
2. Membership (which has a MemberId, MembershipTypeId and Date)
3. MembershipType (which has Id and Name)

The classes I have come up with for the design are:

public class Member : BusinessBase

public class MembershipHistory : BusinessListBase

public class Membership : BusinessBase

public class MembershipTypeList : NameValueListBase

public class MembershipType : BusinessBase

so far.

I have created a Members.DalLinq project and created a Members.dbml in there by dragging in the tables and stored procedures from my database.

Now, when creating the Data Access code for Member I load the data from my Members table like this:

var data = (from m in ctx.DataContext.Members

where m.Id == criteria.Value
select m).Single();


My problem arises when I try to load the "child" data using this:

                //Get child data

LoadProperty(
MembershipProperty,
MembershipHistory.GetMembershipHistory(
data.MembershipHistory.ToArray()));


I get this error:

'Members.DalLinq.Member' does not contain a definition for 'MembershipHistory' and no extension method 'MembershipHistory' accepting a first argument of type 'Members.DalLinq.Member' could be found (are you missing a using directive or an assembly reference?)

It is my understanding that this method that it is looking for should reside in the Members.designer.cs file in the Members.dbml section of my solution and it should have been created automatically by VS. So since it wasn't done, I guess I have set something up incorrectly.

If anyone has read this far and has any clue what I am talking about, I would really appreciate your suggestions.

Thanks very much for your time and patience!

ajj3085 replied on Thursday, December 10, 2009

sounds like the linq2sql is messed up somewhere.  Did intellisense come up with MembershipHistory when you typed data. ?

FCazabon replied on Thursday, December 10, 2009

Nope, no intellisense. I'm wondering if maybe I should be calling FetchChild() instead. ??

FCazabon replied on Friday, December 11, 2009

OK, got some intellisense kicking in now which allows me to set it up like this:

                //Get child data

LoadProperty(
MembershipProperty,
MembershipHistory.GetMembershipHistory(
data.Memberships.ToArray()));


but this now gives me two errors:

Error 1 The best overloaded method match for 'Members.Library.MembershipHistory.GetMembershipHistory(Members.DalLinq.MembershipHistory[])' has some invalid arguments

Error 2 Argument '1': cannot convert from 'Members.DalLinq.Membership[]' to 'Members.DalLinq.MembershipHistory[]'

I seem to have fixed these errors by changing code in my MembershipHistory.cs file from this:

        internal static MembershipHistory GetMembershipHistory(

Members.DalLinq.MembershipHistory[] data)
{
return DataPortal.FetchChild(data);
}


to this:

        internal static MembershipHistory GetMembershipHistory(

Members.DalLinq.Membership[] data)
{
return DataPortal.FetchChild(data);
}


Does that make sense?

ajj3085 replied on Friday, December 11, 2009

It makes sense, if you really want to load memberships instead of membershiphistory.  I have no idea what your domain model is, so I can't tell you if that's correct or not though.  But if you're getting the expected results, then I'd say you're on the right track.  Smile [:)]

FCazabon replied on Friday, December 11, 2009

I *think* memberships and membershiphistory are conceptually the same thing, just I was thinking of it as MembershipHistory, but the SQL to Link thing seems to have called it Memberships. I can't say if it is correct yet as I still need to do bit more coding to get all the other classes set up.

Thanks for your help

ajj3085 replied on Friday, December 11, 2009

Well I'd double check the Linq designer then, to see what data is being pulled out.

Copyright (c) Marimer LLC