简体   繁体   English

c#Fluent nHibernate主键是外键

[英]c# Fluent nHibernate primary key is foreign key

My database layout looks like this: 我的数据库布局如下所示:

Users
------------
|UserId    | <-PK
|FirstName |
|LastName  |
|MiddleName|
|Email     |
------------

Admins
------------
|UserId    | <-PK/FK to Users
------------

I Just want the ability to put in the unique ID to the Admins table to designate if that user is an Admin. 我只是希望能够在Admins表中输入唯一ID来指定该用户是否为Admin。

Here is my Fluent nHibernate code: 这是我的Fluent nHibernate代码:

public class Users
{
    public virtual string UserId { get; set; }
    public virtual string FirstName { get; set; }
    ....
}

public class UserMappings : ClassMap<Users>
{
    public UserMappings()
    {
        Id(x => x.UserId).Column("UserId").Not.Nullable();

        Map(x => x.FirstName).Column("FirstName").Not.Nullable();
        Map(x => x.MiddleName).Column("MiddleName").Nullable();
        ....
    }
}

public class Admins
{
    public virtual Users User { get; set; }
}

public class AdminMappings : ClassMap<Admins>
{
    public AdminMappings()
    {
        Id(x => x.User).GeneratedBy.Foreign("Users");

        HasOne(x => x.User).Constrained();
    }
}

Unfortunately, when I run, I get this error: 不幸的是,当我跑步时,我收到此错误:

Could not determine type for: SMAC.Users, SMAC, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null, for columns: NHibernate.Mapping.Column(User)

What I want is that if I do: 我想要的是,如果我这样做:

var admin;
admin.user <= instance of user to get name, email, etc...

How would I go about accomplishing this? 我将如何实现这一目标?

EDIT: I added this and I got it working though I don't know if its ideal: 编辑:我添加了这个,虽然我不知道它是否理想,但它让它工作了:

public AdminMappings()
{
    Id(x => x.UserId).Column("UserId");
    HasOne(x => x.User).Constrained();
}

public UserMappings()
{
    Id(x => x.UserId).Column("UserId").Not.Nullable();
    Map(x => x.FirstName).Column("FirstName").Not.Nullable();
    Map(x => x.MiddleName).Column("MiddleName").Nullable();
    ...
    HasOne(x => x.Admin).Cascade.All();
}

public class Admins
{
    public virtual string UserId { get; set; }

    public virtual Users User { get; set; }
}

public class Users
{
    public virtual string UserId { get; set; }
    public virtual string FirstName { get; set; }
    ....
    public virtual Admins Admin { get; set; }
}

The only caveat is that I have to actually set the UserId and the User in order to save the Admin record rather then just the UserId and have it automatically pull the User record for me. 唯一需要注意的是,我必须实际设置UserId和User才能保存Admin记录,而不仅仅是UserId,并让它自动为我提取用户记录。

NHibernate can handle FK in primary keys NHibernate可以在主键中处理FK

public class AdminMappings : ClassMap<Admins>
{
    public AdminMappings()
    {
        CompositeId().KeyReference(x => x.User, "UserId");
    }
}

Don't forget to implement equals and gethashcode on Admin appropiatly to use User.Id 不要忘记在Admin上适当地使用equals和gethashcode来使用User.Id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM