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
|
Hi, I structured my database (MySQL) for reading with keytable. Now, I've a table with a composite key and I have to insert/update, but when assign a value to a key field, I can not because is readonly. using (var uow = Program.Context.CreateUnitOfWork()) I read for GeneratedId() method, but I don't now if is my solution and how to use. How can I assign values to key fields? |
|
|
I don't understand even if my tables must have ID column anyway. If yes, how be set the column? Must be part of primary key? |
|
|
But above all, we need a primary key with the Keytable configuration? I'm desperate .... :) |
|
|
For non-composite key scenarios: Your entity tables must have an Id column, which should be the primary key. LightSpeed will assign the value of this column based on your IdentityMethod. If your IdentityMethod is KeyTable, then LightSpeed will get values from a table named KeyTable, and use these to populate the Id. If your IdentityMethod is Guid, then LightSpeed will create a random GUID for each entity and use that to populate the Id. If your IdentityMethod is IdentityColumn, LightSpeed will let the database generate an Id (in which case the Id column must be an identity/autoincrement column) and assign that generated Id. If you override GeneratedId() then this takes priority over the declared IdentityMethod. This is not normally required or advisable unless you have a legacy database with natural keys. Your KeyTable table does NOT need a primary key. It just contains a single scalar value (one column, one row). For composite key scenarios: You must declare a value type to contain your key fields, and use this as your identity type. You will need to override GeneratedId() because there is no IdentityMethod that knows how to generate composite keys. See http://www.mindscape.co.nz/blog/index.php/2009/12/22/composite-keys-in-lightspeed-3/ for a detailed discussion of this scenario. |
|
|
Oops, sorry, I missed the initial post. You are obviously talking about a composite key scenario with the designer-generated key structure. Yes, you need to override GeneratedId(). See this article: http://www.mindscape.co.nz/blog/index.php/2009/12/22/composite-keys-in-lightspeed-3/ and scroll down to the section titled "Assigning composite IDs." Again, apologies for misunderstanding in my previous post. |
|
|
Hi Ivan, thanks for the quick, detailed and precise response. I solved the problem perfectly, but was born a new problem.
using (var uow = Program.Context.CreateUnitOfWork()) gridView1.Columns["Sscordadtmov"].Caption = "Data movimento"; //Sscordadtmov is a key field I think it's normal, because it not recognizes the key fields, but how do I fix this problem? |
|
|
My guess would be that this is because Sscordadtmov is not an attribute of the entity class, so VS hasn't autogenerated a column for it. If Sscordadtmov is a PK field, then it's a property of the XxxId structure. I believe the solution to this would be to manually map these columns (i.e. column Sscordatatmov <---> property path Id.Sscordadtmov), but I'm not sufficiently familiar with the WinForms DataGridView to know how to do that in Windows Forms (in WPF you would just specify "Id.Sscordadtmov" as the property path in the binding, but I don't know if that works in WinForms). If you absolutely can't get WinForms to accept a multipart path, you could work around this by creating properties in the partial class that surface the PK fields directly on the entity, e.g. partial class Sottoscortum { Then you can bind your columns to these properties and everything should work fine. |
|
|
When I assign datasource with uow.Find<Sottoscortum>(_query), I have this scenario: - If table have no data (Sottoscortum is a new empty table), my grid is populated with only non-PK fields - If table have data (populated by hand), I have an error in structure format query. [quote user="ivan"] If you absolutely can't get WinForms to accept a multipart path, you could work around this by creating properties in the partial class that surface the PK fields directly on the entity, e.g. partial class Sottoscortum { Then you can bind your columns to these properties and everything should work fine. [/quote] I'm sorry Ivan, but I don't understand. Can you give me an example? |
|
|
Sure. You want to show the PK fields as separate columns. The DataGridView therefore wants the PK fields to be separate properties of the entity class. But LightSpeed represents them as subproperties of a single property (the Id). So the solution is to create properties on the entity class, as the DataGridView wants: partial class MyEntity { Now the DataGridView can create columns that bind to these properties. But we need to show the values of the PK fields in these columns. So how can we implement the properties so that they return the values of the PK fields? Easy, we just access the properties of the Id itself: public int MyFirstKeyField { |
|