许多一对多没有反向属性 [英] Many-to-many without an inverse property

查看:119
本文介绍了许多一对多没有反向属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个EF许多一对多的关系。通常我会用InverseProperty属性,然而,在这种情况下:

 角色(ID,姓名,ICollection的<特级>)
特权(ID,姓名)

我没有逆属性。如何判断EF一个特权可能在多个角色中使用?



[目前EF把一个ROLE_ID列在表中的权限。这不是我想要的:-)]



编辑:我不想用流利的API,我正在寻找一个属性。


解决方案

  modelBuilder.Entity<作用>()
.HasMany(R => R。特权)
.WithMany()//< - 无参数=无反向属性
.MAP(M = GT;
{
m.ToTable(RolePrivileges);
m.MapLeftKey(角色ID);
m.MapRightKey(PrivilegeId);
});



我已经看到了你不想流利的API,但它是不可能与数据的注解。用注解映射选项只能用流利的API选项中的一部分,这才是与数据的注解映射选项丢失的情况。你需要在这里流利的API。


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.

解决方案

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

I have seen that you don't want Fluent API, but it is not possible with data annotations. Mapping options with annotations are only a subset of the options with Fluent API and this is a case where a mapping option with data annotations is missing. You need Fluent API here.

这篇关于许多一对多没有反向属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆