Linq To Sql Domain Model
This is a more generic question, and I'm not 100% on what I'm actually asking so apologies in advance if this turns out of be a load of waffle!
I am creating a website in ASP.NET MVC2, and I'm looking at setting up my domain model. I am reading books that appear to be setting up the relationships between tables in C#, as opposed to the database (or maybe I'm reading it wrong?).
If so, is this the correct way to do it?
For example, if take the 2 domain model classes below:
[Table(Name="Users")]
public class User
{
[Column(IsPrimaryKey=true, IsDbGenerated=true, AutoSync=AutoSync.OnInsert)]
public int ID{get;set;}
public string Name{get;set;}
}
[Table(Name="Posts")]
public class Post
{
[Column(IsPrimaryKey=true, IsDbGenerated=true, AutoSync=AutoSync.OnInsert)]
internal int ID{get;set;}
internal int MemberID {get;set;}
public string Body {get;set;}
internal EntityRef<User> _user;
[Association(ThisKey="MemberID", storage="_user")]
public User User
{
get{ return _user.Entity; }
internal set{ _member.Entity = value; ID = value.ID; }
}
}
The EntityRef, is this a replacement for creating a relationship in MSSQL Server? Is the idea to define the rela开发者_运维百科tionships between entities in the C# rather than the database itself?
Just a little confused, and as I said I could be totally off course here.
No, you should still create the relationships in the database. What you see in the C# code is simply how LinqToSql knows what tables and columns make up a relationship. But you should still create the relationships in your database so that you can enforce data integrity.
精彩评论