tags:

views:

56

answers:

1

I'm reading Pro ASP.Net MVC2 and I've gotten to a point where nothing is explained well enough. For example, the following tells me to create this C# code manually:

Implementing the Auctions Domain Model With LINQ to SQL, you can set up mappings between C# classes and an implied database schema either by decorating the classes with special attributes or by writing an XML configuration file. The XML option has the advantage that persistence artifacts are totally removed from your domain classes,4 but the disadvantage that it’s not so obvious at first glance. For simplicity, I’ll compromise here and use attributes. Here are the Auctions domain model classes now fully marked up for LINQ to SQL:5

using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq.Mapping;
using System.Data.Linq;
[Table(Name="Members")] 
public class Member
{
    [Column(IsPrimaryKey=true, IsDbGenerated=true, AutoSync=AutoSync.OnInsert)]
    internal int MemberID { get; set; }

    [Column] 
    public string LoginName { get; set; }

    [Column] 
    public int ReputationPoints { get; set; }
}

[Table(Name = "Items")] 
public class Item
{
    [Column(IsPrimaryKey=true, IsDbGenerated=true, AutoSync=AutoSync.OnInsert)]
    public int ItemID { get; internal set; }

    [Column] 
    public string Title { get; set; }

    [Column] 
    public string Description { get; set; }

    [Column] 
    public DateTime AuctionEndDate { get; set; }

    [Association(OtherKey = "ItemID")]
    private EntitySet<Bid> _bids = new EntitySet<Bid>();

    public IList<Bid> Bids { get { return _bids.ToList().AsReadOnly(); } }
}

Where exactly do I have to write this in? Or is he just displaying generated code by the Linq-to-sql DBML?

+1  A: 

That's not generated code. That's how you use Linq mappings to map your classes to your database.

You just write it in a CS file. It can go anywhere, but if you're using ASP.NET MVC you'd usually put it in the Models folder.

See this: http://msdn.microsoft.com/en-us/library/bb386971.aspx

Bennor McCarthy
There's a better explanation here: http://blogs.microsoft.co.il/blogs/bursteg/archive/2007/09/23/24907.aspx
Bennor McCarthy
I'm amazed that this is in fact not generated code. I'll stick to my Django models.
Deniz Dogan
Yeah, it's a little clunky. The attributes are what tells the DataContext where to look for the data in your database. The new Code-First approach in the Entity Framework is much better, but it's just a CTP at this stage.
Bennor McCarthy