EF4 CTP5 数据库优先 +多对多关系(错误)

发布于 2024-10-12 21:35:30 字数 1375 浏览 1 评论 0原文

我很确定这与隐藏约定有关,但在尝试将多对多关系映射到现有数据库时,我总是会遇到错误。 这是最简单的例子:

[Table("ALRole", SchemaName = "AL")]
public class Role
{
    public int ID { get; set; }
    public string Name { get; set; }
    public virtual ICollection<User> Users { get; set; }
}

[Table("ALUser", SchemaName = "AL")]
public class User
{
    public int ID { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Role> Roles { get; set; }
}

我在数据库中得到了通常的三个表:前两个是明显的,第三个是用这个脚本创建的:

CREATE TABLE AL.ALUsersRoles
(
    RoleID int NOT NULL,
    UserID int NOT NULL,
    CONSTRAINT PK_ALUserRole PRIMARY KEY(RoleID, UserID),
    CONSTRAINT FK_ALUserRole_RoleID FOREIGN KEY(RoleID) REFERENCES AL.ALRole(ID),
    CONSTRAINT FK_ALUserRole_UserID FOREIGN KEY(UserID) REFERENCES AL.ALUser(ID)
)

代码来映射多对多关系:

// ...I'm in the EntityTypeConfiguration-derived class (User)
HasMany(u => u.Roles)
    .WithMany(r => r.Users)
    .Map(m =>
    {
        m.MapLeftKey(u => u.ID, "UserID");
        m.MapRightKey(r => r.ID, "RoleID");
        ToTable("ALUsersRoles", "AL");
    });

现在我尝试使用如下 尝试了此代码中的所有可能的组合和变化,但我总是收到错误:

{"Invalid column name 'Name'.\r\nInvalid ...and so on...

所以我认为它一定是未正确创建的表。 有什么想法吗? 提前致谢 安德里亚 PS:我删除了一些代码,所以可能会有一些小错字......

I'm pretty sure it's something regarding hidden conventions, but I always get an error when trying to map a many-to-many relation to an existing database.
Here is the simplest example:

[Table("ALRole", SchemaName = "AL")]
public class Role
{
    public int ID { get; set; }
    public string Name { get; set; }
    public virtual ICollection<User> Users { get; set; }
}

[Table("ALUser", SchemaName = "AL")]
public class User
{
    public int ID { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Role> Roles { get; set; }
}

I got the usual three tables in the db: the first two are obvious, and the third is created with this script:

CREATE TABLE AL.ALUsersRoles
(
    RoleID int NOT NULL,
    UserID int NOT NULL,
    CONSTRAINT PK_ALUserRole PRIMARY KEY(RoleID, UserID),
    CONSTRAINT FK_ALUserRole_RoleID FOREIGN KEY(RoleID) REFERENCES AL.ALRole(ID),
    CONSTRAINT FK_ALUserRole_UserID FOREIGN KEY(UserID) REFERENCES AL.ALUser(ID)
)

Now I try to map the many-to-many relation, with code like this:

// ...I'm in the EntityTypeConfiguration-derived class (User)
HasMany(u => u.Roles)
    .WithMany(r => r.Users)
    .Map(m =>
    {
        m.MapLeftKey(u => u.ID, "UserID");
        m.MapRightKey(r => r.ID, "RoleID");
        ToTable("ALUsersRoles", "AL");
    });

I tried all the possibile combinations and variations in this code, but I always get the error:

{"Invalid column name 'Name'.\r\nInvalid ...and so on...

So I think it must be the table that is not created correctly.
Any ideas?
Thanks in advance
Andrea
P.S.: I stripped down some of my code, so maybe there can be some small typo...

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

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

发布评论

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

评论(2

揽月 2024-10-19 21:35:30

好吧,这对我来说和OP一样有效。

        //many-to-many between *Users -> Web_User_Rol <- Web_Rol*
        modelBuilder.Entity<Users>()
            .HasMany(u => u.Web_Rols).WithMany(r => r.Users)
            .Map(t=>t.MapLeftKey("user_id")
                     .MapRightKey("roleID")
                     .ToTable("Web_User_Rol"));

well, this works for me same as OP.

        //many-to-many between *Users -> Web_User_Rol <- Web_Rol*
        modelBuilder.Entity<Users>()
            .HasMany(u => u.Web_Rols).WithMany(r => r.Users)
            .Map(t=>t.MapLeftKey("user_id")
                     .MapRightKey("roleID")
                     .ToTable("Web_User_Rol"));
偏爱自由 2024-10-19 21:35:30

您的对象模型或流畅的 API 代码没有任何问题。我已经使用过它们,它们完美地创建了所需的架构,没有任何例外。我认为您的问题来自另一个实体(可能具有“Name”属性),与您在此处显示的内容无关。要找到这一点,请删除(或重命名)现有数据库,让 Code First 为您创建一个数据库,然后比较这两个数据库,看看有什么不同。

There is nothing wrong with your object model or fluent API code. I've used them and they perfectly created the desired schema without any exception. I think your problem comes from another entity (perhaps one with a "Name" property) unrelated to what you've shown here. To find that, drop (or rename) your existing database and let Code First create one for you and then compare the 2 databases and see what is different.

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