Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Fluent NHibernate:如何使用HasMany设置外键、插入外键或更新外键?_C#_Sql Server_Database_Nhibernate_Fluent Nhibernate - Fatal编程技术网

C# Fluent NHibernate:如何使用HasMany设置外键、插入外键或更新外键?

C# Fluent NHibernate:如何使用HasMany设置外键、插入外键或更新外键?,c#,sql-server,database,nhibernate,fluent-nhibernate,C#,Sql Server,Database,Nhibernate,Fluent Nhibernate,一点背景: 我有许多SomeCollection实例,它们存储了许多需要持久化到数据库中的规则信息。数据库中的每个规则都有一个自动递增的ID(PK)和一个自定义规则ID。规则表还包含一个外键,它是集合的ID。规则表包含对RuleId和CollectionId的唯一约束 问题: 当映射到HasMany时,外键是如何插入到表中的,插入子项后是否更新?我在下面的消息中显示为null,但我知道值已设置 如果以上是正确的或错误的,我如何实施多列唯一约束并避免下面详述的问题 当我在SomeCollecti

一点背景:

我有许多
SomeCollection
实例,它们存储了许多需要持久化到数据库中的
规则
信息。数据库中的每个
规则都有一个自动递增的ID(PK)和一个自定义
规则ID
规则
表还包含一个外键,它是集合的ID。
规则
表包含对
RuleId
CollectionId
的唯一约束


问题:

  • 当映射到
    HasMany
    时,外键是如何插入到表中的,插入子项后是否更新?我在下面的消息中显示为null,但我知道值已设置

  • 如果以上是正确的或错误的,我如何实施多列唯一约束并避免下面详述的问题

  • 当我在
    SomeCollectionMapping
    中设置强制唯一约束时,我会遇到以下冲突:

    检测到FCE:System.Data.SqlClient.SqlException(0x80131904): 违反唯一密钥约束“UQ_uuusupporte_uuu4d7abd34dc8c8a20”。 无法在对象“dbo.rules”中插入重复的键。这个 重复的键值为(123,NULL)

    正在尝试插入的数据是:

    123, myId1 
    123, myId2
    
    如果省略以下行:
    part.Not.Nullable()
    part.UniqueKey(“uniqueRef”)
    数据将成功插入两个表中,但不会强制执行唯一性

    即使使用Fluent NHibernate插入失败,我也能够使用Microsoft SQl Server Management Studio插入数据,而且多列约束工作正常



    SomeCollection.cs

    internal class SomeCollection
    {
        public SomeCollection()
        {
            this.CollectionId = string.Empty;
            this.CollectionName = string.Empty;
            this.Rules = new List<Rule>();
        }
    
        public virtual string CollectionId { get; set; }
    
        public virtual string CollectionName { get; set; }
    
        public virtual IList<Rule> Rules { get; set; }
    }
    
    internal class Rule
    {
        public Rule()
        {
            this.DisplayName = string.Empty;
            this.RuleId = string.Empty;
            this.MyValue = 0;
        }
    
        public virtual string DisplayName { get; set; }
    
        public virtual string RuleId { get; set; }
    
        public virtual int MyValue { get; set; }
    }
    
    internal sealed class SomeCollectionMapping : ClassMap<SomeCollection>
    {
        private const string TableName = "some_collections";
    
        public SomeCollectionMapping()
        {
            this.Table(TableName);
    
            this.Id(x => x.CollectionId)
                .Not.Nullable()
                .Column("collectionId")
                .UniqueKey("abc123");
    
            this.Map(x => x.CollectionName)
                .Column("name")
                .Not.Nullable();
    
            this.HasMany<Rule>(x => x.Rules)
                .KeyColumns.Add("collectionId", part =>
                {
                    // No violations if I omit these two lines
                    part.Not.Nullable();
                    part.UniqueKey("uniqueRef");
                })
                .Cascade.All();
        }
    }
    
    internal sealed class RuleMapping : ClassMap<Rule>
    {
        private const string TableName = "rules";
    
        public RuleMapping()
        {
            this.Table(TableName);
    
            this.Id().GeneratedBy.Increment().Unique();
    
            this.Map(x => x.RuleId)
                .Not.Nullable()
                .UniqueKey("uniqueRef")
                .Column("ruleId");
    
            this.Map(x => x.DisplayName)
                .Column("name")
                .Not.Nullable();
    
            this.Map(x => x.MyValue)
                .Column("myValue")
                .Not
                .Nullable();
        }
    }
    
    SomeCollectionMapping.cs

    internal class SomeCollection
    {
        public SomeCollection()
        {
            this.CollectionId = string.Empty;
            this.CollectionName = string.Empty;
            this.Rules = new List<Rule>();
        }
    
        public virtual string CollectionId { get; set; }
    
        public virtual string CollectionName { get; set; }
    
        public virtual IList<Rule> Rules { get; set; }
    }
    
    internal class Rule
    {
        public Rule()
        {
            this.DisplayName = string.Empty;
            this.RuleId = string.Empty;
            this.MyValue = 0;
        }
    
        public virtual string DisplayName { get; set; }
    
        public virtual string RuleId { get; set; }
    
        public virtual int MyValue { get; set; }
    }
    
    internal sealed class SomeCollectionMapping : ClassMap<SomeCollection>
    {
        private const string TableName = "some_collections";
    
        public SomeCollectionMapping()
        {
            this.Table(TableName);
    
            this.Id(x => x.CollectionId)
                .Not.Nullable()
                .Column("collectionId")
                .UniqueKey("abc123");
    
            this.Map(x => x.CollectionName)
                .Column("name")
                .Not.Nullable();
    
            this.HasMany<Rule>(x => x.Rules)
                .KeyColumns.Add("collectionId", part =>
                {
                    // No violations if I omit these two lines
                    part.Not.Nullable();
                    part.UniqueKey("uniqueRef");
                })
                .Cascade.All();
        }
    }
    
    internal sealed class RuleMapping : ClassMap<Rule>
    {
        private const string TableName = "rules";
    
        public RuleMapping()
        {
            this.Table(TableName);
    
            this.Id().GeneratedBy.Increment().Unique();
    
            this.Map(x => x.RuleId)
                .Not.Nullable()
                .UniqueKey("uniqueRef")
                .Column("ruleId");
    
            this.Map(x => x.DisplayName)
                .Column("name")
                .Not.Nullable();
    
            this.Map(x => x.MyValue)
                .Column("myValue")
                .Not
                .Nullable();
        }
    }
    
    内部密封类SomeCollectionMapping:ClassMap
    {
    private const string TableName=“some_collections”;
    公共SomeCollectionMapping()
    {
    此.Table(TableName);
    this.Id(x=>x.CollectionId)
    .Not.Nullable()
    .列(“集合ID”)
    .UniqueKey(“abc123”);
    this.Map(x=>x.CollectionName)
    .栏(“名称”)
    .Not.Nullable();
    this.HasMany(x=>x.Rules)
    .KeyColumns.Add(“collectionId”,部分=>
    {
    //如果我省略这两行,就没有违规行为
    part.Not.Nullable();
    第.部分UniqueKey(“uniqueRef”);
    })
    .Cascade.All();
    }
    }
    
    RuleMapping.cs

    internal class SomeCollection
    {
        public SomeCollection()
        {
            this.CollectionId = string.Empty;
            this.CollectionName = string.Empty;
            this.Rules = new List<Rule>();
        }
    
        public virtual string CollectionId { get; set; }
    
        public virtual string CollectionName { get; set; }
    
        public virtual IList<Rule> Rules { get; set; }
    }
    
    internal class Rule
    {
        public Rule()
        {
            this.DisplayName = string.Empty;
            this.RuleId = string.Empty;
            this.MyValue = 0;
        }
    
        public virtual string DisplayName { get; set; }
    
        public virtual string RuleId { get; set; }
    
        public virtual int MyValue { get; set; }
    }
    
    internal sealed class SomeCollectionMapping : ClassMap<SomeCollection>
    {
        private const string TableName = "some_collections";
    
        public SomeCollectionMapping()
        {
            this.Table(TableName);
    
            this.Id(x => x.CollectionId)
                .Not.Nullable()
                .Column("collectionId")
                .UniqueKey("abc123");
    
            this.Map(x => x.CollectionName)
                .Column("name")
                .Not.Nullable();
    
            this.HasMany<Rule>(x => x.Rules)
                .KeyColumns.Add("collectionId", part =>
                {
                    // No violations if I omit these two lines
                    part.Not.Nullable();
                    part.UniqueKey("uniqueRef");
                })
                .Cascade.All();
        }
    }
    
    internal sealed class RuleMapping : ClassMap<Rule>
    {
        private const string TableName = "rules";
    
        public RuleMapping()
        {
            this.Table(TableName);
    
            this.Id().GeneratedBy.Increment().Unique();
    
            this.Map(x => x.RuleId)
                .Not.Nullable()
                .UniqueKey("uniqueRef")
                .Column("ruleId");
    
            this.Map(x => x.DisplayName)
                .Column("name")
                .Not.Nullable();
    
            this.Map(x => x.MyValue)
                .Column("myValue")
                .Not
                .Nullable();
        }
    }
    
    内部密封类规则映射:类映射
    {
    private const string TableName=“rules”;
    公共规则映射()
    {
    此.Table(TableName);
    this.Id().GeneratedBy.Increment().Unique();
    this.Map(x=>x.RuleId)
    .Not.Nullable()
    .UniqueKey(“uniqueRef”)
    .列(“规则ID”);
    this.Map(x=>x.DisplayName)
    .栏(“名称”)
    .Not.Nullable();
    this.Map(x=>x.MyValue)
    .列(“myValue”)
    不
    .Nullable();
    }
    }
    
    Nhibernate 4.1.1 FluentNHibernate 2.0.3.0 SQLServer

    随机尝试之后,我将
    Not.KeyNullable()
    添加到
    HasMany
    链中。数据现在已正确插入,不会引发异常。unique和
    notnull
    约束工作正常

    这似乎是一个解决方案,但如果其他人想回答更详细的问题,我将很乐意接受这些问题。

    随机尝试之后,我将
    Not.KeyNullable()
    添加到
    HasMany
    链中。数据现在已正确插入,不会引发异常。unique和
    notnull
    约束工作正常


    这似乎是一个解决方案,但如果其他人想回答更详细的问题,我会很乐意接受这些答案。

    添加了我自己的答案,似乎解决了任何问题,但它并没有真正回答我的主要问题,即为什么。继续贡献吧。谢谢。你看到了吗?添加了我自己的答案,似乎解决了任何问题,但它并没有真正回答我的主要问题,为什么。继续贡献吧。谢谢。你看到了吗?