2

I have 2 entities Role and Permission with association one-to-many accordingly.

public class Role
{             
    public int Id { get; set; }

    public bool IsAdmin { get; set; }

    public virtual ICollection<Permission> Permissions { get; set; }
}

public class Permission
{
    public int Id { get; set; }

    public string Code { get; set; }

    public string GroupName { get; set; }

    public virtual Role Role { get; set; }    
}

And created mapping classes for them inherited from EntityTypeConfiguration class.

When I run my application EF created database for me and foreign key for these entities above was Role_Id.

How can I change existing or add new convention to get ride of the underscore in foreign key?

So I want to have RoleId as a foreign key for my entities.

I don't want use data annotation attributes and don't want to add extra property to Permission class (public int RoleId { get; set; }) in order to use it in mapping like this:
HasRequired(x => x.Role).WithMany(y => y.Permissions).HasForeignKey(o => o.RoleId);

Thanks,
Alexey

4

1 回答 1

3

实体框架目前不支持自定义全局约定,但您可以覆盖 Fluen API 中的键名:

modelBuilder.Entity<Permission>()
            .HasRequired(x => x.Role)
            .WithMany(y => y.Permissions)
            .Map(m => m.MapKey("RoleId"));
于 2012-10-02T10:27:13.353 回答