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
|
I Made the following simple schema http://screencast.com/t/MjhhNTE3O where entities tasks, files, notes, users and projects inherit from Registry table.
I made the diagram and set CTI inheritance for each of the class generated: http://screencast.com/t/NmY3MjMxN http://screencast.com/t/NmQ3ZmIzY2Y
this is my code:
class Program { private static LightSpeedContext<MyDBUnitOfWork> _context; static void Main(string[] args) { _context = new LightSpeedContext<MyDBUnitOfWork>("default"); using (var uow = _context.CreateUnitOfWork()) { Consumer consumer = new Consumer(); consumer.Name = "First Consumer"; consumer.Created = DateTime.Now; uow.Add(consumer); uow.SaveChanges(); } } }
which in runtime gives me the following Unhandled Exception: Mindscape.LightSpeed.LightSpeedException: Descendent type [ Calion_DB_Bakery.File] of type [MY_DB_Bakery.Registry] is not discriminated at Mindscape.LightSpeed.Model.TypeModel.⌂(Type , ICollection`1 , TypeModel , DiscriminatorAttribute[] ) at Mindscape.LightSpeed.Model.TypeModel.⌂(Type , IList`1 ) at Mindscape.LightSpeed.Model.TypeModel.⌂(IList`1 ) at Mindscape.LightSpeed.Model.TypeModel.⌂(Type , IList`1 ) at Mindscape.LightSpeed.Model.TypeModel.GetTypeModel(Type ) at Mindscape.LightSpeed.Entity.Initialize() at Mindscape.LightSpeed.Entity..ctor(Boolean autoInitialize) at Mindscape.LightSpeed.Entity`1..ctor()
at My_DB_Bakery.Consumer..ctor() in C:\Users\Dmitry\documents\visual stud
io 2010\Projects\My DB Bakery\My DB Bakery\LightSpeedModel1.cs:line 36
at My_DB_Bakery.Program.Main(String[] args) in C:\Users\Dmitry\documents\
visual studio 2010\Projects\Calion DB Bakery\My DB Bakery\Program.cs:line 20
Any ideas why is that or should I tune the lightspeed.cs file with classes generated somehow? I tried to find the answer reading the web & forum but no luck so far.
|
|
|
Okay I partially solved it, I had to manually add Discriminator attribute, however that was from STI pattern not from CTI, so I assume it's a bug. Please correct me if not. the code below doesn't produce the error above, but now gives me another error (see below)
[Serializable] [System.CodeDom.Compiler.GeneratedCode("LightSpeedModelGenerator", "1.0.0.0")] [System.ComponentModel.DataObject] [Table("Files", IdColumnName="fileID")] [Discriminator(Attribute = "typeID", Value = "F")] public partial class File : Registry { #region Fields
[ValidatePresence] [ValidateLength(0, 255)] private string _title; [ValidateLength(0, 255)] private string _description; [ValidatePresence] [ValidateLength(0, 255)] private string _path; private System.Nullable<float> _size; [Column("RelatedTo")] private int _relatedToId;
#endregion
new error in run time:
Unhandled Exception: Mindscape.LightSpeed.LightSpeedException: The field [Relate dToId] on type [File] must be nullable as it represents a hierachical relation at Mindscape.LightSpeed.Model.ToManyModel.⌂() at Mindscape.LightSpeed.Model.AssociationModel.⌂(TypeModel ) at Mindscape.LightSpeed.Model.TypeModel.⌂(IEnumerable`1 ) at Mindscape.LightSpeed.Model.TypeModel.GetTypeModel(Type ) at Mindscape.LightSpeed.Entity.Initialize() at Mindscape.LightSpeed.Entity..ctor(Boolean autoInitialize) at Mindscape.LightSpeed.Entity`1..ctor() at My_DB_Bakery.Consumer..ctor() in C:\Users\Dmitry\documents\visual stud io 2010\Projects\My DB Bakery\My DB Bakery\LightSpeedModel1.cs:line 36 at My_DB_Bakery.Program.Main(String[] args) in C:\Users\Dmitry\documents\ visual studio 2010\Projects\My DB Bakery\My DB Bakery\Program.cs:line 20
Press any key to continue . . .
however the fields itself on my model is NOT NULL: mysql> show columns from files; +-------------+------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------------+------------------+------+-----+---------+-------+ | fileID | int(10) unsigned | NO | PRI | NULL | | | title | varchar(255) | NO | | NULL | | | description | varchar(255) | YES | | NULL | | | path | varchar(255) | NO | | NULL | | | size | float | YES | | NULL | | | relatedTo | int(10) unsigned | NO | MUL | NULL | | +-------------+------------------+------+-----+---------+-------+ 6 rows in set (0.00 sec)
the logic is this simple:
each file can be associated with at least one Element of Registry type, it can't exist without the parent Registry element. So that's why it's NOT NULL. I can add the file to project, task etc. what should I do now to run a simple test? |
|
|
found another bug, everytime I modify in designer something my custom attrubute I added for all inherited classes/tables got removed [Discriminator(Attribute = "typeID", Value = "F")] so I tried to make relationship between tables nullable and got all my custom attributes removed please help! |
|
|
The problem is that concrete table inheritance is not polymorphic. If you want to have associations to a base class (in your case Registry with its RegistryAssignments association), then the hierarchy from that base class has to be polymorphic -- i.e. it must use Single Table Inheritance or Class Table Inheritance. http://www.mindscape.co.nz/blog/index.php/2008/11/03/inheritance-in-lightspeed/ discusses this in a bit more detail. I thought we'd added a warning to the designer to flag this issue up so that it would give you a more meaningful error, but it looks like that must still be on the backlog. I'll see if we can get this improved in future nightly builds. |
|
|
How they can use STI pattern if they reside in separate tables? is it possible still for me to use STI and define discriminator for each? Which can be typeID field of the Registry table, will that work? |
|
|
You cannot have completely separate tables because the RegistryAssociation table is going to contain a RegistryId column which is a foreign key. Now if Project, File and Note were each completely self-contained tables (which is what Concrete Table Inheritance specifies), which table would RegistryId be a foreign key to? Therefore to have associations there has to be a Registry table. Now you can either put the Project, File and Note data into the Registry table (which is the Single Table Inheritance mapping), or you can have Project, File and Note tables in addition to the Registry table, with the Registry table containing the Created, Updated, TypeId etc. columns, the Project table containing the Title, Description, etc. columns, and so on. This second approach is known in LightSpeed as Class Table Inheritance, and it sounds like that may be what you're after. Class Table Inheritance is discriminated and polymorphic so it works with associations. However, it is less efficient than Single Table Inheritance because it requires a lot of joins across tables. See http://www.mindscape.co.nz/blog/index.php/2010/04/27/class-table-inheritance-in-lightspeed/ for more info. Yes, the discriminator can be the TypeId field of the Registry table. You will see the discriminator option setting in the designer when you change the inheritance mode to Single or Class Table Inheritance. Note you must specify the discriminator in the designer (or in a partial class), not by editing the generated code, because the generated code is owned by the designer and is overwritten whenever you change the model. |
|
|
[quote user="ivan"]You cannot have completely separate tables because the RegistryAssociation table is going to contain a RegistryId column which is a foreign key. Now if Project, File and Note were each completely self-contained tables (which is what Concrete Table Inheritance specifies), which table would RegistryId be a foreign key to?[/quote] [quote user="ivan"]Therefore to have associations there has to be a Registry table. Now you can either put the Project, File and Note data into the Registry table (which is the Single Table Inheritance mapping), or you can have Project, File and Note tables in addition to the Registry table, with the Registry table containing the Created, Updated, TypeId etc. columns, the Project table containing the Title, Description, etc. columns, and so on. This second approach is known in LightSpeed as Class Table Inheritance, and it sounds like that may be what you're after.[/quote] [quote user="ivan"]Class Table Inheritance is discriminated and polymorphic so it works with associations. However, it is less efficient than Single Table Inheritance because it requires a lot of joins across tables. See http://www.mindscape.co.nz/blog/index.php/2010/04/27/class-table-inheritance-in-lightspeed/ for more info.[/quote]
Thanks for your prompt reply |
|
|
after changing to class inhertiance pattern got this error
Unhandled Exception: Mindscape.LightSpeed.LightSpeedException: Could not determine the reverse association of [Note.RelatedTo (Registry)] at Mindscape.LightSpeed.Model.AssociationModel.⌂(TypeModel ) at Mindscape.LightSpeed.Model.TypeModel.⌂(IEnumerable`1 ) at Mindscape.LightSpeed.Model.TypeModel.GetTypeModel(Type ) at Mindscape.LightSpeed.Entity.Initialize() at Mindscape.LightSpeed.Entity..ctor(Boolean autoInitialize) at Mindscape.LightSpeed.Entity`1..ctor()
any ideas? Maybe framework just can't work with the structure I have created for demo and all attempts are useless? |
|
|
Okay after many tryouts I see this logic is not possible with this framework. |
|