Consider the following class hierarchy:
public abstract class Animal {}
public class Dog : Animal
{
public int DogTagNumber { get; set; }
}
public class Cat : Animal
{
public int CatTagNumber { get; set; }
}
Note: DogTagNumber and CatTagNumber is purposely placed in the subclasses instead of as TagNumber in Animal class to demonstrate property explicit to each subclass.
The question:
Using Fluent NHibernate, is it possible to map unique constrain on class discriminator together with subclasses's explicit properties, like below:
Unique("DOG", DogTagNumber) and also Unique("CAT", CatTagNumber)
The purpose is to ensure the uniqueness of each Dog and Cat with regard to the discriminator in the table on database level.
Thanks in advance.
try this one:
Map(x => x.Something).UniqueKey("KeyName");
DiscriminateSubClassesOnColumn("discr_column").UniqueKey("KeyName");
Related
As you know, C# 9.0 (.Net 5) now allows Covariant Returns. I need help applying this to a set of classes having Auto-Implemented properties.
I have two abstract classes that represent financial bank accounts and transactions. I made them abstract since I will pull data from various data sources and while the main properties will be common across all sources, each source may have additional fields I want to keep. A 1 to Many relationship exists between both classes (1 account has many transactions AND 1 transaction belongs to only 1 account).
public abstract class BankAccount
{
public string Name { get; set; }
public IList<Transaction> Transactions { get; set; } = new List<Transaction>();
...
}
public abstract class Transaction
{
public string Name { get; set; }
public virtual BankAccount BankAccount { get; set; } // This doesn't work unless I remove set;
...
}
And here is an example of the concrete implementations
public class PlaidBankAccount : BankAccount
{
public string PlaidId { get; set; }
...
}
public class PlaidTransaction : Transaction
{
public string PlaidId { get; set; }
public override PlaidBankAccount BankAccount { get; set; } // This doesn't work unless I remove set;
...
}
What I want to do is to override the base class getters and setters so that they use derived classes. For example:
If I create an instance of the concrete transaction and call the BankAccount getter, I want to get an instance of the derived PlaidBankAccount not the base BankAccount.
What I've found is that when I only define virtual getter in the base class and override it in the derived class, it works. But just as I add both properties {get;set;}, I get the same error as in previous C# versions:
error CS1715: 'PlaidTransaction.BankAccount': type must be 'BankAccount' to match overridden member 'Transaction.BankAccount'
How could I fix this?
In C# 9 properties are only able to have co-variant returns when they are readonly, so unfortunately, no set; is possible.
An overriding property declaration must specify exactly the same access modifier, type, and name as the inherited property. Beginning with C# 9.0, read-only overriding properties support covariant return types. The overridden property must be virtual, abstract, or override.
From the Microsoft Docs - Override keyword
I need to map the following class hierarchy using Entity Framework 4.3.
public abstract class Rule
{
public long Id {get;set;}
public abstract ICollection<Parameter> Parameters {get;set;}
}
public class Entity1
{
public long Id {get;set;}
public ICollection<Rule> Rules {get;set;}
// Map Rule to table Entity1Rules and
// Parameters to table Entity1RuleParameters
}
public class Entity2
{
public long Id {get;set;}
public ICollection<Rule> Rules {get;set;}
// Map Rule to table Entity2Rules and
// Parameters to table Entity2RuleParameters
}
Thanks
It is not possible. Each entity can be mapped only once. It would require you to involve some inheritance in Rule and Parameter classes but at the end your Entity1 and Entity2 will need to reference derived rule to set correct structure.
I have the following domain model to map using ClassMaps/SubclassMaps
A - HasMany -> B (A should be table)
B is abstract in code (and should not be a table)
C,D,E inherits B and all have very different properties. Each of them should be a table with foreign key to A.
I get the tables I want, but I cannot see how to map HasMany from entity A that has one property IList<B> SomeProperty
I would like to define in ClassMap<A> that SomeProperty cascades on C,D,E
This obviously doesn't work, in ClassMap<A>:
HasMany<C>(x => x.B).Cascade.All();
HasMany<D>(x => x.B).Cascade.All();
HasMany<E>(x => x.B).Cascade.All();
As I cannot duplicate B.
Example:
public class Person
{
public virtual IList<Animal> Animals { get; set; }
public void AddAnimal(Animal animal)
{
Animals.Add(animal);
}
}
public abstract class Animal
{
//some common properties
}
public class Cat : Animal
{
//some cat properties
}
public class Horse : Animal
{
//some horse properties
}
In this case I would like ClassMap<Person> to map to Cat and Horse over the abstract class Animal.
Try...
public void AddAnimal(Animal animal)
{
animal.Person = this; //Recomended
Animals.Add(animal);
}
public abstract class Animal
{
public virtual Person Person {get;set;} //Recomended Addition
//some common properties
}
I'm trying to use the table-per-subclass (which fluent-nhibernate automaps by default) with a class structure like the following:
public class Product
{
public virtual int Id{ get; set; }
public virtual string Title{ get; set; }
}
public class ProductPackage : Product
{
public ProductPackage(){ Includes = new List<Product>(); }
public virtual IList<Prodcut> Includes{ get; private set; }
[EditorBrowsable( EditorBrowsableState.Never )]
public class ProductPackageAutoOverride : IAutoMappingOverride<ProductPackage>
{
public void Override( AutoMap<ProductPackage> mapping )
{
mapping.HasManyToMany( x => x.Includes )
.WithTableName( "IncludesXProduct" )
.WithParentKeyColumn( "ProductId" )
.WithChildKeyColumn( "IncludesProductId" )
.Cascade.SaveUpdate();
}
}
}
Instead of adding a new table "IncludesXProduct" to represent the many-to-many mapping, it adds a property "ProductPackageId" to the Product table. Of course persisting to this schema doesn't work.
Have I missed something simple or is this type of thing not really supported by NHibernate?
It is possible to do this with NHibernate. Unfortunately my fluent syntax isn't very good, but it looks like FNH is somehow regarding the relationship as a many-to-one rather than a many-to-many.
If you tag your question with "fluent-nhibernate" then you may get more knowledgeable people answering.
I'm in the process of adapting Fluent NHibernate to our existing legacy app and am trying to determine how to use ClassMap and SubclassMap for the entity hierarchy shown.
// BaseObject contains database columns common to every table
public class BaseObject
{
// does NOT contain database id column
public string CommonDbCol1 { get; set; }
public string CommonDbCol2 { get; set; }
// ...
}
public class Entity1 : BaseObject
{
public int Entity1Id { get; set; }
// other Entity1 properties
}
public class Entity2 : BaseObject
{
public int Entity2Id { get; set; }
// other Entity2 properties
}
The identity columns for Entity1 and Entity2 are uniquely named per table. BaseObject contains columns that are common to all of our entities. I am not using AutoMapping, and thought I could use ClassMap on the BaseObject, and then use SubclassMap on each Entity like this:
public class Entity1Map : SubclassMap<Entity1>
{
public Entity1Map()
{
Id(x => x.Entity1Id);
// ...
}
}
Problem is, Id() is not defined for SubclassMap. So, how do I specify within each Entity1Map, Entity2Map, ... (we have 100+ entity classes all inheriting from BaseObject) what the entity-specific Id is?
Thanks in advance for any insight!
It's not possible to do that in either Fluent NHibernate or NHibernate. Do you actualy want your classes to be mapped as subclasses, or do you just want them to share the common mappings? If you truly want subclasses, then you're going to need to have them share the identity column, no other way around it; if you don't want actual subclasses, create an abstract ClassMap<T> where T : BaseObject and map the common properties in there.
Something like:
public abstract class BaseObjectMap<T> : ClassMap<T> where T : BaseObject
{
public BaseObjectMap()
{
Map(x => x.CommonProperty1);
}
}