开发者

Fluent Nhibernate modify default id

Today i have been setting up fluent nhibernate. I came across to Ids and when i mapped one class to the other, i had to specify what id the class is 开发者_开发百科mapped to. As my id is Always class name + id.

nhibernate has as default class name + "_"+ id.

so when i have to create mapping as follows:

Id(x => x.MenuItemId, "MenuItemId");

How do i overwrite the settings to be as i need and therefor i can use only mapping as

Id(x=>x.MenuItemId)

the problem does come up only when i do joining on another classes eg

Entity Menu joing on MenuItem and i use

HasMany(x=>x.MenuItems).Class(typeOf(Menu))


found this: that is the solution.

Identities

The automapper is opinionated, it expects your classes to be designed in a particular manner; if they're not, then it won't be able to automap them without a little assistance. The automapper expects your identities to be named Id, and if they aren't it won't find them.

You can modify the way the automapper discovers identities by overriding the IsId method in your automapping configuration. This method will be called for all the members in your entities that have already been accepted by the ShouldMap(Member) criteria; whichever member you return true for will be mapped as the identity.

public override bool IsId(Member member)
{
  return member.Name == member.DeclaringType.Name + "Id";
}

That example would match any ids that are named after their entity, such as CustomerId.

and calling it:

return Fluently.Configure()
               .MsSql2008
               .ConnectionString(c => c.FromConnectionStringWithKey("database"))
           .ShowSql()
               .ProxyFactoryFactory("NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu"))
                .Mappings(x => x.FluentMappings
.AddFromAssembly(
Assembly.GetExecutingAssembly())
.Conventions.AddFromAssemblyOf<Location.IdConvention>())
.BuildSessionFactory();

and after you set up id convencion

public class IdConvention : IIdConvention
    {
        public void Apply(IIdentityInstance identityInstance)
        {
            identityInstance.Column(identityInstance.EntityType.Name + "Id");
        }
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜