使用 EF CTP4 映射一对多关系时出现问题

发布于 2024-10-02 13:17:54 字数 1419 浏览 2 评论 0原文

我正在尝试定义类别和项目之间的一对多关系(一个类别可以有一个或多个项目,一个项目可以有一个或没有类别)

public class Project : Entity {

    public virtual string Title { get; set; }

    public virtual Guid? CategoryId { get; set; }
    public virtual Category Category { get; set; }
}

public class Category : Entity {       
    public virtual string Name { get; set; }
    public virtual ICollection<Project> Projects { get; set; }
}

我已经定义了以下映射:

            modelBuilder.Entity<Project>()
            .MapSingleType(p => new {
                ProjectId = p.Id,
                p.CategoryId,
                p.Title,
                p.Slug,
                p.ShortDescription,
                p.Description,
                p.CreatedOn,
                p.UpdatedOn
            })
            .ToTable("Projects");

        modelBuilder.Entity<Category>()
            .MapSingleType(c => new {
                CategoryId = c.Id,
                c.Name,
                c.CreatedOn,
                c.UpdatedOn
            })
            .ToTable("Categories");

        // relationships
        modelBuilder.Entity<Project>()
            .HasOptional<Category>(p => p.Category)
            .WithMany()
            .HasConstraint((p, c) => p.CategoryId == c.Id);

现在虽然这看起来工作正常,但 EF仍在生成类别_产品表(用于多对多关联)。

我已禁用默认数据库初始值设定项,但仍在生成该表。我做错了什么?

谢谢 本

I am trying to define a one to many relationship between Category and Project (a Category can have one or many Projects, a Project can have one or no Category)

public class Project : Entity {

    public virtual string Title { get; set; }

    public virtual Guid? CategoryId { get; set; }
    public virtual Category Category { get; set; }
}

public class Category : Entity {       
    public virtual string Name { get; set; }
    public virtual ICollection<Project> Projects { get; set; }
}

I have defined the following mappings:

            modelBuilder.Entity<Project>()
            .MapSingleType(p => new {
                ProjectId = p.Id,
                p.CategoryId,
                p.Title,
                p.Slug,
                p.ShortDescription,
                p.Description,
                p.CreatedOn,
                p.UpdatedOn
            })
            .ToTable("Projects");

        modelBuilder.Entity<Category>()
            .MapSingleType(c => new {
                CategoryId = c.Id,
                c.Name,
                c.CreatedOn,
                c.UpdatedOn
            })
            .ToTable("Categories");

        // relationships
        modelBuilder.Entity<Project>()
            .HasOptional<Category>(p => p.Category)
            .WithMany()
            .HasConstraint((p, c) => p.CategoryId == c.Id);

Now although this appears to be working fine, EF is still generating a Categories_Products table (used for many to many associations).

I've disabled the default database initializer yet this table is still being generated. What am I doing wrong?

Thanks
Ben

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

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

发布评论

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

评论(1

江湖正好 2024-10-09 13:17:54

我删除了项目和类别映射代码,并让 EF 使用默认约定来创建数据库。这创建了我期望的关系(类别和项目之间的一对多)。

我想补充一点,我显式定义映射的唯一原因是 EF 似乎不能很好地处理基类。我有一个基类“Entity”,它有一个属性“Id”,我的所有实体都继承自该属性。这给 CTP4 带来了很多问题,所以我只是将其替换为接口 IEntity。这仍然给了我在使用通用存储库类时所需的约束。

希望实体基类能够在 RTM 中得到更好的支持

I removed the project and category mapping code and let EF use default conventions to create the database. This created the relationship I expected (one to many between category and project).

I would add that the only reason that I was explicitly defining the mapping was because EF does not appear to handle base classes very well. I had a base class "Entity" with a single property "Id" that all my entities inherited from. This caused so many problems with CTP4 that I just swapped it with an interface IEntity. This still gave me the constraints I needed when working with generic repository classes.

Hopefully entity base classes will be better supported in the RTM

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