(流畅)nhibernate条件表映射策略
我无法控制数据库模式并具有以下(简化的)表结构:
- CityProfile
- 身份证
- 姓名
- CountryProfile
- 身份证
- 姓名
- RegionProfile
- 身份证
- 姓名
我有一个 .Net 枚举和类封装了很多内容:
public enum Scope { Region, Country, City }
public class Profile {
public Scope Scope { get; set; }
public int Id { get; set; }
public string Name { get; set; }
}
我正在寻找一种允许我映射到正确表的机制,例如:
public class ProfileMap : ClassMap<Profile> {
public ProfileMap() {
switch (x => x.Scope) { // <--Invalid code here!
case Scope.City: Table("CityProfile"); break;
case Scope.Country: Table("CountryProfile"); break;
case Scope.Region: Table("RegionProfile"); break;
}
Id(x => x.Id);
Map(x => x.Name);
}
}
或者我是否采取了错误的方法?
I have no control over database schema and have the following (simplified) table structure:
- CityProfile
- Id
- Name
- CountryProfile
- Id
- Name
- RegionProfile
- Id
- Name
I have a .Net enum and class encapsulating the lot:
public enum Scope { Region, Country, City }
public class Profile {
public Scope Scope { get; set; }
public int Id { get; set; }
public string Name { get; set; }
}
I am looking for a mechanism that allows me to map to the correct table, something like:
public class ProfileMap : ClassMap<Profile> {
public ProfileMap() {
switch (x => x.Scope) { // <--Invalid code here!
case Scope.City: Table("CityProfile"); break;
case Scope.Country: Table("CountryProfile"); break;
case Scope.Region: Table("RegionProfile"); break;
}
Id(x => x.Id);
Map(x => x.Name);
}
}
Or have I approached this wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
鉴于数据库架构是固定的,我会将它们映射为 3 个单独的类,并作为 映射到公共接口任何参考。
Given that the database schema is fixed, I would map these as 3 separate classes and map to the common interface as an any reference.