使用 Fluent-nhibernate 自动映射在外键上添加多列唯一约束

发布于 2024-08-18 09:26:26 字数 1542 浏览 7 评论 0原文

我是一个 NHibernate 和流畅的 NHibernate 新手。我在唯一约束和 nhibernate 映射方面遇到了一些问题。

我有域模型的以下部分。

public class Batch
{
    public virtual int Id {get; set;}
    public virtual string Name {get; set;}
    public virtual IList<BatchParameter> BatchParameters {get; set;}
}
public class BatchParameter
{
    public virtual int Id {get; set;}
    public virtual string Name {get; set;}
    public virtual Batch Batch {get; set;}
}

我正在尝试使用 Fluent-nhibernate 使用自动映射将其映射到数据库(SQLServer)上。 我想设置我的数据库,以便具有:

  • “Id”属性上的主键

  • BatchParamets 表上的外键

    p>
  • Batch 表中 Name 列的唯一约束

  • BatchParameters 表中 Name 和列的唯一约束Batch_Id

所以我写下了以下代码:

public class BatchMapping : IAutoMappingOverride<Batch>
{
    public void Override(FluentNHibernate.Automapping.AutoMapping<Batch> mapping)
    {
        mapping.Id( b => b.Id);
        mapping.HasMany<BatchParameter>(p => p.BatchParameters).Cascade.All().Inverse();
    }
}

public class BatchParameterMapping : IAutoMappingOverride<BatchParameter>
{
    public void Override(FluentNHibernate.Automapping.AutoMapping<BatchParameter> mapping)
    {
        mapping.Id( b => b.Id);
        mapping.Map(b => b.Name).Unique();
        //mapping.Map(p => p.Name).UniqueKey("Batch_Parameter");
        //mapping.Map(p => p.Batch.Id).UniqueKey("Batch_Parameter");
    }
}

主键、外键和第一个唯一约束没有问题。对于唯一约束有点头疼。

有人可以告诉我直接的方法吗???

谢谢!

I'm an NHibernate and fluent-nhibernate newbie. And I've got some problem with unique constraint and nhibernate mapping.

I've got the following part of domain model.

public class Batch
{
    public virtual int Id {get; set;}
    public virtual string Name {get; set;}
    public virtual IList<BatchParameter> BatchParameters {get; set;}
}
public class BatchParameter
{
    public virtual int Id {get; set;}
    public virtual string Name {get; set;}
    public virtual Batch Batch {get; set;}
}

I'm trying to use fluent-nhibernate to map it on the db (SQLServer) using automapping.
I want to be set up my db in order to have :

  • Primary Keys on the "Id"s properties

  • a Foreign Key on the BatchParamets table

  • a Unique Constraint on the Batch table on column Name

  • a Unique Constraint on the BatchParameters table on columns Name and Batch_Id

So I've written down this code:

public class BatchMapping : IAutoMappingOverride<Batch>
{
    public void Override(FluentNHibernate.Automapping.AutoMapping<Batch> mapping)
    {
        mapping.Id( b => b.Id);
        mapping.HasMany<BatchParameter>(p => p.BatchParameters).Cascade.All().Inverse();
    }
}

public class BatchParameterMapping : IAutoMappingOverride<BatchParameter>
{
    public void Override(FluentNHibernate.Automapping.AutoMapping<BatchParameter> mapping)
    {
        mapping.Id( b => b.Id);
        mapping.Map(b => b.Name).Unique();
        //mapping.Map(p => p.Name).UniqueKey("Batch_Parameter");
        //mapping.Map(p => p.Batch.Id).UniqueKey("Batch_Parameter");
    }
}

No problems for the primary keys, the foreign key and the first Unique Constraint. A little bit of headache for the Unique Constraint.

Can someone show me the straight way???

Thanks!

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

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

发布评论

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

评论(2

疑心病 2024-08-25 09:26:26

首先,您似乎遇到了复制粘贴错误:...Map(b => b.Name)... 应该放入 BatchMapping,不是BatchParameterMapping。

public class BatchMapping : IAutoMappingOverride<Batch>
{
    public void Override(AutoMapping<Batch> mapping)
    {
        mapping.Map(b => b.Name).Unique();
    }
}

接下来,BatchParameter.Batch 是从 BatchParameterBatch 的多对一关系,因此应该使用 References( ...) 而不是 Map(...)。您可以使用 References 作为另一个实体的外键,并使用 Map 作为简单属性。

public class BatchParameterMapping : IAutoMappingOverride<BatchParameter>
{
    public void Override(AutoMapping<BatchParameter> mapping)
    {
        mapping.Map(p => p.Name).UniqueKey("Batch_Parameter");
        mapping.References(p => p.Batch).UniqueKey("Batch_Parameter");
    }
}

最后,您应该删除 Id 属性和 Batch.BatchParameters 的不必要映射。默认情况下,Fluent NHibernate 的自动映射将根据需要映射它们。在 Override 方法中,您只需指定要执行与自动映射默认值不同的操作的属性,例如指定唯一键。

First, it looks like you have a copy-and-paste error: ...Map(b => b.Name)... should go in BatchMapping, not BatchParameterMapping.

public class BatchMapping : IAutoMappingOverride<Batch>
{
    public void Override(AutoMapping<Batch> mapping)
    {
        mapping.Map(b => b.Name).Unique();
    }
}

Next, BatchParameter.Batch is a many-to-one relationship from BatchParameter to Batch, so it should be mapped with References(...) instead of Map(...). You use References for foreign keys to another entity and use Map for simple properties.

public class BatchParameterMapping : IAutoMappingOverride<BatchParameter>
{
    public void Override(AutoMapping<BatchParameter> mapping)
    {
        mapping.Map(p => p.Name).UniqueKey("Batch_Parameter");
        mapping.References(p => p.Batch).UniqueKey("Batch_Parameter");
    }
}

Finally, you should remove the unnecessary mappings for the Id properties and Batch.BatchParameters. Fluent NHibernate's auto-mapping will map them as desired by default. In your Override methods you only need to specify the properties where you want to do something differently than the auto-mapping default, such as specifying unique keys.

赤濁 2024-08-25 09:26:26

如果 Id 和 Name 是 BatchParameter 表中的主键,则您将需要一个复合 id。另外,如果您想从 BatchParameter 类返回 Batch 的引用,则需要使用 Reference。以下内容应该接近您的需要:

public class BatchParameterMapping : IAutoMappingOverride<BatchParameter>
{
    public void Override(FluentNHibernate.Automapping.AutoMapping<BatchParameter> mapping)
    {
        mapping.CompositeId()
            .KeyProperty(x => x.Id)
            .KeyProperty(x => x.Name);

        mapping.References(x => x.Batch);
    }
}

If Id and Name are primary keys in your BatchParameter table you would need a composite id. Also if you want to have a reference back to Batch from your BatchParameter class you will need to use Reference. The following should be close to what you need:

public class BatchParameterMapping : IAutoMappingOverride<BatchParameter>
{
    public void Override(FluentNHibernate.Automapping.AutoMapping<BatchParameter> mapping)
    {
        mapping.CompositeId()
            .KeyProperty(x => x.Id)
            .KeyProperty(x => x.Name);

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