NHibernate - 实现相同接口的多种类型

发布于 2024-12-26 18:08:13 字数 1299 浏览 3 评论 0原文

我的应用程序定义了以下类型

public interface IUser {
   int Id { get; set; }
   int UserName { get; set; }
}

public class User : IUser {
   public virtual int Id { get; set; }
   public virtual string UserName { get; set; }
}

User 类流畅地映射如下:

public class UserMap : ClassMap<User> {
    public UserMap() {
        Table("Users");
        Id(x => x.Id);
        Map(x => x.UserName);
    }
}

我想创建 IUser 的另一个实现,但我有一些限制:

  1. 新字段必须添加到与 Users 表相同的表中
  2. 要扩展的代码该类型存在于单独的项目中

例如,这是我的新类:

public class CustomUser : IUser {
   public virtual int Id { get; set; }
   public virtual string UserName { get; set; }
   public virtual string Name { get; set; }
}

映射如下:

public class CustomUserMap : ClassMap<CustomUser> {
    public CustomUserMap() {
        Table("Users");
        Id(x => x.Id);
        Map(x => x.UserName);
        Map(x => x.Name);
    }
}

我遇到的第一个问题是它不喜欢将同一接口映射两次。我通过在 Application_Start 事件中添加一些代码来克服这个问题,以便仅在不存在 CustomUserMap 类的情况下添加 UserMap 类。

然而我很快就遇到了下一个问题,如果我在另一种类型中添加对 IUser 接口的引用,它会抛出错误:

Blogs 表中的关联引用未映射的类:IUser

我可能正在做完全错误的事情,如果有人能告诉我我做错了什么,我将不胜感激。谢谢

My application has the following types defined

public interface IUser {
   int Id { get; set; }
   int UserName { get; set; }
}

public class User : IUser {
   public virtual int Id { get; set; }
   public virtual string UserName { get; set; }
}

The User class is fluently mapped like:

public class UserMap : ClassMap<User> {
    public UserMap() {
        Table("Users");
        Id(x => x.Id);
        Map(x => x.UserName);
    }
}

I'd like to create another implementation of IUser but i have a couple restrictions:

  1. The new fields must be added to the same table as the Users table
  2. The code to extend the type exists in a seperate project

For example here is my new class:

public class CustomUser : IUser {
   public virtual int Id { get; set; }
   public virtual string UserName { get; set; }
   public virtual string Name { get; set; }
}

Which is mapped like:

public class CustomUserMap : ClassMap<CustomUser> {
    public CustomUserMap() {
        Table("Users");
        Id(x => x.Id);
        Map(x => x.UserName);
        Map(x => x.Name);
    }
}

The first problem i had with this was that it did not like the same interface to be mapped twice. I overcame this by adding some code in the Application_Start event to only add the UserMap class if no CustomUserMap class existed.

However i soon ran into my next problem, if i add a reference to the IUser interface in another type, it throws the error:

An association from the table Blogs refers to an unmapped class: IUser

I'm probably going about things completely wrong and i'd appreciate it if someone could show me what i'm doing wrong. Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉生梦死 2025-01-02 18:08:13

如果你想让它们映射到同一个表,为什么不继承User呢?这也将实现 IUser 接口。

为了防止重复选择(在获取 User 时,可能会在第二次往返中获取 CustomerUser),您需要在 User 类上显式指定多态性。在 hbm 文件中,这将是多态性=“显式”,不知道流畅的 nhibernate。

If you want them to map to the same table, why don't you inherit User? That will also implement the IUser interface.

In order to prevent duplicate selects (when fetching User, it might fetch the CustomerUser in a second roundtrip), you need to specify polymorphism explicit on the User class. In hbm files this would be polymorphism="explicit", don't know about fluent nhibernate.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文