自动映射没有映射 ID

发布于 2024-08-27 09:06:59 字数 1202 浏览 8 评论 0原文

My Entity Class:
public class Building 
    {
        /// <summary>
        /// internal Id 
        /// </summary>
        public virtual long Id { get; set; }
..............
}

我的映射:

var model = AutoMap.AssemblyOf<Building>()
                        .Setup(s => s.FindIdentity = p => p.Name == "Id")
                        .Where(t => t.Namespace == "SpikeAutoMappings");

var database = Fluently.Configure()
                        .Database(DatabaseConfigurer)
                        .Mappings(m=>m.AutoMappings.Add(model));

我需要有人帮助我查看问题所在,因为在运行单元测试时我一直遇到此错误:

Initialization method TestProject1.MappingTestBase.TestInitialize threw exception. FluentNHibernate.Cfg.FluentConfigurationException:  FluentNHibernate.Cfg.FluentConfigurationException: An invalid or incomplete configuration was used while creating a SessionFactory. Check PotentialReasons collection, and InnerException for more detail.

 --->  FluentNHibernate.Visitors.ValidationException: The entity doesn't have an Id mapped. Use the Id method to map your identity property. For example: Id(x => x.Id)..
My Entity Class:
public class Building 
    {
        /// <summary>
        /// internal Id 
        /// </summary>
        public virtual long Id { get; set; }
..............
}

My Mapping:

var model = AutoMap.AssemblyOf<Building>()
                        .Setup(s => s.FindIdentity = p => p.Name == "Id")
                        .Where(t => t.Namespace == "SpikeAutoMappings");

var database = Fluently.Configure()
                        .Database(DatabaseConfigurer)
                        .Mappings(m=>m.AutoMappings.Add(model));

I need somebody to help me see what is wrong because I keep having this error when run unit test:

Initialization method TestProject1.MappingTestBase.TestInitialize threw exception. FluentNHibernate.Cfg.FluentConfigurationException:  FluentNHibernate.Cfg.FluentConfigurationException: An invalid or incomplete configuration was used while creating a SessionFactory. Check PotentialReasons collection, and InnerException for more detail.

 --->  FluentNHibernate.Visitors.ValidationException: The entity doesn't have an Id mapped. Use the Id method to map your identity property. For example: Id(x => x.Id)..

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

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

发布评论

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

评论(3

柏林苍穹下 2024-09-03 09:06:59

上面两个答案都是对的;除非您指定不同,否则自动映射器假定您有一个 int Id 字段。
如果您的 ID 很长,自动映射器可能无法正确识别它。
尝试为您的类定义一个 MappingOverride,如下所示:

public class UserMappingOverride : IAutoMappingOverride<User>
{
    #region IAutoMappingOverride<User> Members

    public void Override(AutoMapping<User> mapping)
    {
        mapping.Id(u => u.Name);
    }

    #endregion
}

Id() 函数允许您覆盖自动映射器关于 ID 字段的约定。
有关覆盖的更多信息,请参阅 http://wiki. Fluentnhibernate.org/Auto_mapping#Overrides
干杯,
强尼

both answers above are right; unless you specify differently, the automapper assumes that you have an int Id field.
If your Id is long, the automapper might not recognize it correctly.
try defining a MappingOverride for your class(es), like so:

public class UserMappingOverride : IAutoMappingOverride<User>
{
    #region IAutoMappingOverride<User> Members

    public void Override(AutoMapping<User> mapping)
    {
        mapping.Id(u => u.Name);
    }

    #endregion
}

the Id() function allows you to override the automapper's convention of what the ID field should be.
for further info on overriding, see http://wiki.fluentnhibernate.org/Auto_mapping#Overrides.
Cheers,
Jhonny

2024-09-03 09:06:59

一般来说,使用 AutoMapping 是一个糟糕的策略,因为归档的 Id 必须存在于数据库表中。相反,请考虑使用流畅的映射生成器,例如 NMG 来处理映射。

在这种情况下,您首先需要下载/安装应用程序,然后从数据库(Oracle、SQL 和其他各种数据库)生成映射文件。

为了创建映射文件,首先在项目中创建一个 /Entities/ 文件夹。接下来,按如下方式配置生成器软件:

首选项

  1. 生成的属性名称 = 与数据库列名称相同(无更改)
  2. 映射样式 = Fluent 映射
  3. 字段或属性 = 自动属性

​​可用语言:C# 和 VB

  1. 文件夹:[您的项目文件夹]\Entities
  2. 命名空间:[您的项目命名空间].Entities
  3. 程序集名称:[您的项目名称].Entities< /code>

接下来,生成全部或生成特定表。

现在应该在您的项目中创建所有 *.cs*Map.cs 文件(您可以使用 Add Existing Item... 添加它们) code> 如果它们没有出现)。

使用 Fluent,您将看到类似以下内容:

Id(x => x.keyName_ID)
  .Column(x => x.keyname_ID)
  .GeneratedBy
  .Sequence("keyname_ID")

Id(x => x.keyName_ID)
  .Column(x => x.keyname_ID)
  .GeneratedBy
  .Identity()
  .Column("keyname_ID")

Id(x => x.keyName_ID)
  .Column(x => x.keyname_ID)
  .GeneratedBy
  .Assigned()

因此,现在我们需要使用 FluentMappingFluent nHibernate 来指定 Id。为此,您需要覆盖解决方案中每个 Map 文件中代码的 Id 行。只需添加:

Id(x => x.KeyName_ID)
  .GeneratedBy
  .GetGeneratorMapping()
  .IsSpecified("KeyName_ID");

其中 keyname_id 是数据库中 id 的列名称,而不是创建的列名称。

请注意,在 BuildSession 的映射中,您必须具有:

(...).Mappings(m => 
    m.FluentMappings.AddFromAssemblyOf<[one of your entities]>()
);

并且,现在 Id 已映射。 :) 我希望这有帮助!

Generally, using AutoMapping is a poor policy because the filed Id must exist in your database tables. Instead, consider using a fluent mapping generator, such as NMG to handle your mapping.

In this case, you would first want to download/install the application, then generate the Mapping Files from your database (Oracle, SQL and various others).

In order to create the Mapping Files, first create an /Entities/ folder within your project. Next, configure the generator software as follows:

Preferences

  1. Generated Property Name = Same as database column name (No change)
  2. Mapping Style = Fluent Mapping
  3. Field or Property = Auto Property

Languages available: C# and VB

  1. Folder : [your project folder]\Entities
  2. Namespace : [your project namespace].Entities
  3. Assembly Name: [your project name].Entities

Next, either Generate All or Generate the Specific Table.

All of the *.cs and *Map.cs files should now be created in your project (you can add them with Add Existing Item... if they don't show up).

Using Fluent, you will see something like the following:

Id(x => x.keyName_ID)
  .Column(x => x.keyname_ID)
  .GeneratedBy
  .Sequence("keyname_ID")

or

Id(x => x.keyName_ID)
  .Column(x => x.keyname_ID)
  .GeneratedBy
  .Identity()
  .Column("keyname_ID")

or

Id(x => x.keyName_ID)
  .Column(x => x.keyname_ID)
  .GeneratedBy
  .Assigned()

So, now we need to specify the Id using FluentMapping with Fluent nHibernate. To do this, you need to overwrite the Id line of on code in each of the Map files in the solution. Simply add:

Id(x => x.KeyName_ID)
  .GeneratedBy
  .GetGeneratorMapping()
  .IsSpecified("KeyName_ID");

Where keyname_id is the column name of the id in your database, rather than the one created.

Notice that in your mapping at the BuildSession you must have:

(...).Mappings(m => 
    m.FluentMappings.AddFromAssemblyOf<[one of your entities]>()
);

And, now Id is mapped. :) I hope this helps!

素年丶 2024-09-03 09:06:59

我对自动映射的经验是,只要您的实体类具有以下行:

    public virtual int Id { get; private set; }

自动映射器就会将其视为 ID,而无需程序员的进一步帮助(即不需要您在 AutoMap 调用中使用的 FindIdenity 代码)。

我在你的 ID 声明中看到的唯一区别是你使用了 long 类型而不是 int 类型。不知道这是否重要。

My experience with Automapping is that as long as your Entity class has the line:

    public virtual int Id { get; private set; }

the automapper will treat it as an ID with no further help from the programmer (i.e. no need for the FindIdenity code you are using in your AutoMap call).

The only difference I see in your ID declaration is that you use a type long instead of int. Don't know if this matters or not.

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