This question is locked. New answers and comments are not allowed.
I'm having trouble getting a one-way association to work from a base class (Transaction) to another persistent class (Profile) using the foreign key column I've specified in ToColumn().
Using horizontal inheritance in code-only mappings, I defined a base class (Transaction) and several derived classes (TrxAddCustomer, TrxReceivePayment, etc). Each of these transaction records needs to point to a Profile, but I don't want the Profile to be littered with a collection for every transaction type, so I'm using a one-way association.
I found that I had to define all of the HasProperty statements in the derived classes, even for those properties that were already defined in base classes. This was annoying, knowing that the metadata for the base class was already defined once, and that setting column types or other parameters in the base class were ignored instead of being inherited by the derived classes. Because of this, I tried adding the association from each Transaction-derived class to the Profile class, but I got the error about the property's backing field not existing in the derived class (again, OpenAccess being unintelligent in looking for where they're defined), so I moved the association to the configuration of the base class. Despite using ToColumn("ProfileOid") to designate the foreign key column, OpenAccess creates and uses a column called Oid2.
Here is my test data model:
My code-only fluent mapping looks like this:
I just downloaded OpenAccess a week or two ago, so I assume I'm using the latest version. VS2010, Win7, SQL Server. I was going to attach my complete Visual Studio test solution with the problem. but the forums here don't allow it? Bummer. Here is that Sample Project.
I attached an image to this post showing the query results.
Using horizontal inheritance in code-only mappings, I defined a base class (Transaction) and several derived classes (TrxAddCustomer, TrxReceivePayment, etc). Each of these transaction records needs to point to a Profile, but I don't want the Profile to be littered with a collection for every transaction type, so I'm using a one-way association.
I found that I had to define all of the HasProperty statements in the derived classes, even for those properties that were already defined in base classes. This was annoying, knowing that the metadata for the base class was already defined once, and that setting column types or other parameters in the base class were ignored instead of being inherited by the derived classes. Because of this, I tried adding the association from each Transaction-derived class to the Profile class, but I got the error about the property's backing field not existing in the derived class (again, OpenAccess being unintelligent in looking for where they're defined), so I moved the association to the configuration of the base class. Despite using ToColumn("ProfileOid") to designate the foreign key column, OpenAccess creates and uses a column called Oid2.
Here is my test data model:
public abstract class ModelObject{ public Guid Oid { get; set; }}public class Profile : ModelObject{ public string Name { get; set; }}public abstract class Transaction : ModelObject{ public string TransactionCode { get; set; } public Profile Profile { get; set; } public Guid ProfileOid { get; set; }}public class TrxGetBallmerDrunk : Transaction{ public string Joke { get; set; }}My code-only fluent mapping looks like this:
protected override IList<MappingConfiguration> PrepareMapping(){ var configurations = new List<MappingConfiguration>(); var ModelObjectConfig = new MappingConfiguration<ModelObject>(); ModelObjectConfig.MapType( x => new { Oid = x.Oid }) .Inheritance(InheritanceStrategy.Horizontal); var TransactionConfig = new MappingConfiguration<Transaction>(); TransactionConfig.MapType( x => new { Profile = x.Profile, ProfileOid = x.ProfileOid, TransactionCode = x.TransactionCode }) .Inheritance(InheritanceStrategy.Horizontal); TransactionConfig.HasAssociation(x => x.Profile).ToColumn("ProfileOid"); var ProfileConfig = new MappingConfiguration<Profile>(); ProfileConfig.MapType(x => new { Oid = x.Oid, Name = x.Name }).ToTable("Profiles"); ProfileConfig.HasProperty(x => x.Oid).IsIdentity(KeyGenerator.Guid); ProfileConfig.HasProperty(x => x.Name).HasLength(20); var BallmerConfig = new MappingConfiguration<TrxGetBallmerDrunk>(); BallmerConfig.MapType(x => new { Oid = x.Oid, TransactionCode = x.TransactionCode, Joke = x.Joke, Profile = x.Profile, ProfileOid = x.ProfileOid }).ToTable("TrxGetBallmerDrunk"); BallmerConfig.HasProperty(x => x.Oid).IsIdentity(KeyGenerator.Guid); BallmerConfig.HasProperty(x => x.TransactionCode).HasLength(20); BallmerConfig.HasProperty(x => x.Joke).HasLength(100); configurations.Add(ModelObjectConfig); configurations.Add(TransactionConfig); configurations.Add(ProfileConfig); configurations.Add(BallmerConfig); return configurations;}I just downloaded OpenAccess a week or two ago, so I assume I'm using the latest version. VS2010, Win7, SQL Server. I was going to attach my complete Visual Studio test solution with the problem. but the forums here don't allow it? Bummer. Here is that Sample Project.
I attached an image to this post showing the query results.