This thread looks to be a little on the old side and therefore may no longer be relevant. Please see if there is a newer thread on the subject and ensure you're using the most recent build of any software if your question regards a particular product.
This thread has been locked and is no longer accepting new posts, if you have a question regarding this topic please email us at support@mindscape.co.nz
|
Hello - I am using VS2010, .NET 3.5 and SQL Server 2008. Still playing, not production code yet. I am struggling with trying to use ThroughAssociations and the designer. I tried to do what is outlined in the blog by Ivan dated 17 September 2009, but the generated code seems to be very confused. I have 2 tables, TestResults and TestPrograms, with IDs called TestProgramID and TestResultID. I am using pluralized names, so the records are TestResult and TestProgram. I then have a link table called TestResultTestPrograms, with an ID called TestResultTestProgramID. Using the designer, I set up a Through Association between TestResult and TestProgram. The designer didn't automatically find the link table, so I first assigned it as the Through Entity, then converted it to an Auto Through Entity. The first time I tried this, the generated SQL was trying to add a record to the TestResultTestProgram table. Note, *not* pluralized. I double-checked the setting for "Pluralized in Database" and it was set to false, so I set it to True and tried again (several times, at different stages of design), to no avail. Grumbling, I decided to just change the name in the schema to match what the designer was looking for. That seemed to work, but then it died because it was trying to set the TestResultTestProgram.Id field -- not in the POCO code, but in the generated SQL! Of course that field does not exist... I can do what I need to do without using Through Entities, so that is the direction I will go until and unless you tell me this has been fixed. Great idea, but seemingly difficult to actually use it...
My other gripe -- I found the forum post where you added AllowEmptyString to the ValidatePresenceAttribute, so I changed the generated code (Model.cs) to include that attribute, and it works great! :) Until I change something, then it goes away. :( How do I make a change like that "sticky"? Can I assign that in a partial class in Extensions? If so, can you give me an example of the syntax?
Don't get me wrong, I love the product so far, its just the learning curve is steeper than I expected. Thanks for the assistance, Dave
|
|
|
Regarding through associations and auto through entities: Auto through entities are very basic. If you use an auto through entity, the only things you can control are the class name and the identity type. If you need to override the table name or identity column name, then you'll need an explicit through entity. It sounds like we should add an alert if converting to an auto through entity is going to lose information like a identity column mapping. It also sounds like you've been the victim of a designer-runtime disconnect. There are, unfortunately, two independent settings that control pluralisation in LightSpeed: * The designer "Pluralise in Database" option determines how the designer creates tables if you do an Update Database to apply your model changes to your database schema. * The LightSpeedContext.PluralizeTableNames property determines whether LightSpeed pluralises table names when it generates SQL for queries. These two settings are (rather confusingly we know) independent, though if you use the Get Started command then the designer will suggest using the 'right' setting for PluralizeTableNames. So if you set PluralizeTableNames to true in your code or configuration file, then the TestResultTestProgram class will get mapped to a TestResultTestPrograms table in the generated SQL even without an explicit Table Name setting. However, this won't be enough to let you get away with an auto through entity, because you also need to map the ID column name. So for your schema you will have to use an explicit through entity. I've logged a ticket to add some checks if converting to an auto through entity is safe, so hopefully future versions of the designer will help you avoid this situation. Regarding ValidatePresence.AllowEmptyString: The generated code is just that -- generated. It is a one-way street. If you change the generated code, your changes will just get overwritten when it is regenerated. So if you need to make changes you must either do so in the .lsmodel or do a 'convert to manual implementation' and make the changes in the partial class. Fortunately, 'specialised' validation options like ValidatePresence.AllowEmptyString are available through the designer, but they're not surfaced through the normal Entity Property editing options (basically because they'd have bogged down the Properties window too much). So instead you need to dive into the 'raw' model as follows: * Go into the LightSpeed Model Explorer (View > Other Windows > LightSpeed Model). * Expand the tree view through Entities > Entity Properties to find the Entity Property with the presence validation. * Expand that Entity Property node and open its Validations folder. Select the validation you want to tweak (in your case, the Presence Validation). * You can now edit any 'specialised' options associated with that validation through the Properties window. (In your case, the Properties window will display an Allow Empty String option which you can set to True.) See Help Topics > Modelling Tools > Validation in the Designer for more info about other validation options in the LightSpeed Model Explorer. |
|
|
OK, thanks for all the information. It's late, so I'll have to look at this again tomorrow. However, if I do decide to use an explicit through entity to accomplish this, I can see where saving everything may be fraught with peril, so to speak. I completely understand how to read the associated collection using the through entity, but I'm unclear how much LightSpeed will do to help me with saving the whole mess. Is there an example or sample that shows how best to do this?
Thanks, Dave
|
|
|
You work with through associations in exactly the same way regardless of whether you made the through entity explicit or auto in the designer. The through entity still exists: it's just a matter of whether it's shown on the design surface or not. So in either case, you can work with the through association just as if you were working with an entity collection. You don't need to work directly with the through entity, even if it was shown explicitly in the designer. I've just pushed up a blog post that shows some examples of working with a through association, and how it saves you working with the through entities directly: http://www.mindscape.co.nz/blog/index.php/2010/08/19/many-to-many-relationships-with-lightspeed-through-associations/ Hope this helps -- I'll try to post more on this over time and would welcome your feedback so we can fold the blog posts back into the core docs. |
|
|
Great blog post! It shows just how truly easy you guys make things... I was able to get my test scenario working in 30 seconds after reading your last 2 responses and the blog post cited here. Your support is phenomenal, keep up the great work! Dave Newman Geist Manufacturing Lincoln, NE USA |
|
|
[quote user="ivan"] Regarding ValidatePresence.AllowEmptyString: The generated code is just that -- generated. It is a one-way street. If you change the generated code, your changes will just get overwritten when it is regenerated. So if you need to make changes you must either do so in the .lsmodel or do a 'convert to manual implementation' and make the changes in the partial class. Fortunately, 'specialised' validation options like ValidatePresence.AllowEmptyString are available through the designer, but they're not surfaced through the normal Entity Property editing options (basically because they'd have bogged down the Properties window too much). So instead you need to dive into the 'raw' model as follows: * Go into the LightSpeed Model Explorer (View > Other Windows > LightSpeed Model). * Expand the tree view through Entities > Entity Properties to find the Entity Property with the presence validation. * Expand that Entity Property node and open its Validations folder. Select the validation you want to tweak (in your case, the Presence Validation). * You can now edit any 'specialised' options associated with that validation through the Properties window. (In your case, the Properties window will display an Allow Empty String option which you can set to True.) See Help Topics > Modelling Tools > Validation in the Designer for more info about other validation options in the LightSpeed Model Explorer. [/quote] This is great stuff, exactly what I needed! I knew there had to be a better way, but this is easier than I thought it would be. My only question is, how could you make this information more easily available? I shouldn't have to get on the support forum to find little jewels like this... Thanks again, Dave |
|