2

I want to create a many-to-many relationship in EF. Normally I would use the InverseProperty attribute, however, in this case:

Role ( Id, Name, ICollection<Privilege> )
Privilege ( Id, Name )

I don't have an inverse property. How to tell EF that a privilege may be used in more than one Role?

[ Currently EF puts a Role_Id column in the Privilege table. This is not what I want :-) ]

Edit: I do not want to use the Fluent API, I'm looking for an attribute.

4

1 回答 1

2
modelBuilder.Entity<Role>()
    .HasMany(r => r.Privileges)
    .WithMany() // <- no parameter = no inverse property
    .Map(m =>
    {
        m.ToTable("RolePrivileges");
        m.MapLeftKey("RoleId");
        m.MapRightKey("PrivilegeId");
    });

我已经看到您不想要 Fluent API,但使用数据注释是不可能的。带有注释的映射选项只是 Fluent API 选项的一个子集,这是缺少带有数据注释的映射选项的情况。你需要 Fluent API。

于 2013-04-20T14:05:27.600 回答