The below classes in Domain Emtity:
public class SecondaryCustomerProfile
{
public int Id { get; set; }
public List<SecondaryCustomerProfileProductType> ProductTypes { get; set; }
}
public class SecondaryCustomerProfileProductType
{
public int IdField { get; set; }
public int SecondaryCustomerProfileIdField { get; set; }
public string TypeField { get; set; }
}
//The following class in WCF Service
public class SecondaryCustomerProfile
{
public int Id { get; set; }
public SecondaryCustomerProfileProductType[] ProductTypes { get; set; }
}
public class SecondaryCustomerProfileProductType
{
public int Id{ get; set; }
public int SecondaryCustomerProfileIdField { get; set; }
public string Type{ get; set; }
}
Now when I am trying to map as following...
Mapper.CreateMap<Core.DomainEntities.SecondaryCustomerProfile, Core.CustomerService.SecondaryCustomerProfile>()
.ForMember(d => d.Id, m => m.MapFrom(s => s.Id))
.ForMember(d => d.ProductTypes, m => m.MapFrom(s => s.ProductTypes));
Mapper.CreateMap<Core.DomainEntities.SecondaryCustomerProfileProductType, Core.CustomerService.SecondaryCustomerProfileProductType>()
.ForMember(d => d.Id, m => m.MapFrom(s => s.IdField))
.ForMember(d => d.SecondaryCustomerProfileId, m => m.MapFrom(s => s.SecondaryCustomerProfileIdField))
.ForMember(d => d.Type, m => m.MapFrom(s => s.TypeField));
Mapper.Map<DomainEntities.SecondaryCustomerProfile, CustomerService.SecondaryCustomerProfile>(profile);
I am getting error:
AutoMapper.AutoMapperMappingException was unhandled by user code
HResult=-2146233088
Message=Missing type map configuration or unsupported mapping.
Mapping types:
SecondaryCustomerProfile -> SecondaryCustomerProfile
DomainEntities.SecondaryCustomerProfile -> CustomerService.SecondaryCustomerProfile
Destination path:
SecondaryCustomerProfile
Source value:
DomainEntities.SecondaryCustomerProfile
Source=AutoMapper
StackTrace:
at Services\Customer\CustomerCoreService.cs:line 75
at CustomerController.Save(List`1 secondaryCustomerProfileModels) in C:\Test\CustomerController.cs:line 99
at lambda_method(Closure , ControllerBase , Object[] )
at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f()
InnerException:
.
Related
I have a method in my repository that I’m trying to test
public User UpdateUserManyToMany(User user, List<Guid> manyToManyds)
{
var dbContext = _databaseContext as DbContext;
dbContext?.TryUpdateManyToMany(user.ManyToMany, manyToManyds
.Select(x => new ManyToMany{
OtherEntityId = x,
UserId = user.Id,
}), x => x.OtherEntityId);
return user;
}
My ManyToMany Entity :
public class ManyToMany
{
public Guid OtherEntityId { get; set; }
public OtherEntity OtherEntityId { get; set; }
public Guid UserId { get; set; }
public User User { get; set; }
}
My TryUpdateManyToMany :
public static class ManyToManyExtensions
{
public static void TryUpdateManyToMany<T, TKey>(this DbContext db, IEnumerable<T> currentItems, IEnumerable<T> newItems, Func<T, TKey> getKey) where T : class
{
db.Set<T>().RemoveRange(currentItems.Except(newItems, getKey));
db.Set<T>().AddRange(newItems.Except(currentItems, getKey));
}
public static IEnumerable<T> Except<T, TKey>(this IEnumerable<T> items, IEnumerable<T> other, Func<T, TKey> getKeyFunc)
{
return items
.GroupJoin(other, getKeyFunc, getKeyFunc, (item, tempItems) => new { item, tempItems })
.SelectMany(t => t.tempItems.DefaultIfEmpty(), (t, temp) => new { t, temp })
.Where(t => ReferenceEquals(null, t.temp) || t.temp.Equals(default(T)))
.Select(t => t.t.item);
}
}
Here’s my unit test :
using (var context = new InMemoryDataBaseContext())
{
// Arrange
var repository = new UserRepository(context);
await context.Users.AddRangeAsync(GetUser());
await context.SaveChangesAsync();
// Act
var manyIds = new List<Guid>();
manyIds.Add(Guid.Parse("855d1a64-a707-40d5-ab93-34591a923abf"));
manyIds.Add(Guid.Parse("855d1a64-a787-40d9-ac93-34591a923abf"));
manyIds.Add(Guid.Parse("855d1a64-a707-41d9-ab93-39591a923abf"));
var user = new User();
var expected = repository.UpdateUserManyToMany(GetUser(), manyIds);
// Assert
}
But I get the following error in my test :
Message:
System.InvalidOperationException : The instance of entity type 'ManyToMany' cannot be tracked because another instance with the same key value for {'UserId', 'OtherEntityId'} is already being tracked. When attaching existing entities, ensure that only one entity instance with a given key value is attached. Consider using 'DbContextOptionsBuilder.EnableSensitiveDataLogging' to see the conflicting key values.
Arborescence des appels de procédure:
IdentityMap`1.ThrowIdentityConflict(InternalEntityEntry entry)
IdentityMap`1.Add(TKey key, InternalEntityEntry entry, Boolean updateDuplicate)
IdentityMap`1.Add(TKey key, InternalEntityEntry entry)
NullableKeyIdentityMap`1.Add(InternalEntityEntry entry)
StateManager.StartTracking(InternalEntityEntry entry)
InternalEntityEntry.SetEntityState(EntityState oldState, EntityState newState, Boolean acceptChanges, Boolean modifyProperties)
InternalEntityEntry.SetEntityState(EntityState entityState, Boolean acceptChanges, Boolean modifyProperties, Nullable`1 forceStateWhenUnknownKey)
EntityGraphAttacher.PaintAction(EntityEntryGraphNode`1 node)
EntityEntryGraphIterator.TraverseGraph[TState](EntityEntryGraphNode`1 node, Func`2 handleNode)
EntityGraphAttacher.AttachGraph(InternalEntityEntry rootEntry, EntityState targetState, EntityState storeGeneratedWithKeySetTargetState, Boolean forceStateWhenUnknownKey)
DbContext.SetEntityState(InternalEntityEntry entry, EntityState entityState)
DbContext.RemoveRange(IEnumerable`1 entities)
InternalDbSet`1.RemoveRange(IEnumerable`1 entities)
ManyToManyExtensions.TryUpdateManyToMany[T,TKey](DbContext db, IEnumerable`1 currentItems, IEnumerable`1 newItems, Func`2 getKey) ligne 24
UserRepository.UpdateUserManyToMany(User user, List`1 manyToManyds) ligne 59
MyRepoUnitTest.MyTestMethod() ligne 102
--- End of stack trace from previous location where exception was thrown ```
The following sample program, that is based on the code you provided, works without issues:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
namespace IssueConsoleTemplate
{
public class User
{
public Guid Id { get; set; }
public string Name { get; set; }
public ICollection<ManyToMany> ManyToMany { get; set; } = new HashSet<ManyToMany>();
}
public class OtherEntity
{
public Guid Id { get; set; }
public string Name { get; set; }
public ICollection<ManyToMany> ManyToMany { get; set; } = new HashSet<ManyToMany>();
}
public class ManyToMany
{
public Guid OtherEntityId { get; set; }
public Guid UserId { get; set; }
public OtherEntity OtherEntity { get; set; }
public User User { get; set; }
}
public class Context : DbContext
{
public DbSet<OtherEntity> OtherEntities { get; set; }
public DbSet<User> Users { get; set; }
public DbSet<ManyToMany> ManyToMany { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder
.UseSqlServer(#"Data Source=.\MSSQL14;Integrated Security=SSPI;Initial Catalog=So63077461")
.UseLoggerFactory(
LoggerFactory.Create(
b => b
.AddConsole()
.AddFilter(level => level >= LogLevel.Information)))
.EnableSensitiveDataLogging()
.EnableDetailedErrors();
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<OtherEntity>(
entity =>
{
entity.HasKey(e => e.Id);
entity.HasMany(e => e.ManyToMany)
.WithOne(e => e.OtherEntity)
.HasForeignKey(e => e.OtherEntityId);
entity.HasData(
new OtherEntity
{
Id = new Guid("855d1a64-a707-40d5-ab93-34591a923abf"),
Name = "Bicycle"
},
new OtherEntity
{
Id = new Guid("855d1a64-a787-40d9-ac93-34591a923abf"),
Name = "Bus"
},
new OtherEntity
{
Id = new Guid("855d1a64-a707-41d9-ab93-39591a923abf"),
Name = "Plane"
});
});
modelBuilder.Entity<User>(
entity =>
{
entity.HasKey(e => e.Id);
entity.HasMany(e => e.ManyToMany)
.WithOne(e => e.User)
.HasForeignKey(e => e.UserId);
});
modelBuilder.Entity<ManyToMany>(
entity =>
{
entity.HasKey(e => new {e.OtherEntityId, e.UserId});
});
}
}
public static class ManyToManyExtensions
{
public static void TryUpdateManyToMany<T, TKey>(this DbContext db, IEnumerable<T> currentItems, IEnumerable<T> newItems, Func<T, TKey> getKey) where T : class
{
db.Set<T>().RemoveRange(currentItems.Except(newItems, getKey));
db.Set<T>().AddRange(newItems.Except(currentItems, getKey));
}
public static IEnumerable<T> Except<T, TKey>(this IEnumerable<T> items, IEnumerable<T> other, Func<T, TKey> getKeyFunc)
{
return items
.GroupJoin(other, getKeyFunc, getKeyFunc, (item, tempItems) => new { item, tempItems })
.SelectMany(t => t.tempItems.DefaultIfEmpty(), (t, temp) => new { t, temp })
.Where(t => ReferenceEquals(null, t.temp) || t.temp.Equals(default(T)))
.Select(t => t.t.item);
}
}
internal class UserRepository
{
private readonly Context _databaseContext;
public UserRepository(Context context)
{
_databaseContext = context;
}
public User UpdateUserManyToMany(User user, List<Guid> manyToManyds)
{
var dbContext = _databaseContext as DbContext;
dbContext?.TryUpdateManyToMany(user.ManyToMany, manyToManyds
.Select(x => new ManyToMany{
OtherEntityId = x,
UserId = user.Id,
}), x => x.OtherEntityId);
return user;
}
}
internal static class Program
{
private static async Task Main()
{
//
// Operations with referential integrity intact:
//
using var context = new Context();
context.Database.EnsureDeleted();
context.Database.EnsureCreated();
// Arrange
var repository = new UserRepository(context);
await context.Users.AddRangeAsync(GetUser());
await context.SaveChangesAsync();
// Act
var manyIds = new List<Guid>
{
new Guid("855d1a64-a707-40d5-ab93-34591a923abf"),
new Guid("855d1a64-a787-40d9-ac93-34591a923abf"),
new Guid("855d1a64-a707-41d9-ab93-39591a923abf")
};
var expected = repository.UpdateUserManyToMany(GetUser(), manyIds);
}
private static User GetUser()
=> User;
private static readonly User User = new User
{
Id = new Guid("30c35d2e-77fd-480b-9974-6ebf037a8f86"),
Name = "John"
};
}
}
System.InvalidOperationException : The instance of entity type 'ManyToMany' cannot be tracked because another instance with the same key value for {'UserId', 'OtherEntityId'} is already being tracked. When attaching existing entities, ensure that only one entity instance with a given key value is attached. Consider using 'DbContextOptionsBuilder.EnableSensitiveDataLogging' to see the conflicting key values.
The error message says, that at least one of your ManyToMany entries already exists in the database (there is an entry with the same UserId and OtherEntityId combination).
You can verify this, by running the following code directly after your filled the manyIds variable with the 3 IDs:
var user = GetUser();
var alreadyExistingManyToMany = context.ManyToMany
.Where(m => m.UserId == user.Id &&
manyIds.Contains(m.OtherEntityId))
.ToList();
Debug.Assert(alreadyExistingManyToMany.Count == 0);
I implemented objects like below in ASP.NET Core 3.1:
public class Content: BaseModel
{
public int ContentId { get; set; }
public virtual List<ContentCBlock> ContentCBlocks { get; set; } = new List<ContentCBlock>();
}
public class CBlock : BaseModel
{
public int CBlockId { get; set; }
public virtual List<ContentCBlock> ContentCBlocks { get; set; } = new List<ContentCBlock>();
}
public class ContentCBlock
{
[Key]
public int ContentId { get; set; }
public virtual Content Content { get; set; }
[Key]
public int CBlockId { get; set; }
public virtual CBlock CBlock { get; set; }
public int DisplayOrder { get; set; }
}
and DBContext
modelBuilder.Entity<ContentCBlock>().HasKey(t => new { t.ContentId, t.CBlockId });
modelBuilder.Entity<ContentCBlock>()
.HasOne(c => c.CBlock)
.WithMany(c => c.ContentCBlocks)
.HasForeignKey(cc => cc.CBlockId);
modelBuilder.Entity<ContentCBlock>()
.HasOne(c => c.Content)
.WithMany(c => c.ContentCBlocks)
.HasForeignKey(cc => cc.ContentId);
DBSets:
public DbSet<ContentCBlock> ContentCBlocks { get; set; }
public DbSet<Content> Contents { get; set; }
public DbSet<CBlock> CBlocks { get; set; }
Everything works fine, relations, lazyLoading, ... everything.
The question is How I can sort a List of Contents by ContentCBlock.DisplayOrder
P.S.: I cannot change classes
Hope this will help you.
_context.Contents.OrderBy(c => c.ContentCBlocks.
OrderBy(c => c.DisplayOrder).Select(c => c.DisplayOrder).FirstOrDefault());
I am attempting to configure 2 classes fluently.
public class Company
{
[Key]
public int Id {get; set; }
public string Name { get; set; }
public List<CompanyOwnership> OwnedBy { get; set; }
}
public class CompanyOwnership
{
public static void Configure(ModelBuilder modelBuilder)
{
modelBuilder.Entity<CompanyOwnership>()
.HasOne(cpo => cpo.OwnedCompany)
.WithMany(cp => cp.OwnedBy)
.HasForeignKey(cpo => cpo.OwnedCompanyId);
modelBuilder.Entity<CompanyOwnership>()
.HasOne(cpo => cpo.OwningCompany)
.WithMany()
.HasForeignKey(cpo => cpo.OwningCompanyId);
}
[Key]
public int Id {get; set; }
public int OwnedCompanyId { get; set; }
public Company OwnedCompany { get; set; }
public int OwningCompanyId { get; set; }
public Company OwningCompany { get; set; }
public decimal Percentage { get; set; }
}
The above code will result in an error:
InvalidOperationException: Unable to determine the relationship
represented by navigation property 'Company.OwnedBy' of type
'List<CompanyOwnership>'. Either manually configure the relationship,
or ignore this property using the '[NotMapped]' attribute or by using
'EntityTypeBuilder.Ignore' in 'OnModelCreating'.
Could I get some input about why the above setup is not enough?
Thank you,
Nvm,
it turned out that I forgot to call Configure(...).
It is working fine now.
I have a domain class:
public class Agencia : IEntity
{
public virtual int Id { get; set; }
public virtual string Nome { get; set; }
public virtual string Identificacao { get; set; }
public virtual IList<Pessoa> Gerentes { get; protected set; }
public Agencia()
{
Gerentes = new List<Pessoa>();
}
public virtual void AddGerente(Pessoa gerente)
{
Gerentes.Add(gerente);
}
public virtual void AddGerentes(params Pessoa[] gerentes)
{
Parallel.ForEach(gerentes, (pessoa) => Gerentes.Add(pessoa));
}
}
public class Pessoa: IEntity
{
public virtual int Id { get; set; }
public virtual string Nome { get; set; }
}
With this convention (defined as set AsSet)
public class AgenciaConvention : IAutoMappingOverride<Agencia>
{
public void Override(AutoMapping<Agencia> mapping)
{
mapping.HasManyToMany(a => a.Gerentes).Cascade.AllDeleteOrphan().AsSet().Not.Inverse();
}
}
When I run this test:
[TestMethod]
[Description("Uma agência tem vários gerêntes")]
public void AgenciaTemVariosGerentes()
{
// Arrange
var fix = new Fixture();
var currentUser = GetLoggedUser();
// Create a List<Pessoa>
var gerentes = fix.Build<Pessoa>()
.With(p => p.Nome)
.With(p => p.CPF)
.With(p => p.CreateBy, currentUser)
.OmitAutoProperties()
.CreateMany<Pessoa>(10).ToList();
// Action
new PersistenceSpecification<Agencia>(Session)
.CheckProperty(p => p.Nome, fix.Create<string>().Truncate(80))
.CheckProperty(p => p.Identificacao, fix.Create<string>().Truncate(10))
.CheckReference(p => p.Regional,
fix.Build<Regional>()
.With(p => p.Nome)
.OmitAutoProperties()
.Create()
, new IDEqualityComparer())
.CheckList(p => p.Gerentes, gerentes, new IDEqualityComparer())
.CheckReference(p => p.CreateBy, currentUser, new IDEqualityComparer())
.VerifyTheMappings(); // Assert
}
How can I test this list?
The collection should be AsSet, it necessary that the Parent and Children fields are PK, FK
Full Error:
Test Name: AgenciaTemVariosGerentes
Test FullName: {OMMITED}.Integration.Test.AgenciaTest.AgenciaTemVariosGerentes
Test Source: {OMMITED}.Integration.Test\AgenciaTest.cs : line 22
Test Outcome: Failed
Test Duration: 0:00:02,4093555
Result Message:
Test method {OMMITED}.Integration.Test.AgenciaTest.AgenciaTemVariosGerentes threw exception:
NHibernate.PropertyAccessException: Invalid Cast (check your mapping for property type mismatches); setter of CreditoImobiliarioBB.Model.Regional ---> System.InvalidCastException: Unable to cast object of type 'NHibernate.Collection.Generic.PersistentGenericSet1[CreditoImobiliarioBB.Model.Pessoa]' to type 'System.Collections.Generic.IList1[CreditoImobiliarioBB.Model.Pessoa]'.
Result StackTrace:
at (Object , Object[] , SetterCallback )
at NHibernate.Bytecode.Lightweight.AccessOptimizer.SetPropertyValues(Object target, Object[] values)
at NHibernate.Tuple.Entity.PocoEntityTuplizer.SetPropertyValuesWithOptimizer(Object entity, Object[] values)
--- End of inner exception stack trace ---
at NHibernate.Tuple.Entity.PocoEntityTuplizer.SetPropertyValuesWithOptimizer(Object entity, Object[] values)
at NHibernate.Tuple.Entity.PocoEntityTuplizer.SetPropertyValues(Object entity, Object[] values)
at NHibernate.Persister.Entity.AbstractEntityPersister.SetPropertyValues(Object obj, Object[] values, EntityMode entityMode)
at NHibernate.Event.Default.AbstractSaveEventListener.PerformSaveOrReplicate(Object entity, EntityKey key, IEntityPersister persister, Boolean useIdentityColumn, Object anything, IEventSource source, Boolean requiresImmediateIdAccess)
at NHibernate.Event.Default.AbstractSaveEventListener.PerformSave(Object entity, Object id, IEntityPersister persister, Boolean useIdentityColumn, Object anything, IEventSource source, Boolean requiresImmediateIdAccess)
at NHibernate.Event.Default.AbstractSaveEventListener.SaveWithGeneratedId(Object entity, String entityName, Object anything, IEventSource source, Boolean requiresImmediateIdAccess)
at NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.SaveWithGeneratedOrRequestedId(SaveOrUpdateEvent event)
at NHibernate.Event.Default.DefaultSaveEventListener.SaveWithGeneratedOrRequestedId(SaveOrUpdateEvent event)
at NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.EntityIsTransient(SaveOrUpdateEvent event)
at NHibernate.Event.Default.DefaultSaveEventListener.PerformSaveOrUpdate(SaveOrUpdateEvent event)
at NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.OnSaveOrUpdate(SaveOrUpdateEvent event)
at NHibernate.Impl.SessionImpl.FireSave(SaveOrUpdateEvent event)
at NHibernate.Impl.SessionImpl.Save(Object obj)
at FluentNHibernate.Testing.PersistenceSpecification1.TransactionalSave(Object propertyValue)
at FluentNHibernate.Testing.Values.ReferenceProperty2.HasRegistered(PersistenceSpecification1 specification)
at FluentNHibernate.Testing.PersistenceSpecification1.RegisterCheckedProperty(Property1 property, IEqualityComparer equalityComparer)
at FluentNHibernate.Testing.PersistenceSpecificationExtensions.CheckReference[T](PersistenceSpecification1 spec, Expression`1 expression, Object propertyValue, IEqualityComparer propertyComparer)
at CreditoImobiliarioBB.Repository.Integration.Test.AgenciaTest.AgenciaTemVariosGerentes() in {OMMITED}.Integration.Test\AgenciaTest.cs:line 27
Thanks.
Sets don't implement IList<T>.
Define your properties as ICollection<T> instead.
I have situation where I DO require one-to-one relationship thus the usage of HasOne. Yes I do want to have separate table for Patrons and Members. Following are my classes and their corresponding mapping classes.
public class Member
{
public virtual string ID { get; set; }
public virtual bool IsRegistered { get; set; }
public virtual Patron Patron { get; set; }
public virtual string Reference1 { get; set; }
public virtual string Reference2 { get; set; }
}
public class Patron
{
public virtual string ID { get; set; }
public virtual string FirstName { get; set; }
public virtual string LastName { get; set; }
public virtual string Address { get; set; }
public virtual int Telephone1 { get; set; }
public virtual int Telephone2 { get; set; }
public virtual int Age { get; set; }
public virtual string Occupation { get; set; }
public virtual string Gender { get; set; }
public virtual string Room { get; set; }
}
public class PatronMap : ClassMap<Patron>
{
public PatronMap()
{
Id(x => x.ID);
Map(x => x.FirstName);
Map(x => x.LastName);
Map(x => x.Gender);
Map(x => x.Age);
Map(x => x.Address);
Map(x => x.Occupation);
Map(x => x.Telephone1);
Map(x => x.Telephone2);
Map(x => x.Room);
Table("Patrons");
}
}
public class MemberMap : ClassMap<Member>
{
public MemberMap()
{
Id(x => x.ID);
HasOne(x => x.Patron).PropertyRef(x => x.ID).Constrained();
Map(x => x.Reference1);
Map(x => x.Reference2);
Map(x => x.IsRegistered);
Table("Members");
}
}
I also have a little test, as below, to see if the things are mapped correctly or not.
[TestMethod]
public void MemberMap_Create_Success()
{
new PersistenceSpecification<Member>( Database.Session, new CustomEqualityComparer() )
.CheckProperty(x => x.ID, "1")
//I'm not quite sure about the following mapping check. How do I do HasOne check here? :-(
.CheckReference(x => x.Patron, new Patron()
{
ID = "2",
FirstName = "Foo",
LastName = "Bar",
Gender = "M",
Age = 59,
Address = "City, Coutnry",
Telephone1 = 0123456789,
Telephone2 = 0987654321,
Occupation = "Learning Fluent nHibernate",
Room = "Somewhere"
})
.CheckProperty(x => x.Reference1, "Ref1")
.CheckProperty(x => x.Reference2, "Ref2")
.CheckProperty(x => x.IsRegistered, true)
.VerifyTheMappings();
}
I'm not sure how to test HasOne mapping property in this test. Any help is appreciated. Thanks in advance.
Following is what I did to implement HasOne mapping and unit test. Maps are as followed:
public class PatronMap : ClassMap<Patron>
{
public PatronMap()
{
Id(x => x.ID);
Map(x => x.FirstName);
Map(x => x.LastName);
Map(x => x.Gender);
Map(x => x.Age);
Map(x => x.Address);
Map(x => x.Occupation);
Map(x => x.Telephone1);
Map(x => x.Telephone2);
Map(x => x.AshramRoom);
HasOne(x => x.Member).ForeignKey();
Table("Patrons");
}
}
public class MemberMap : ClassMap<Member>
{
public MemberMap()
{
Id(x => x.ID);
Map(x => x.IsRegistered);
Map(x => x.Reference1);
Map(x => x.Reference2);
References(x => x.Patron)
.Column("PatronID")
.Unique()
.UniqueKey("IDX_UniquePatronID");
Table("Members");
}
}
Unit test is as thus:
public void MemberMap_Create_Success()
{
new PersistenceSpecification<Member>( Database.Session, new CustomEqualityComparer() )
.CheckProperty(x => x.ID, "1")
.CheckReference(x => x.Patron, new Patron()
{
ID = "2",
FirstName = "Abc",
LastName = "Xyz",
Gender = "M",
Age = 99,
Address = "Address",
Telephone1 = 0000000001,
Telephone2 = 1000000000,
Occupation = "Occupation",
AshramRoom = "Room"
})
.CheckProperty(x => x.Reference1, "Ref1")
.CheckProperty(x => x.Reference2, "Ref2")
.CheckProperty(x => x.IsRegistered, true)
.VerifyTheMappings();
}
Hope this helps someone. :-)
I know it's been a while but if it helps.
You can use:
var patrol = new Patron()
{
ID = "2",
FirstName = "Foo",
LastName = "Bar",
Gender = "M",
Age = 59,
Address = "City, Coutnry",
Telephone1 = 0123456789,
Telephone2 = 0987654321,
Occupation = "Learning Fluent nHibernate",
Room = "Somewhere"
};
...
.CheckReference(x => x.Patron, patrol)
.CheckProperty(x => x.Reference1, patrol)
.CheckProperty(x => x.Reference2, patrol)
See: https://github.com/jagregory/fluent-nhibernate/wiki/Persistence-specification-testing