public class Company : Entity
{ }
public class Factory : Entity
{ }
public class CompanyDBContext : MongoDBContext
{
public CompanyDBContext() : base("CompanyDBContext") { }
public override void OnRegisterModel(ITypeRegistration registration)
{
registration.RegisterType().RegisterType(); //一次注册
}
}
public class FactoryDBContext : MongoDBContext
{
public FactoryDBContext() : base("FactoryDBContext") { }
public override void OnRegisterModel(ITypeRegistration registration)
{
registration.RegisterType(); //二次注册
}
}
//注册上下文
MongoDBRepository.RegisterMongoDBContext(new CompanyDBContext());
MongoDBRepository.RegisterMongoDBContext(new FactoryDBContext()); //因Factory进行了多次注册,在此会抛出类型多次注册异常
如果需要使用文件,那么文件实体也一样需要注册,这在之前的版本并非强制,也存在一定的问题:
public class MyFile : MongoFile
{
public MyFile()
: base(@"c:\testxml.xml", "test.xml", "xml")
{ }
}
public class TestDBContext : MongoDBContext
{
public TestDBContext() : base("TestDBContext") { }
public override void OnRegisterModel(ITypeRegistration registration)
{
registration.RegisterType(); //注册文件实体
}
}