开发者

How to map with Linq to SQL one to many

I have a table '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; }
    }

and a table comment

 [Table(Name = "dbo.Comments")]
pub开发者_如何学编程lic 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; }
    }
}

One Article can have many comments and each comment can be placed by a User

[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; }
    }

How do I map the relatinship between these tables?

Thanks


I think you are creating your table classes manually. there is no need to do that.

add a LINQ to SQL (dbml) file to your solution, open Server Explorer window and connect to your database, drag and drop the tables into the design of your dbml class.

if there are foreign key constraints in your tables, then link will create respective properties in both classes.

and if you want to do it manually (which I can't see why), create a property with the type of the referenced class and this is the attribute needed:

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

hope I helped a little


You can read about mapping associations here.

In your case:

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; }
}

Of course, you should first add ArticleID column to Comments table in database.

The part below is not presented in MSDN code from the link above, but without it I had a lot of problems with DTO in WCF service. So, now I prefer to add it to every association:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜