LINQ to SQL 1:1 Insert?

I had to read this question a number of times to understand what you are getting at. This line They are structured in a way where UserLogin's id is a PK and FK to UserProfile and the same goes for UserProfile initially seemed to mean that UserLogin has a foreign key to UserProfile and UserProfile has a foreign key to UserLogin But I now think that what you're trying to do is use the same values for the PK in both tables; if I have UserLoginId 123 and I then go create a profile, you want me to have UserProfileId 123 Why are you doing this? It's okay from a database perspective (although I'd argue not particularly beneficial, and somewhat confusing), but LINQ-to-SQL wants a primary key that it knows is the primary key for a single row, and not used as a primary key for any other table.

And it wants a foreign key that isn't also a primary key The value 123 in the UserProfile table is really a Foreign Key to UserLogin but not a primary key. It doesn't need to be the primary key and you're making things more confusing for LINQ-to-SQL by doing so. Let UserProfile have its own PK.

Create a auto-incrementing identity, and change what you've got to be the foreign key back to UserLogin All your problems will be resolved UserLogin PK - UserLoginId int identity UserProfile PK - UserProfileId int identity FK - UserLoginId int In my experience, both LINQ-to-SQL & Entity Framework work best with simple surrogate primary keys - independent of everything else. Anything too tricky or non-standard can cause you more headaches than any perceived benefit or simplicity.

I had to read this question a number of times to understand what you are getting at. This line They are structured in a way where UserLogin's id is a PK and FK to UserProfile and the same goes for UserProfile initially seemed to mean that UserLogin has a foreign key to UserProfile, and UserProfile has a foreign key to UserLogin. But I now think that what you're trying to do is use the same values for the PK in both tables; if I have UserLoginId = 123 and I then go create a profile, you want me to have UserProfileId 123.

Why are you doing this? It's okay from a database perspective (although I'd argue not particularly beneficial, and somewhat confusing), but LINQ-to-SQL wants a primary key that it knows is the primary key for a single row, and not used as a primary key for any other table. And it wants a foreign key that isn't also a primary key.

The value 123 in the UserProfile table is really a Foreign Key to UserLogin, but not a primary key. It doesn't need to be the primary key and you're making things more confusing for LINQ-to-SQL by doing so. Let UserProfile have its own PK.

Create a auto-incrementing identity, and change what you've got to be the foreign key back to UserLogin. All your problems will be resolved. UserLogin PK - UserLoginId int identity UserProfile PK - UserProfileId int identity FK - UserLoginId int In my experience, both LINQ-to-SQL & Entity Framework work best with simple surrogate primary keys - independent of everything else.

Anything too tricky or non-standard can cause you more headaches than any perceived benefit or simplicity.

Yea you got that right. That's precisely what I intend to do, to make UserProfile's id = UserLogin's id, skipping surrogate key. In EF, I was able to achieve this by playing around with fluent api but not in L2S.

Sadly, this project is still stuck to L2S, which would give me a hard time should I decide to convert it to EF4.1 Anyway, I'll suggest your approach to my DBA and see how we go from there. Thanks. – Jon Sep 22 at 15:22.

This code creates a new instance of our context and then uses it to insert a new Blog. Then it uses a LINQ query to retrieve all Blogs from the database ordered alphabetically by Title. You can now run the application and test it out.

What if My Database Changes?

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions