I have some troubles with breeze and expanding deep properties. Here's the code:
public abstract class BaseEntity
{
public virtual Guid? Id { get; set; }
}
public partial class Request : BaseEntity
{
public virtual IList<ServiceBase> Services { get; set; }
public Request()
{
this.Services = new List<ServiceBase>();
}
}
public abstract class ServiceBase : BaseEntity
{
public virtual Guid? RequestId { get; set; }
public virtual Request Request { get; set; }
public virtual IList<Contact> Contacts { get; set; }
public ServiceBase()
{
Contacts = new List<Contact>();
}
}
public class ServiceTranslation : ServiceBase
{
public virtual string MyProp { get; set; }
}
public class ServiceModification : ServiceBase
{
public virtual string MyProp2 { get; set; }
}
public class BaseMapping<T> : ClassMapping<T> where T : BaseEntity
{
public BaseMapping()
{
this.Lazy(true);
//Schema("DEMO");
Id(x => x.Id, map => { map.Generator(Generators.GuidComb); });
}
}
public RequestMap()
{
this.Bag<ServiceBase>(x => x.Services, colmap =>
{
//colmap.Schema("demo");
colmap.Key(x => x.Column("RequestId"));
colmap.Inverse(false);
colmap.Cascade(Cascade.All);
}, map =>
{
map.OneToMany();
});
}
}
public ServicebaseMap()
{
this.Property(x => x.RequestId, map =>
{
map.Column("RequestId");
map.Insert(false);
map.Update(false);
map.NotNullable(true);
});
this.ManyToOne(x => x.Request, map =>
{
map.Column("RequestId");
map.NotNullable(true);
map.Cascade(Cascade.None);
});
this.Bag<Contact>(x => x.Contacts, colmap =>
{
//colmap.Schema("demo");
colmap.Table("ServiceContact");
colmap.Key(x => { x.Column("ContactId"); });
colmap.Inverse(false);
colmap.Cascade(Cascade.All);
}, map =>
{
map.ManyToMany(x => { x.Column("ServiceId"); });
});
}
public class ServicetranslationMap : JoinedSubclassMapping<ServiceTranslation>
{
public ServicetranslationMap()
{
Property(x => x.MyProp);
}
}
public class ServiceModificationMap : JoinedSubclassMapping<ServiceModification>
{
public ServiceModificationMap()
{
Property(x => x.MyProp2);
}
}
A Request contains a collection of ServiceBase(Abstract) and those ServiceBase contains contacts.
If I try to expand like this:
http://localhost:61971/breeze/EAINHibernate/Requests?$expand=Services,Services.Contacts
Contacts are never expanded. Looking through the code, I found the ExpandMap used by breeze in the InitializeWithCascade methods of NHInitializer.cs contains the type of the base class. But, when expanding, the type of the object in the collection is the concrete type, either ServiceTranslation or either ServiceModification.
What can we do in that kind of situation? Can we expect a fix?
Good catch! Yes, that's a bug and you can expect a fix.
UPDATE
Fixed now in GitHub. Fix will be included in the next release.
Related
After updating to nHibernate (4.0.2.4000 via nuget), the many to many mappings that previously worked now cause mapping exception "Could not determine type for: nHibernateManyToMany.IRole, nHibernateManyToMany, for columns: NHibernate.Mapping.Column(id)"
Seems to be only for many to many, and when the List is a interface (i.e. List<Role> vs List<IRole>).
Example code that now fails:
class Program
{
static void Main(string[] args)
{
var configuration = new Configuration().SetProperty(Environment.ReleaseConnections, "on_close")
.SetProperty(Environment.Dialect, typeof(SQLiteDialect).AssemblyQualifiedName)
.SetProperty(Environment.ConnectionDriver, typeof(SQLite20Driver).AssemblyQualifiedName)
.SetProperty(Environment.CollectionTypeFactoryClass, typeof(DefaultCollectionTypeFactory).AssemblyQualifiedName)
.SetProperty(Environment.CommandTimeout, "0");
var mapper = new ModelMapper();
mapper.AddMappings(new[] { typeof(EmployeeMapping), typeof(RoleMapping) });
var hbmMapping = mapper.CompileMappingForAllExplicitlyAddedEntities();
hbmMapping.autoimport = false;
configuration.AddMapping(hbmMapping);
// this line will fail
var factory = configuration.BuildSessionFactory();
}
}
public class Employee
{
public virtual int Id { get; set; }
public virtual List<IRole> Roles { get; set; }
}
public interface IRole
{
int Id { get; set; }
string Description { get; set; }
}
public class Role : IRole
{
public virtual int Id { get; set; }
public virtual string Description { get; set; }
}
public class EmployeeMapping : ClassMapping<Employee>
{
public EmployeeMapping()
{
Id(c => c.Id, x =>
{
x.Type(NHibernateUtil.Int32);
x.Generator(Generators.Identity);
x.Column("EmployeeId");
});
Bag(x => x.Roles, m =>
{
m.Table("EmployeeRole");
m.Key(km =>
{
km.Column("EmployeeId");
km.NotNullable(true);
km.ForeignKey("FK_Role_Employee");
});
m.Lazy(CollectionLazy.Lazy);
}, er => er.ManyToMany(m =>
{
m.Class(typeof(Role));
m.Column("RoleId");
}));
}
}
public class RoleMapping : ClassMapping<Role>
{
public RoleMapping()
{
Id(c => c.Id, x =>
{
x.Type(NHibernateUtil.Int32);
x.Generator(Generators.Identity);
x.Column("RoleId");
});
Property(x => x.Description, c =>
{
c.Length(50);
c.NotNullable(true);
});
}
}
Any help or suggestions about where we could look for details on how this has changed since v3 would be appreciated.
Turned out to be a bug in nHibernate.
A pull request has been submitted here https://github.com/nhibernate/nhibernate-core/pull/385
I have two classes, each having a domain and a repo version.
DOMAIN:
public class MusicInfo
{
public string Id { get; set; }
public MusicImage Image { get; set; }
public MusicInfo(byte[] image)
{
this.Image = new MusicImage(this, image);
}
}
public class MusicImage
{
public byte[] Blob { get; set; }
public MusicInfo MusicInfo { get; set; }
public string Id { get; set; }
public MusicImage(MusicInfo musicInfo, byte[] blob)
{
if (musicInfo == null)
throw new ArgumentNullException("musicInfo");
if (blob == null)
throw new ArgumentNullException("blob");
this.MusicInfo = musiscInfo;
this.Blob = blob;
}
}
REPO:
public class MusicInfoRepo
{
public virtual long Id { get; set; }
public virtual MusicImageRepo Image { get; set; }
}
public class MusicImageRepo
{
public virtual byte[] Blob { get; set; }
public virtual MusicInfoRepo MusicInfo { get; set; }
public virtual long Id { get; set; }
}
And here are their mappings:
public class MusicInfoRepoMap : HighLowClassMapping<MusicInfoRepo>
{
public MusicInfoRepoMap()
{
Table("MusicInfo");
Id(f => f.Id, m => m.Generator(Generators.HighLow, HighLowMapper));
OneToOne(f => f.Image, m => m.Cascade(Cascade.All));
}
}
public class MusicImageRepoMap : ClassMapping<MusicImageRepo>
{
public MusicImageRepoMap()
{
Table("MusicImage");
Id(f => f.Id, m => m.Generator(Generators.Foreign<MusicImageRepo>(f => f.MusicInfo)));
Property(f => f.Blob, m =>
{
m.NotNullable(true);
m.Column(c => c.SqlType("VARBINARY(MAX)"));
m.Length(Int32.MaxValue);
m.Update(false);
});
OneToOne(f => f.MusicInfo,
m =>
{
m.Cascade(Cascade.None);
m.Constrained(true);
m.Lazy(LazyRelation.NoLazy);
});
}
}
When I am trying to query for a ClassA that has a one to one relationship with ClassB which also has a one to one relationship with MusicInfo, an error occurs that says:
Missing type map configuration or unsupported mapping.
Mapping types:
MusicImageRepo -> Byte[]
Blah.MusicImageRepo -> System.Byte[]
Destination path:
List`1[0]
Source value:
Blah.MusicImageRepo
but here is how i map them:
Mapper.CreateMap<MusicInfo, MusicInfoRepo>();
Mapper.CreateMap<MusicInfoRepo, MusicInfo>();
Mapper.CreateMap<MusicImage, MusicImageRepo>();
Mapper.CreateMap<MusicImageRepo, MusicImage>();
There are no problems when saving these classes.
I really dont get why the error happens.
Will really appreciate your help.
OneToOne says that the other entity/table has the Reference(column). It should not work when both sides have a onetoone mapping since they bounce the responsiblity back and forth.
Also the classes look a bit overcomplicated when all you need is to store the bytes somewhere else.
public class MusicInfoRepo
{
public virtual long Id { get; private set; }
public virtual MusicImageRepo Image { get; private set; }
public MusicInfo(byte[] image)
{
this.Image = new MusicImageRepo(this, image);
}
}
public class MusicImageRepo
{
public virtual MusicInfoRepo MusicInfo { get; private set; }
public virtual byte[] Blob { get; set; }
}
public class MusicInfoRepoMap : HighLowClassMapping<MusicInfoRepo>
{
public MusicInfoRepoMap()
{
Table("MusicInfo");
Id(f => f.Id, m => m.Generator(Generators.HighLow, HighLowMapper));
OneToOne(f => f.Image, m => m.Cascade(Cascade.All));
}
}
public class MusicImageRepoMap : ClassMapping<MusicImageRepo>
{
public MusicImageRepoMap()
{
Table("MusicImage");
ComposedId(m => m.ManyToOne(x => x.MusicInfo));
Property(f => f.Blob, m =>
{
m.NotNullable(true);
m.Column(c => c.SqlType("VARBINARY(MAX)"));
m.Length(Int32.MaxValue);
m.Update(false);
});
}
}
Note: think about it if the seperation between DomainModel and MappedModel really makes sense. NHibernate goes to great length supporting mapping of DomainModels.
I have the following POCO classes:
public class Container
{
public virtual Int64 ContainerId { get; protected set; }
public virtual string Name { get; set; }
public virtual Location Location { get; set; }
}
public abstract class Location
{
public virtual Int64 LocationId { get; protected set; }
public virtual string Name { get; set; }
}
public class UniqueLocation : Location
{
public virtual Container Container { get; set; }
}
public class SharedLocation : Location
{
public SharedLocation()
{
this.Containers = new List<Container>();
}
public virtual IList<Container> Containers { get; set; }
}
and the following Fluent mapping:
public class ContainerMap: ClassMap<Container>
{
public ContainerMap()
{
Table("Containers");
Id(x => x.ContainerId);
Map(x => x.Name);
ReferencesAny(x => x.Location).IdentityType<Int64>().EntityTypeColumn("LocationType").EntityIdentifierColumn("LocationId")
.AddMetaValue<UniqueLocation>("U")
.AddMetaValue<SharedLocation>("S");
}
}
public class LocationMap : ClassMap<Location>
{
public LocationMap()
{
Table("Locations");
Id(x => x.LocationId);
Map(x => x.Name);
}
}
public class UniqueLocationMap : SubclassMap<UniqueLocation>
{
public UniqueLocationMap()
{
HasOne(x => x.Container).PropertyRef(x => x.Location).ForeignKey("LocationId").Cascade.All().Constrained();
}
}
public class SharedLocationMap : SubclassMap<SharedLocation>
{
public SharedLocationMap()
{
HasMany(x => x.Containers).KeyColumn("LocationId");
}
}
The problem is HasOne() mapping generates the following exception: "broken column mapping for: Container.Location of: UniqueLocation, type Object expects 2 columns, but 1 were mapped".
How do I tell HasOne() to use/map both LocationType and LocationId?
AFAIK Where conditions are not possible on Entity references except using Formulas. The design seems a strange because it would be nasty to change a unique Location to a shared location.
what you want can be done using:
Reference(x => x.Container).Formula("(SELECT c.Id FROM Container c WHERE c.LocationId = Id AND c.LocationType = 'U')");
But i would prefere
class Location
{
...
public virtual bool IsUnique { get { return Container.Count == 1; } }
}
I've created 2 objects:
public class Set
{
public Set()
{
_sorts = new List<Sort>();
}
public virtual int Id { get; set; }
public virtual string Code { get; set; }
private ICollection<Sort> _sorts;
public virtual ICollection<Sort> Sorts
{
get { return _sorts; }
set { _sorts = value; }
}
}
public class Sort
{
public Sort()
{
_sets = new List<Set>();
}
public virtual int Id { get; set; }
public virtual string Name { get; set; }
private ICollection<Set> _sets;
public virtual ICollection<Set> Sets
{
get { return _sets; }
set { _sets = value; }
}
}
And 2 mappings:
public class SetMapping: ClassMapping<Set>
{
public SetMapping()
{
Table("Sets");
Id(x => x.Id, map => map.Generator(IdGeneratorSelector.CreateGenerator()));
Property(x => x.Code, map =>
{
map.Length(50);
map.NotNullable(false);
});
Bag(x => x.Sorts, map =>
{
map.Key(k =>
{
k.Column("SetId");
k.NotNullable(true);
});
map.Cascade(Cascade.All);
map.Table("SetsToSorts");
map.Inverse(true);
}, r => r.ManyToMany(m => m.Column("SortId")));
}
}
public class SortMapping: ClassMapping<Sort>
{
public SortMapping()
{
Table("Sorts");
Id(x => x.Id, map => map.Generator(IdGeneratorSelector.CreateGenerator()));
Property(x => x.Name, map =>
{
map.Length(50);
map.NotNullable(false);
});
}
}
usage:
Set can have many sorts
Sort can belong to many sets.
And I would like to use this as:
var set = new Set() {Code = "001"};
var sort = new Sort {Name = "My name"};
set.Sorts.Add(sort);
sort.Sets.Add(set);
Somehow the relations are not working yet because when I try to use the above code to add sorts to set for example and commit then I don't see any records saved to the SetsToSorts linked table.
Does anyone have a clue what I'm missing in my mapping? Or otherwise doing wrong?
Thank you,
Joost
Your mapping says that Set's Sort collection is inverse (map.Inverse(true)). That means the other side of the bidirectional association is responsible for persisting changes.
But your Sort class mapping doesn't have any collection mapping. Remove map.Inverse(true) on SetMapping or add noninverse collection mapping to SortMapping.
I have an Address entity with 2 sub types. Here's my simplified code:
public class Address {
public string Street1 { get; set; }
public string Country { get; set; }
}
public class UsAddress : Address {
public string State { get; set; }
}
public class CandianAddress : Address {
public string Providence { get; set; }
}
Here's my simplified view models:
public class LocationModel {
public string Street1 { get; set; }
}
public class UsLocationModel : LocationModel {
public string State { get; set; }
}
public class CaLocationModel : LocationModel {
public string Providence { get; set; }
}
public class AddressModel {
public int? Country { get; set; }
public UsLocationModel UsLocation { get; set; }
public CaLocationModel CaLocation { get; set; }
}
Here's my simplified AutoMapper config:
Mapper.CreateMap<Address, AddressModel>()
.Include<UsAddress, AddressModel>()
.Include<CanadianAddress, AddressModel>();
Mapper.CreateMap<UsAddress, AddressModel>();
Mapper.CreateMap<CanadianAddress, AddressModel>();
Mapper.CreateMap<Address, LocationModel>()
.Include<UsAddress, USLocationModel>()
.Include<CanadianAddress, CALocationModel>();
Mapper.CreateMap<UsAddress, USLocationModel>();
Mapper.CreateMap<CanadianAddress, CALocationModel>();
I can't figure out how to resolve the UsLocation and CaLocation properties on AddressModel...
I figured it out. Here's a simplified version of my Automapper config:
Mapper.CreateMap<Address, AddressModel>()
.Include<UsAddress, AddressModel>()
.Include<CanadianAddress, AddressModel>()
.ForMember(x => x.USLocation, a => a.Ignore())
.ForMember(x => x.CALocation, a => a.Ignore())
.ForMember(x => x.Country, a => a.ResolveUsing<HaveIdValueResolver<Country, int>>().FromMember(x => x.Country);
Mapper.CreateMap<UsAddress, AddressModel>()
.ForMember(x => x.USLocation, a => a.MapFrom(Mapper.Map<UsAddress, USLocationModel>))
.ForMember(x => x.CALocation, a => a.Ignore())
.ForMember(x => x.Country, a => a.ResolveUsing<HaveIdValueResolver<Country, int>>().FromMember(x => x.Country));
Mapper.CreateMap<CanadianAddress, AddressModel>()
.ForMember(x => x.USLocation, a => a.Ignore())
.ForMember(x => x.CALocation, a => a.MapFrom(Mapper.Map<CanadianAddress, CALocationModel>))
.ForMember(x => x.Country, a => a.ResolveUsing<HaveIdValueResolver<Country, int>>().FromMember(x => x.Country));
Mapper.CreateMap<Address, LocationModel>()
.Include<UsAddress, USLocationModel>()
.Include<CanadianAddress, CALocationModel>();
Mapper.CreateMap<UsAddress, USLocationModel>();
Mapper.CreateMap<CanadianAddress, CALocationModel>();
Here's a simplified sample test:
var usAddress = new FakeUsAddress("111 L St", new FakeState(id: 17));
var addressModel = Mapper.Map<UsAddress, AddressModel>(usAddress);
Assert.IsNotNull(addressModel.USLocationModel);
Assert.IsNull(addressModel.CALocationModel);
Assert.AreEqual(usAddress.Street1, addressModel.USLocationModel.Street1);
Assert.AreEqual(usAddress.State.Id, addressModel.USLocationModel.State);
Assert.AreEqual(usAddress.Country.Id, addressModel.USLocationModel.Country);