开发者

Database design help. Many to many creates an association table, correct?

开发者 https://www.devze.com 2023-01-04 15:53 出处:网络
I\'m designing a very simple database for my application and it\'s configured like this: Employee Carnet

I'm designing a very simple database for my application and it's configured like this:

Employee
Carnet
Name
LastName

Area
Name

Document
ID
Employee (FK)
Project (FK)

Project
ID
Company (FK)
Title

Company
Name
CEO (FK)
NIT

Person
Carn开发者_如何转开发et
Name
Lastname

Now the gist of this question is, an Area can have many Document; and a Document can belong to many Area.

This I'm told, creates a third relationship table, correct?

What would go in that table and would this be able to work if I'm going to be using Linq-to-SQL as the only tool for accessing my database.


Yes... You'd have a table called AreaDocuments with columns called AreaID and DocumentId. This kind of relationship is M x N where M instances can be associated to N instances and vice-versa.

Sample data:

AreaId      DocumentId
1           1
1           2
2           1
2           2

How to handle it in code:

Document document1 = new Document();
document1.Id = 1;
document1.Title = "Whatever";

Document document2 = new Document();
document2.Id = 2;
document2.Title = "Whatever";

Area area1 = new Area();
area1.Documents.Add(document1);
area1.Documents.Add(document2);

Area area2 = new Area();
area2.Documents.Add(document1);
area2.Documents.Add(document2);

This link How to implement a many-to-many relationship using Linq to Sql? can provide more information about using this kind of relationship in LINQ to SQL.

0

精彩评论

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