Last active
October 13, 2015 10:57
-
-
Save FransBouma/5a9e3f2bc6a8d76d0827 to your computer and use it in GitHub Desktop.
Mongo C# inheritance in subtypes failure.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//------------------------------------------------------------------------------ | |
// <auto-generated>This code was generated by LLBLGen Pro v5.0.</auto-generated> | |
//------------------------------------------------------------------------------ | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
namespace IHMMDocs.MongoDB.Persistence | |
{ | |
/// <summary>Static class for (extension) methods for fetching and projecting instances of IHMMDocs.MongoDB.DocumentClasses.BoardMember from / to the entity model.</summary> | |
public static partial class BoardMemberPersistence | |
{ | |
/// <summary>Extension method which produces a projection to IHMMDocs.MongoDB.DocumentClasses.BoardMember which instances are projected from the | |
/// results of the specified baseQuery, which returns IHMM.Adapter.EntityClasses.BoardMemberEntity instances, the root entity of the derived element returned by this query.</summary> | |
/// <param name="baseQuery">The base query to project the derived element instances from.</param> | |
/// <returns>IQueryable to retrieve IHMMDocs.MongoDB.DocumentClasses.BoardMember instances</returns> | |
public static IQueryable<IHMMDocs.MongoDB.DocumentClasses.BoardMember> ProjectToBoardMember(this IQueryable<IHMM.Adapter.EntityClasses.BoardMemberEntity> baseQuery) | |
{ | |
return baseQuery.Select(p__0 => new IHMMDocs.MongoDB.DocumentClasses.BoardMember() | |
{ | |
CompanyCar = new IHMMDocs.MongoDB.DocumentClasses.BoardMemberTypes.CompanyCar() | |
{ | |
Brand = p__0.CompanyCar.Brand, | |
CarId = p__0.CompanyCar.CarId, | |
}, | |
CompanyCarId = p__0.CompanyCarId, | |
EmployeeId = p__0.EmployeeId, | |
ManagesDepartmentId = p__0.ManagesDepartmentId, | |
ManagesDepartmentName = p__0.ManagesDepartment.Name, | |
Name = p__0.Name, | |
StartDate = p__0.StartDate, | |
WorksForDepartment = new IHMMDocs.MongoDB.DocumentClasses.BoardMemberTypes.WorksForDepartment() | |
{ | |
Employees = p__0.WorksForDepartment.Employees.OfType<IHMM.Adapter.EntityClasses.EmployeeEntity>() | |
.Where(p__1 => !(p__1 is IHMM.Adapter.EntityClasses.ManagerEntity) && | |
!(p__1 is IHMM.Adapter.EntityClasses.ClerkEntity) && | |
!(p__1 is IHMM.Adapter.EntityClasses.BoardMemberEntity)) | |
.Select(p__1 => new IHMMDocs.MongoDB.DocumentClasses.BoardMemberTypes.WorksForDepartmentTypes.Employee() | |
{ | |
Name = p__1.Name, | |
}) | |
.Union(p__0.WorksForDepartment.Employees.OfType<IHMM.Adapter.EntityClasses.ManagerEntity>() | |
.Where(p__1 => !(p__1 is IHMM.Adapter.EntityClasses.BoardMemberEntity)) | |
.Select(p__1 => new IHMMDocs.MongoDB.DocumentClasses.BoardMemberTypes.WorksForDepartmentTypes.EmployeeManager() | |
{ | |
ManagesDepartment = new IHMMDocs.MongoDB.DocumentClasses.BoardMemberTypes.WorksForDepartmentTypes.EmployeeManagerTypes.ManagesDepartment() | |
{ | |
DepartmentId = p__1.ManagesDepartment.DepartmentId, | |
DepartmentType = p__1.ManagesDepartment.DepartmentType, | |
MeetingRoomCode = p__1.ManagesDepartment.MeetingRoomCode, | |
Name = p__1.ManagesDepartment.Name, | |
}, | |
ManagesDepartmentId = p__1.ManagesDepartmentId, | |
})) | |
.Union(p__0.WorksForDepartment.Employees.OfType<IHMM.Adapter.EntityClasses.ClerkEntity>() | |
.Select(p__1 => new IHMMDocs.MongoDB.DocumentClasses.BoardMemberTypes.WorksForDepartmentTypes.EmployeeClerk() | |
{ | |
JobDescription = p__1.JobDescription, | |
})) | |
.Union(p__0.WorksForDepartment.Employees.OfType<IHMM.Adapter.EntityClasses.BoardMemberEntity>() | |
.Select(p__1 => new IHMMDocs.MongoDB.DocumentClasses.BoardMemberTypes.WorksForDepartmentTypes.EmployeeBoardMember() | |
{ | |
CompanyCarId = p__1.CompanyCarId, | |
})) | |
.ToList(), | |
Name = p__0.WorksForDepartment.Name, | |
}, | |
WorksForDepartmentId = p__0.WorksForDepartmentId, | |
}); | |
} | |
} | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//------------------------------------------------------------------------------ | |
// <auto-generated>This code was generated by LLBLGen Pro v5.0.</auto-generated> | |
//------------------------------------------------------------------------------ | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using MongoDB.Bson.Serialization; | |
namespace IHMMDocs.MongoDB.Persistence | |
{ | |
/// <summary>Static class which contains the Bson class mappings for the document classes defined in the Derived Model 'Docs'.</summary> | |
public static partial class BsonClassMappings | |
{ | |
/// <summary>Method to register all bson class mappings for the document classes of the derived model</summary> | |
public static void RegisterClassMappings() | |
{ | |
BsonClassMap.RegisterClassMap<IHMMDocs.MongoDB.DocumentClasses.BoardMember>(cm=> | |
{ | |
cm.AutoMap(); | |
cm.MapIdProperty(t=>t.EmployeeId); | |
AdditionalBoardMemberMappings(cm); | |
}); | |
BsonClassMap.RegisterClassMap<IHMMDocs.MongoDB.DocumentClasses.BoardMemberTypes.CompanyCar>(cm => cm.SetIsRootClass(true)); | |
BsonClassMap.RegisterClassMap<IHMMDocs.MongoDB.DocumentClasses.BoardMemberTypes.CompanyCarSportsCar>(); | |
BsonClassMap.RegisterClassMap<IHMMDocs.MongoDB.DocumentClasses.BoardMemberTypes.CompanyCarFamilyCar>(); | |
BsonClassMap.RegisterClassMap<IHMMDocs.MongoDB.DocumentClasses.BoardMemberTypes.WorksForDepartmentTypes.Employee>(cm => cm.SetIsRootClass(true)); | |
BsonClassMap.RegisterClassMap<IHMMDocs.MongoDB.DocumentClasses.BoardMemberTypes.WorksForDepartmentTypes.EmployeeManager>(); | |
BsonClassMap.RegisterClassMap<IHMMDocs.MongoDB.DocumentClasses.BoardMemberTypes.WorksForDepartmentTypes.EmployeeBoardMember>(); | |
BsonClassMap.RegisterClassMap<IHMMDocs.MongoDB.DocumentClasses.BoardMemberTypes.WorksForDepartmentTypes.EmployeeClerk>(); | |
BsonClassMap.RegisterClassMap<IHMMDocs.MongoDB.DocumentClasses.Clerk>(cm=> | |
{ | |
cm.AutoMap(); | |
cm.MapIdProperty(t=>t.EmployeeId); | |
AdditionalClerkMappings(cm); | |
}); | |
BsonClassMap.RegisterClassMap<IHMMDocs.MongoDB.DocumentClasses.Employee>(cm=> | |
{ | |
cm.AutoMap(); | |
cm.MapIdProperty(t=>t.EmployeeId); | |
AdditionalEmployeeMappings(cm); | |
}); | |
BsonClassMap.RegisterClassMap<IHMMDocs.MongoDB.DocumentClasses.Manager>(cm=> | |
{ | |
cm.AutoMap(); | |
AdditionalManagerMappings(cm); | |
}); | |
} | |
static partial void AdditionalBoardMemberMappings(BsonClassMap<IHMMDocs.MongoDB.DocumentClasses.BoardMember> cm); | |
static partial void AdditionalClerkMappings(BsonClassMap<IHMMDocs.MongoDB.DocumentClasses.Clerk> cm); | |
static partial void AdditionalEmployeeMappings(BsonClassMap<IHMMDocs.MongoDB.DocumentClasses.Employee> cm); | |
static partial void AdditionalManagerMappings(BsonClassMap<IHMMDocs.MongoDB.DocumentClasses.Manager> cm); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"_id": 4, | |
"CompanyCar": { | |
"_t": "CompanyCar" | |
}, | |
"CompanyCarId": 1, | |
"ManagesDepartmentId": 1, | |
"ManagesDepartmentName": "Marketing Europe", | |
"Name": "Boardmember 1", | |
"StartDate": { | |
"$date": 1198767184770 | |
}, | |
"WorksForDepartment": { | |
"Employees": [ | |
{ | |
"_t": [ | |
"Employee", | |
"EmployeeManager", | |
"EmployeeBoardMember" | |
], | |
"CompanyCarId": 1 | |
} | |
], | |
"Name": "Board Europe" | |
}, | |
"WorksForDepartmentId": 2 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* same code, json result after insert in RavenDB instead of MongoDB */ | |
{ | |
"CompanyCar": { | |
"Brand": "Fiat", | |
"CarId": 1 | |
}, | |
"CompanyCarId": 1, | |
"ManagesDepartmentId": 1, | |
"ManagesDepartmentName": "Marketing Europe", | |
"Name": "Boardmember 1", | |
"StartDate": "2007-12-27T15:53:04.7700000", | |
"WorksForDepartment": { | |
"Employees": [ | |
{ | |
"$type": "IHMMDocs.RavenDB.DocumentClasses.BoardMemberTypes.WorksForDepartmentTypes.EmployeeBoardMember, IHMMDocs.RavenDB", | |
"CompanyCarId": 1, | |
"ManagesDepartment": null, | |
"ManagesDepartmentId": null, | |
"Name": null | |
} | |
], | |
"Name": "Board Europe" | |
}, | |
"WorksForDepartmentId": 2 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private MongoClient _client; | |
private IMongoDatabase _ihDatabase; | |
[TestFixtureSetUp] | |
public void Init() | |
{ | |
BsonClassMappings.RegisterClassMappings(); | |
_client = new MongoClient("mongodb://192.168.0.54:27017"); | |
_ihDatabase = _client.GetDatabase("InheritanceOne"); | |
} | |
// ... | |
[Test] | |
public async Task InsertBoardMember() | |
{ | |
using(var adapter = new DataAccessAdapter()) | |
{ | |
var metaData = new LinqMetaData(adapter); | |
var toInsert = metaData.BoardMember.ProjectToBoardMember().FirstOrDefault(); | |
Assert.IsNotNull(toInsert); | |
var collection = _ihDatabase.GetCollection<BoardMember>("Default"); | |
await collection.DeleteOneAsync(b=>b.EmployeeId == 4); | |
await collection.InsertOneAsync(toInsert); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// this fails | |
[Test] | |
public async Task ReadBoardMember() | |
{ | |
using(var adapter = new DataAccessAdapter()) | |
{ | |
var metaData = new LinqMetaData(adapter); | |
var ob = metaData.BoardMember.ProjectToBoardMember().FirstOrDefault(); | |
Assert.IsNotNull(ob); | |
var collection = _ihDatabase.GetCollection<BoardMember>("Default"); | |
var b = await collection.Find<BoardMember>(x=>x.EmployeeId == 4).FirstOrDefaultAsync(); | |
Assert.IsNotNull(b); | |
Assert.AreEqual(ob.CompanyCarId, b.CompanyCarId); | |
Assert.AreEqual(ob.ManagesDepartmentId, b.ManagesDepartmentId); | |
Assert.AreEqual(ob.ManagesDepartmentName, b.ManagesDepartmentName); | |
Assert.AreEqual(ob.Name, b.Name); | |
Assert.AreEqual(ob.StartDate.ToUniversalTime(), b.StartDate.ToUniversalTime()); | |
Assert.AreEqual(ob.WorksForDepartmentId, b.WorksForDepartmentId); | |
Assert.AreEqual(ob.CompanyCar.Brand, b.CompanyCar.Brand); // fails, data isn't read as it's not available in DB | |
Assert.AreEqual(ob.CompanyCar.CarId, b.CompanyCar.CarId); | |
Assert.AreEqual(ob.WorksForDepartment.Name, b.WorksForDepartment.Name); | |
Assert.AreEqual(ob.WorksForDepartment.Employees[0].GetType(), b.WorksForDepartment.Employees[0].GetType()); | |
Assert.AreEqual(((IHMMDocs.MongoDB.DocumentClasses.BoardMemberTypes.WorksForDepartmentTypes.EmployeeBoardMember)ob.WorksForDepartment.Employees[0]).CompanyCarId, | |
((IHMMDocs.MongoDB.DocumentClasses.BoardMemberTypes.WorksForDepartmentTypes.EmployeeBoardMember)b.WorksForDepartment.Employees[0]).CompanyCarId); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment