在EF Code-first中,三个实体可以共享同一个连接表吗?

发布于 2024-10-17 14:20:33 字数 929 浏览 2 评论 0原文

鉴于以下情况,实体框架代码优先会丢失/重命名连接或联接表。

public class User () {
 //all fields from Users table
 //...
 //Role collection from Roles table through UserRoleLinks table
 public List<Role> Roles { get; set; }
} 

public class AppUser() {
 //subset of fields from Users through AppUsers View
 //...
 //Role collection from Roles table through UserRoleLinks table
 public List<Role> Roles { get; set; }
}

User 和 AppUser 都有以下配置条目:

this.HasMany(e => e.Roles)
    .WithMany().Map(m => {
        m.MapLeftKey(a => a.Id, "Users_Id");
        m.MapRightKey(b => b.Id, "Roles_Id");
        m.ToTable("UserRoleLinks");
});

在运行时,尝试使用 AppUsers (DbSet) 时出现以下错误。

“对象名称无效 'dbo.UserRoleLinks1'。”“无效对象 名称“dbo.UserRoleLinks1”。”

代码优先配置似乎正在增加连接表名称,因为连接表名称已经存在。

我怎样才能使其工作?是否有更好的方法来获取表列的子集?

Given the following situation, Entity Framework Code-first missnames/renames the connecting or Join table.

public class User () {
 //all fields from Users table
 //...
 //Role collection from Roles table through UserRoleLinks table
 public List<Role> Roles { get; set; }
} 

public class AppUser() {
 //subset of fields from Users through AppUsers View
 //...
 //Role collection from Roles table through UserRoleLinks table
 public List<Role> Roles { get; set; }
}

Both User and AppUser have the following config entry:

this.HasMany(e => e.Roles)
    .WithMany().Map(m => {
        m.MapLeftKey(a => a.Id, "Users_Id");
        m.MapRightKey(b => b.Id, "Roles_Id");
        m.ToTable("UserRoleLinks");
});

At runtime I get the following error when trying to use the AppUsers (DbSet<AppUser>).

"Invalid object name
'dbo.UserRoleLinks1'." "Invalid object
name 'dbo.UserRoleLinks1'."

It appears that the Code-first configuration is incrementing the Join table name because one already exists.

How can I get this working? Is there a better way to get a subset of a table's columns?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泪冰清 2024-10-24 14:20:33

我认为这在 EF 中根本不可能(不仅在代码优先)。在您证明这在 EDMX 中可行之前,没有理由在代码优先中尝试它。

这里的问题是 AppUser 是 EF 的新实体。现在,用户 <-> 之间就有了 M:N 关系。角色和 AppUser <->角色和您希望将 M:N 关系映射到同一个联结表中。因为 EF 没有看到 AppUser 实际上只是用户表顶部的视图/查询,所以它可能会将其标记为无效映射。

如果您仅使用 AppUser 获取列的子集,请改为在 linq 查询中使用投影到自定义类型。

I think this is not possible in EF at all (not only in code-first). Until you prove that this is possible in EDMX there is no reason to try it in code-first.

The problem here is that AppUser is new entity for EF. Now you have M:N relation between the User <-> Roles and AppUser <-> Roles and you want to map both M:N relations into the same junction table. Because EF doesn't see that AppUser is actually only View/Query on top of User table it will probably mark this as invalid mapping.

If you only use AppUser to get subset of columns use projection into custom type in linq query instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文