开发者

Can you help me understand this C# code?

开发者 https://www.devze.com 2023-01-10 09:52 出处:网络
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 manual开发者_开发知识库ly:

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 manual开发者_开发知识库ly:

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?


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

0

精彩评论

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

关注公众号