自动映射没有映射 ID
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
上面两个答案都是对的;除非您指定不同,否则自动映射器假定您有一个 int Id 字段。
如果您的 ID 很长,自动映射器可能无法正确识别它。
尝试为您的类定义一个 MappingOverride,如下所示:
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:
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
一般来说,使用 AutoMapping 是一个糟糕的策略,因为归档的 Id 必须存在于数据库表中。相反,请考虑使用流畅的映射生成器,例如 NMG 来处理映射。
在这种情况下,您首先需要下载/安装应用程序,然后从数据库(Oracle、SQL 和其他各种数据库)生成映射文件。
为了创建映射文件,首先在项目中创建一个
/Entities/
文件夹。接下来,按如下方式配置生成器软件:首选项
可用语言:C# 和 VB
[您的项目文件夹]\Entities
[您的项目命名空间].Entities
[您的项目名称].Entities< /code>
接下来,生成全部或生成特定表。
现在应该在您的项目中创建所有
*.cs
和*Map.cs
文件(您可以使用Add Existing Item...
添加它们) code> 如果它们没有出现)。使用 Fluent,您将看到类似以下内容:
或
或
因此,现在我们需要使用
FluentMapping
和Fluent nHibernate
来指定Id
。为此,您需要覆盖解决方案中每个Map
文件中代码的Id
行。只需添加:其中
keyname_id
是数据库中id
的列名称,而不是创建的列名称。请注意,在
BuildSession
的映射中,您必须具有:并且,现在
Id
已映射。 :) 我希望这有帮助!Generally, using
AutoMapping
is a poor policy because the filedId
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
Languages available: C# and VB
[your project folder]\Entities
[your project namespace].Entities
[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 withAdd Existing Item...
if they don't show up).Using Fluent, you will see something like the following:
or
or
So, now we need to specify the
Id
usingFluentMapping
withFluent nHibernate
. To do this, you need to overwrite theId
line of on code in each of theMap
files in the solution. Simply add:Where
keyname_id
is the column name of theid
in your database, rather than the one created.Notice that in your mapping at the
BuildSession
you must have:And, now
Id
is mapped. :) I hope this helps!我对自动映射的经验是,只要您的实体类具有以下行:
自动映射器就会将其视为 ID,而无需程序员的进一步帮助(即不需要您在 AutoMap 调用中使用的 FindIdenity 代码)。
我在你的 ID 声明中看到的唯一区别是你使用了 long 类型而不是 int 类型。不知道这是否重要。
My experience with Automapping is that as long as your Entity class has the line:
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.