gpt4 book ai didi

fluent-nhibernate - 自动映射没有映射 Id

转载 作者:行者123 更新时间:2023-12-04 07:48:43 25 4
gpt4 key购买 nike

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)..

最佳答案

以上两个答案都是正确的;除非您指定不同,否则自动映射器会假定您有一个 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 .
干杯,
强尼

关于fluent-nhibernate - 自动映射没有映射 Id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2541382/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com