PrimaryKeyNamingConvention Fluent Automapping

2019-06-24 12:02发布

问题:

I have a question about using PrimaryKeyNamingConvention Suppose the following class:

public class banco
{
    [Required]
    public virtual int banco_id { get; set; }
   ...
}

and

public class PrimaryKeyNamingConvention : IIdConvention
{
    public void Apply(IIdentityInstance instance)
    {
        instance.Column(instance.EntityType.Name + "_id");
    }
}

and

 static AutoPersistenceModel CreateAutomappings()
 {
 ... Conventions.Setup(c =>
            {
                c.Add<PrimaryKeyNamingConvention>();
             });

You can use something like described above? When I try to run an error occurs

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

回答1:

You can use such Ids. But you need to map not only column name, but property name also.

[Edit] Code added from this question

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