nhibernate - Fluent NHibernate - 如何映射两个连接表中存在的不可为空的外键

标签 nhibernate fluent-nhibernate

我正在使用 Fluent NHibernate 为我的应用程序映射一组成员资格类。我将这些类映射到 asp.net 成员数据库结构。与问题相关的数据库架构如下所示:

ASPNET_USERS
UserId        PK
ApplicationId FK NOT NULL
other user columns ...

ASPNET_MEMBERSHIP
UserId        PK,FK
ApplicationID FK NOT NULL
other membership columns...

这两个表之间存在一对一的关系。我正在尝试将两个表连接在一起,并将两个表中的数据映射到单个“用户”实体,如下所示:
public class User
{
    public virtual Guid Id { get; set; }
    public virtual Guid ApplicationId { get; set; }

    // other properties to be mapped from aspnetuser/membership tables ...

我的映射文件如下:
public class UserMap : ClassMap<User>
{
    public UserMap()
    {
        Table("aspnet_Users");
        Id(user => user.Id).Column("UserId").GeneratedBy.GuidComb();
        Map(user => user.ApplicationId);
        // other user mappings

        Join("aspnet_Membership", join => {
            join.KeyColumn("UserId");
            join.Map(user => user.ApplicationId);
            // Map other things from membership to 'User' class
        }
    }
}

如果我尝试使用上面的代码运行,我会收到 FluentConfiguration 异常

Tried to add property 'ApplicationId' when already added.



如果我删除“Map(user => user.ApplicationId);”行或将其更改为“Map(user => user.ApplicationId) .Not.Update().Not.Insert(); ”然后应用程序运行,但尝试插入新的时出现以下异常用户:

Cannot insert the value NULL into column 'ApplicationId', table 'ASPNETUsers_Dev.dbo.aspnet_Users'; column does not allow nulls. INSERT fails. The statement has been terminated.



如果我保留原来的 .Map(user => user.ApplicationId) 并对 进行任何更改加入 .Map(user => user.ApplicationId) 然后我得到与上面相同的异常,当然异常与插入 aspnet_Membership 表有关

那么......假设我无法更改我的数据库架构,我该如何进行这种映射?

最佳答案

我想我得到了一些有用的东西。

  public class Application
    {
        public virtual Guid ApplicationId { get; set; }


        /* Scalar Properties of an Application  */
        public virtual string ApplicationName { get; set; }
        public virtual string Description { get; set; }

        public virtual string LoweredApplicationName 
        {
            get
            {
                return this.ApplicationName.ToLower();
            }
            set
            {
                if (String.IsNullOrEmpty(this.ApplicationName))
                {
                    this.ApplicationName = value;
                }
            } 
        }

        public virtual IList<Membership> TheManyMemberships { get; protected set; }

    }


    public class User
    {
        public virtual Guid Id { get; set; }
        public virtual Application TheApplication { get; set; }

        public virtual Membership TheMembership { get; set; }

        /* Scalar Properties of a User  */
        public virtual string UserName { get; set; }
    }


    public class Membership
    {
        private Guid UserId { get; set; }
        private User _theUser { get; set; }

        protected Membership() { }

        public Membership(User theUser)
        {
            _theUser = theUser;
        }

        public virtual Application TheApplication { get; set; }

        /* Scalar Properties of a Membership  */
        public virtual string Password { get; set; }
}





    public class ApplicationMap : ClassMap<Application>
    {
        public ApplicationMap()
        {
            Table("aspnet_Applications");
            Id(app => app.ApplicationId).Column("ApplicationId").GeneratedBy.GuidComb();
            Map(x => x.ApplicationName );
            Map(x => x.LoweredApplicationName);
            Map(x => x.Description );

            HasMany<Membership>(x => x.TheManyMemberships)
                .Inverse()
                .AsBag();
        }
    }


    public class UserMap : ClassMap<User>
    {
        public UserMap()
        {
            Table("aspnet_Users");
            Id(user => user.Id).Column("UserId").GeneratedBy.GuidComb();
            References(x => x.TheApplication, "ApplicationId")
                  .Not.Nullable();

            HasOne(x => x.TheMembership)
            .Cascade.All();//
            //.Constrained();

            Map(x => x.UserName).Not.Nullable();

        }
    }


   public class MembershipMap : ClassMap<Membership>
    {
        public MembershipMap()
        {
            Table("aspnet_Membership");

            Id(Reveal.Member<Membership>("UserId"))
                .GeneratedBy.Foreign("_theUser");
            HasOne(
              Reveal.Member<Membership, User>("_theUser"))
                    .Constrained()
                    .ForeignKey();

            References<Application>(x => x.TheApplication, "ApplicationId")
            .Not.Nullable();

            Map(x => x.Password);

        }
    }

原谅一些命名约定,在原型(prototype)设计时,我使用明确的名称而不是适当的约定以避免混淆。

我拥有的 DDL(来自上述代码)和来自 asp.net (4.0) 输出的 DDL(使用 aspnet_regsql.exe 构建 DDL)似乎是一致的(在两个版本之间)。

我要感谢这篇文章:
http://brunoreis.com/tech/fluent-nhibernate-hasone-how-implement-one-to-one-relationship/

如果您进行任何调整,请发布它们。

但我能够保存应用程序、用户和成员(member)资格。

但是,我想我可能对 User:Membership 关系有些不满意。
微软的场景似乎是“有一个用户,但允许该用户为每个应用程序使用不同的密码”,这是有道理的。
但有时在使用 MembershipProvider 代码(MS 代码,与 NHibernate 无关)时,我“感觉”有时它假设一个应用程序。

我觉得 MS DDL 应该有一个独特的约束
dbo.Membership (UserId, ApplicationId),但我在他们的 DDL 中没有看到它。

无论如何,这应该提供一些思考的食物。

关于nhibernate - Fluent NHibernate - 如何映射两个连接表中存在的不可为空的外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2679307/

相关文章:

c# - NHibernate - 从 sql 函数返回复杂对象

c# - 流利的 NHibernate、varbinary(max) 和 SQLite

.net - NHibernate:无法成功预加载

sql - 如何获取此 sql 查询并将其转换为 nhibernate 查询

c# - Web 应用程序中的 NHibernate 查询策略

asp.net-mvc-3 - nHibernate 二级缓存,带有 memcached 和随机无效强制转换

nhibernate - 使用普通 NHibernate 的 SQL 注入(inject)

nhibernate - Nhibernate 和 Fluent Nhibernate 的只读策略

nhibernate - Fluent Nhibernate 1.0 - 在类和加入的子类之间指定外键约束名称

fluent-nhibernate - 在流畅的nhibernate中设置复合键属性的类型和长度