Hi,
Supposedly I have a db / model as such :
public
class
UserModel
{
public
int
Id {
get
;
set
; }
public
string
DisplayName {
get
;
set
; }
public
string
Status {
get
;
set
; }
public
virtual
LookupModel Lookup {
get
;
set
; }
}
public
class
LookupModel
{
public
string
Description {
get
;
set
; }
public
string
Value {
get
;
set
; }
}
Can I use the One Sided Association to map between the UserModel and LookupModel based on "Status" field in UserModel and "Value" field in LookupModel ?
Is association in Telerik Data Access only limited to type integer and using Foreign Key ?
3 Answers, 1 is accepted
Telerik Data Access does not limit the type of columns used in association to integers (as long as both columns are of the same type). In your case I see you have chosen strings which is a certainly supported scenario.
You should note though that one-to-one association is only supported if it is between the primary keys of your entities. You should be able to create one-to-many or many-to-many association with the described set-up.
Please refer to this documentation section to learn more of how to association are defined in Telerik Data Access and how they function.
Do not hesitate to get back to us if you have any more questions or need any further assistance.
Regards,
Boyan
Telerik
Hi Boyan,
I've tried to use string as the association but it throws an error :
Error reading field TestOpenAccess.UserModel.<Lookup>k__BackingField from ResultSet: System.InvalidOperationException: Wrong type System.String
at OpenAccessRuntime.Data.CommonNumberConverter.ReadIntegralValue(DbDataReader r, Int32 pos, Type t)
at OpenAccessRuntime.Data.IntConverter.Read(DataHolder& data)
at OpenAccessRuntime.Relational.metadata.RelationalColumn.GetValue(DataHolder& data)
at OpenAccessRuntime.Relational.RelationalGenericOID.CopyKeyFields(DataHolder& data)
at OpenAccessRuntime.Relational.RelationalGenericState.GetFieldDataPass1(DataHolder& holder, IBackendField field)
at OpenAccessRuntime.Relational.RelationalGenericState.copyPass1Fields(ResultSet rs, FetchGroupField[] fetchGroups, Int32 firstCol) System.InvalidOperationException: Wrong type System.String
at OpenAccessRuntime.Data.CommonNumberConverter.ReadIntegralValue(DbDataReader r, Int32 pos, Type t)
at OpenAccessRuntime.Data.IntConverter.Read(DataHolder& data)
at OpenAccessRuntime.Relational.metadata.RelationalColumn.GetValue(DataHolder& data)
at OpenAccessRuntime.Relational.RelationalGenericOID.CopyKeyFields(DataHolder& data)
at OpenAccessRuntime.Relational.RelationalGenericState.GetFieldDataPass1(DataHolder& holder, IBackendField field)
at OpenAccessRuntime.Relational.RelationalGenericState.copyPass1Fields(ResultSet rs, FetchGroupField[] fetchGroups, Int32 firstCol)
While the mapping settings is :
var configurations = new List<
MappingConfiguration
>();
var lookupMapping = new MappingConfiguration<
LookupModel
>();
lookupMapping.MapType(comlookup => new
{
Id = comlookup.Id,
Key = comlookup.Key,
Description = comlookup.Description,
Value = comlookup.Value,
CreatedOn = comlookup.CreatedOn,
CreatedBy = comlookup.CreatedBy,
ModifiedOn = comlookup.ModifiedOn,
ModifiedBy = comlookup.ModifiedBy
}).ToTable(tableName: "comlookup");
lookupMapping.HasProperty(x => x.Id).IsIdentity(KeyGenerator.Autoinc);
configurations.Add(lookupMapping);
var userMapping = new MappingConfiguration<
UserModel
>();
userMapping.MapType(systemuser => new
{
Id = systemuser.Id,
LoginId = systemuser.LoginId,
DisplayName = systemuser.DisplayName,
Salt = systemuser.Salt,
Password = systemuser.Password,
Status = systemuser.Status,
EmployeeId = systemuser.EmployeeId,
CreatedOn = systemuser.CreatedOn,
CreatedBy = systemuser.CreatedBy,
ModifiedOn = systemuser.ModifiedOn,
ModifiedBy = systemuser.ModifiedBy
})
.ToTable(tableName: "msSystemUser");
userMapping.HasProperty(x => x.Id).IsIdentity(KeyGenerator.Autoinc);
userMapping.HasAssociation(x => x.Lookup).ToColumn("Status");
configurations.Add(userMapping);
return configurations;
This error usually indicates that a property has backing field that cannot be accessed.
One potential reason for that could be inheritance - if a Property has backing field in the base class that is not accessible. Do you use inheritance in you model for the entities in question?
Another possible reason is using .HasFieldName with a field name that does not exists. Could you check for that as well? Also do you use default naming rules as shown in this documentation article?
I was not able to reproduce the behavior you described. For you convince I have prepared a small sample project that demonstrates the described mapping scenario. Please find it attached. Applying the same approach, does the issue continue to persist?
I hope this is helpful. Do get back to us should the issue continue to persist.
Regards,
Boyan
Telerik