如何使用Linq一对多映射到SQL

本文关键字:SQL 映射 一对多 何使用 Linq | 更新日期: 2023-09-27 17:58:11

我有一个表'Article'

private int id;
    [ColumnAttribute(Storage = "id", AutoSync = AutoSync.OnInsert, DbType = "Int NOT NULL IDENTITY", IsPrimaryKey = true, IsDbGenerated = true)]
    public int Id
    {
        get { return id; }
        set { id = value; }
    }
    private string title;
    [ColumnAttribute(Storage = "title", DbType = "NVarChar(250) NOT NULL", CanBeNull = false)]
    public string Title
    {
        get { return title; }
        set { title = value; }
    }
    private string description;
    [ColumnAttribute(Storage = "description", DbType = "NVarChar(350) NOT NULL", CanBeNull = false)]
    public string Description
    {
        get { return description; }
        set { description = value; }
    }

和一个表注释

 [Table(Name = "dbo.Comments")]
public class CommentDto
{
    private int id;
    [ColumnAttribute(Storage = "id", AutoSync = AutoSync.OnInsert, DbType = "Int NOT NULL IDENTITY", IsPrimaryKey = true, IsDbGenerated = true)]
    public int Id
    {
        get { return id; }
        set { id = value; }
    }
    private string content;
    [ColumnAttribute(Storage = "content", DbType = "NVarChar(600) NOT NULL", CanBeNull = false)]
    public string Content
    {
        get { return content; }
        set { content = value; }
    }
    private string date;
    [ColumnAttribute(Storage = "date", DbType = "DateTime NOT NULL", CanBeNull = false)]
    public string Date
    {
        get { return date; }
        set { date = value; }
    }
}

一篇文章可以有许多评论,每个评论都可以由用户放置

[TableAttribute(Name = "dbo.Users")]
public class UserDto
{
    private int id;
    [ColumnAttribute(Storage = "id", AutoSync = AutoSync.OnInsert, DbType = "Int NOT NULL IDENTITY", IsPrimaryKey = true, IsDbGenerated = true)]
    public int Id
    {
        get { return id; }
        set { id = value; }
    }
    private string username;
    [ColumnAttribute(Storage = "username", DbType = "NVarChar(150) NOT NULL", CanBeNull = false)]
    public string Username
    {
        get { return username; }
        set { username = value; }
    }

如何映射这些表之间的关系?

感谢

如何使用Linq一对多映射到SQL

我认为您正在手动创建表类。没有必要那样做。

将LINQ to SQL(dbml)文件添加到您的解决方案中,打开服务器资源管理器窗口并连接到您的数据库,将这些表拖放到dbml类的设计中。

如果表中存在外键约束,那么link将在两个类中创建各自的属性。

如果你想手动完成(我不明白为什么),使用引用类的类型创建一个属性,这是所需的属性:

[Association(Name="your_fk_constraint_name", Storage="name_of_your_private_backup_field", ThisKey="name_of_the_key_in_this_table", IsForeignKey=true)]

希望我帮了一点忙

您可以在此处阅读有关映射关联的信息。

在您的情况下:

class Article
{
    private EntitySet<CommentDto> _Comments;
    [Association(OtherKey = "ArticleID")]
    public virtual IList<CommentDto> Comments
    {
            get
            {
                if (_Comments == null)
                    _Comments = new EntitySet<CommentDto>();
                return _Comments;
            }
            set
            {
                Comments.Assign(value);
            }
    }
}
class Comment
{
    [Association(ThisKey="ArticleID")]
    public ArticleDto Article { get; set; }
}

当然,您应该首先将ArticleID列添加到数据库中的Comments表中。

下面的部分没有出现在上面链接的MSDN代码中,但如果没有它,我在WCF服务中的DTO会遇到很多问题。所以,现在我更喜欢将它添加到每个关联中:

if (_Comments == null)
    _Comments = new EntitySet<CommentDto>();