(Created page with "In MDriven it is possible to create your own persistence mappers per attribute. If you do not create your own it is still possible to pick and choose from the ones supplied....") |
No edit summary |
||
Line 1: | Line 1: | ||
In MDriven it is possible to create your own persistence mappers per attribute. If you do not create your own it is still possible to pick and choose | In MDriven, it is possible to create your own persistence mappers per attribute. If you do not create your own, it is still possible to pick and choose from the ones supplied. Note that this level of specialization will stop the generic tools like MDriven Prototyping and MDriven Server from functioning properly (they use our standard). | ||
One specific case that | One specific case that raises questions about this functionality is Enums. | ||
Our standard is to store the enum value as its text representation in the database. | Our standard is to store the enum value as its text representation in the database. | ||
However legacy databases typically treat application level enums as integers. | However, legacy databases typically treat application-level enums as integers. | ||
This question was posted on the forum: | This question was once posted on the forum: | ||
'''When using enums that are persisted with the GenericEnumAsInteger mapper, you can’t | '''When using enums that are persisted with the GenericEnumAsInteger mapper, you can’t execute a query like this:''' | ||
MyEnumType eTest = MyEnumType.theFirstValue | MyEnumType eTest = MyEnumType.theFirstValue | ||
IList<Class1> result = (from x in ecoSpace.PSQuery<Class1>() where | IList<Class1> result = (from x in ecoSpace.PSQuery<Class1>() where | ||
(x.MyEnumAttribute == eTest) select x).ToList(); | (x.MyEnumAttribute == eTest) select x).ToList(); | ||
'''In this case you get the following error:''' | '''In this case, you get the following error:''' | ||
An exception of type ‘'''System.Data.SqlClient.SqlException’''' occurred in Eco.Persistence.dll but was not handled in user code. | An exception of type ‘'''System.Data.SqlClient.SqlException’''' occurred in Eco.Persistence.dll but was not handled in the user code. | ||
Additional information: Error converting a nvarchar value to int. Using the GenericEnumAsVar or executing a MemQuery instead works as expected. | Additional information: Error converting a nvarchar value to int. Using the GenericEnumAsVar or executing a MemQuery instead works as expected. | ||
Line 29: | Line 29: | ||
[[File:Persistencce Mapper 3.png|frameless|392x392px]] | [[File:Persistencce Mapper 3.png|frameless|392x392px]] | ||
What the questioner was unaware of – since this was not easily understood – hence this article – is | What the questioner was unaware of – since this was not easily understood – hence this article – is how MDriven finds what enum persistence mapper to use. For the attribute, we use the override value set on the attribute, but for the parameter, the attribute is not available to us. | ||
Instead we use this strategy: | Instead, we use this strategy: | ||
1. Try to find a Persistence Mapper with the same name as the Type of the parameter. In this case “EcoProject1.MyEnumType” | 1. Try to find a Persistence Mapper with the same name as the Type of the parameter. In this case “EcoProject1.MyEnumType” | ||
Line 37: | Line 37: | ||
2. If none was found and if the parameter is of type Enum – we try to find the persistence mapper with the name “Enum” | 2. If none was found and if the parameter is of type Enum – we try to find the persistence mapper with the name “Enum” | ||
This is what | This is what happens in this particular case. In the error reported, I believe that the “Enum” persistence mapper is still set to “Eco.Persistence.Default.GenericEnumAsVarChar” | ||
[[File:Persistencce Mapper 4.png|frameless|397x397px]] | [[File:Persistencce Mapper 4.png|frameless|397x397px]] | ||
So what must be done to make the | So what must be done to make the LINQ query below work is either to change persistence mapper Enum to Eco.Persistence.Default.GenericEnumAsInteger or add a new row to the collection of persistence mappers named EcoProject1.MyEnumType with MapperTypeName Eco.Persistence.Default.GenericEnumAsInteger . | ||
MyEnumType eTest = MyEnumType.theFirstValue | MyEnumType eTest = MyEnumType.theFirstValue | ||
IList<Class1> result = (from x in ecoSpace.PSQuery<Class1>() where (x.MyEnumAttribute | IList<Class1> result = (from x in ecoSpace.PSQuery<Class1>() where (x.MyEnumAttribute | ||
== eTest) select x).ToList(); | == eTest) select x).ToList(); | ||
[[Category:Advanced]] | [[Category:Advanced]] | ||
[[Category:MDriven Designer]] | |||
[[Category:MDriven Designer]] |
Revision as of 06:35, 6 February 2023
In MDriven, it is possible to create your own persistence mappers per attribute. If you do not create your own, it is still possible to pick and choose from the ones supplied. Note that this level of specialization will stop the generic tools like MDriven Prototyping and MDriven Server from functioning properly (they use our standard).
One specific case that raises questions about this functionality is Enums.
Our standard is to store the enum value as its text representation in the database.
However, legacy databases typically treat application-level enums as integers.
This question was once posted on the forum:
When using enums that are persisted with the GenericEnumAsInteger mapper, you can’t execute a query like this:
MyEnumType eTest = MyEnumType.theFirstValue IList<Class1> result = (from x in ecoSpace.PSQuery<Class1>() where (x.MyEnumAttribute == eTest) select x).ToList();
In this case, you get the following error:
An exception of type ‘System.Data.SqlClient.SqlException’ occurred in Eco.Persistence.dll but was not handled in the user code.
Additional information: Error converting a nvarchar value to int. Using the GenericEnumAsVar or executing a MemQuery instead works as expected.
What the questioner (Alois) has done in this case is to override the suggested persistence mapper on attribute level:
The name GenericEnumAsInteger was found in this collection:
What the questioner was unaware of – since this was not easily understood – hence this article – is how MDriven finds what enum persistence mapper to use. For the attribute, we use the override value set on the attribute, but for the parameter, the attribute is not available to us.
Instead, we use this strategy:
1. Try to find a Persistence Mapper with the same name as the Type of the parameter. In this case “EcoProject1.MyEnumType”
2. If none was found and if the parameter is of type Enum – we try to find the persistence mapper with the name “Enum”
This is what happens in this particular case. In the error reported, I believe that the “Enum” persistence mapper is still set to “Eco.Persistence.Default.GenericEnumAsVarChar”
So what must be done to make the LINQ query below work is either to change persistence mapper Enum to Eco.Persistence.Default.GenericEnumAsInteger or add a new row to the collection of persistence mappers named EcoProject1.MyEnumType with MapperTypeName Eco.Persistence.Default.GenericEnumAsInteger .
MyEnumType eTest = MyEnumType.theFirstValue IList<Class1> result = (from x in ecoSpace.PSQuery<Class1>() where (x.MyEnumAttribute == eTest) select x).ToList();