【问题标题】:Many-to-many mapping with LINQ使用 LINQ 的多对多映射
【发布时间】:2010-06-12 07:11:10
【问题描述】:

我想在 C# 中以多对多关系执行 LINQ to SQL 映射,但数据不是强制性的。
说清楚:
我有一个新闻站点/博客,还有一个名为Posts 的表。一个博客可以同时与多个类别相关,因此有一个名为CategoriesPosts 的表与Posts 表和Categories 表的外键链接。如果在这种情况下很重要,我已经为每个表设置了一个身份主键,每个表中都有一个 id 字段。
在 C# 中,我为每个表定义了一个类,尽可能明确地定义了每个字段。 Post 类和 Category 类有一个 EntitySet 链接到 CategoryPost 对象,CategoryPost 类有 2 个 EntityRef 成员链接到彼此类型的 2 个对象。

问题在于帖子可能与任何类别相关或不相关,以及类别中可能包含或不包含帖子。我没有找到制作EntitySet<CategoryPost?> 或类似内容的方法。

所以当我添加第一篇文章时,没有一条 SQL 语句就一切顺利。此外,这篇文章出现在输出中。当我尝试添加第二个帖子时,我遇到了一个异常,Object reference not set to an instance of an object,关于 CategoryPost 成员。

帖子:

[Table(Name="tm_posts")]
public class Post : IDataErrorInfo
{
    public Post()
    {
        //Initialization of NOT NULL fields with their default values
    }

    [Column(Name = "id", DbType = "int", CanBeNull = false, IsDbGenerated = true, IsPrimaryKey = true)]
    public int ID { get; set; }

    private EntitySet<CategoryPost> _categoryRef = new EntitySet<CategoryPost>();
    [Association(Name = "tm_rel_categories_posts_fk2", IsForeignKey = true, Storage = "_categoryRef", ThisKey = "ID", OtherKey = "PostID")]
    public EntitySet<CategoryPost> CategoryRef
    {
        get { return _categoryRef; }
        set { _categoryRef.Assign(value); }
    }
}

分类帖子

[Table(Name = "tm_rel_categories_posts")]
public class CategoryPost
{
    [Column(Name = "id", DbType = "int", CanBeNull = false, IsDbGenerated = true, IsPrimaryKey = true)]
    public int ID { get; set; }

    [Column(Name = "fk_post", DbType = "int", CanBeNull = false)]
    public int PostID { get; set; }

    [Column(Name = "fk_category", DbType = "int", CanBeNull = false)]
    public int CategoryID { get; set; }

    private EntityRef<Post> _post = new EntityRef<Post>();
    [Association(Name = "tm_rel_categories_posts_fk2", IsForeignKey = true, Storage = "_post", ThisKey = "PostID", OtherKey = "ID")]
    public Post Post
    {
        get { return _post.Entity; }
        set { _post.Entity = value; }
    }

    private EntityRef<Category> _category = new EntityRef<Category>();
    [Association(Name = "tm_rel_categories_posts_fk", IsForeignKey = true, Storage = "_category", ThisKey = "CategoryID", OtherKey = "ID")]
    public Category Category
    {
        get { return _category.Entity; }
        set { _category.Entity = value; }
    }
}

类别

[Table(Name="tm_categories")]
public class Category
{
    [Column(Name = "id", DbType = "int", CanBeNull = false, IsDbGenerated = true, IsPrimaryKey = true)]
    public int ID { get; set; }

    [Column(Name = "fk_parent", DbType = "int", CanBeNull = true)]
    public int ParentID { get; set; }

    private EntityRef<Category> _parent = new EntityRef<Category>();
    [Association(Name = "tm_posts_fk2", IsForeignKey = true, Storage = "_parent", ThisKey = "ParentID", OtherKey = "ID")]
    public Category Parent
    {
        get { return _parent.Entity; }
        set { _parent.Entity = value; }
    }

    [Column(Name = "name", DbType = "varchar(100)", CanBeNull = false)]
    public string Name { get; set; }
}

那我做错了什么?如何使插入不属于任何类别的帖子成为可能?如何插入没有帖子的类别?

【问题讨论】:

  • 您不使用设计器有什么原因吗?它会为你处理大部分事情......
  • 主要是因为我现在基于两本书:Apress "ASP.NET MVC Professional" 和 Manning "LINQ in action",它们都强调硬编码定义。此外,我在这些实体类中有更多功能,我不知道在使用设计器时它是否可以正常工作。 (诸如带有默认值初始化器、验证器、注释的构造函数之类的东西)。不过谢谢。现在或星期天会试试这个。同时,我仍然愿意接受有关如何更正现有代码的建议。

标签: linq orm many-to-many


【解决方案1】:

似乎错误与映射无关。映射是正确的。
正如我所写,第一篇文章被插入没有问题,其余的都没有插入。从数据库中删除后,我仍然无法添加帖子。很明显,这与我在数据库中是否有某些东西无关,仅与我对代码进行了一些更改这一事实有关。

那么有哪些变化呢?在 Apress“ASP.NET MVC Pro”中,第一个示例说明了一种以迭代方式验证数据的方法(非声明性,使用 IDataErrorInfo 提供的工具),我坚持使用该方法。我通过该示例完成了所有操作,并且应该验证输入的函数调用搞砸了我的数据流,并在提交到数据库时抛出了该异常。

删除了该验证,一切正常。

抱歉,误报了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-04
    • 2011-05-19
    • 2023-03-07
    • 2012-06-22
    • 2021-12-19
    • 1970-01-01
    相关资源
    最近更新 更多