Here is my fictional tables:
Post{
Post-Id int primary key identity,
Post-another-columns blahblahblah
}
Lamp{
Lamp-Id int primary key identity,
Lamp-another-columns blahblahblah
}
Post-Lamp(n-n Relationship){
Post-Id FOREIGN KEY REFERENCES Posts(Post-Id),
Lamp-Id FOREIGN KEY REFERENCES Lamp(Lamp-Id),
Lamp-Index int not null, --(describe the Lamp index that is in this Post)
Post-Bar-another-columns blahblahblah,
CONSTRAINT PK_Post_Lamp PRIMARY KEY NONCLUSTERED ([Post-Id], [Lamp-Id])
}
On my .Net code, every time that i put a Lamp into a Post, i insert a index based at the MAX index that a Post has of the Lamps. It's a foreach Lamp in Post, insert into Post-Lamp(postId, index, lampId)
, but this seems very wrong.
How can i make a incremental column for the Lamp-Index that respects each change of Post-Id?
Edit: .Net code
int postId = await _repositories.Post.Add(post);
List<LampPost> lamps = model.Lamp.Select((l, i) => new { Lamp = l, Index = i })
.Select(item => new LampPost
{
LampId= item.Lamp,
LampIndex = item.Index,
PostId = postId,
}).ToList();
await _repositories.Lamp.Add(lamps);