Click here to Skip to main content
14,211,543 members
Rate this:
Please Sign up or sign in to vote.
See more:
Mates,

I have a SQL Server table <entity> which is in a certain administration. The table <entity> has a relation with <entitystatus>.

<entity>
Id (int, PK, auto-increment)
AdminstrationId int
Status int
Name varchar
....

<entitystatus>
Id (int, PK, auto-increment)
AdminstrationId int
Number int
Name varchar

As you can see, both tables have a Id field which is a PK and have auto-increment on.

Now I want entity framework to create a relation between both AdministrationId fields, and the <entity>.Status & <entitystatus>Number field.

How can I do this?

The code below in the 'what have you tried', actually works for the relation, but it removes the Id field as a PK of the status table. This causes an error when I enter new status values in the <entitystatus> table

What I have tried:

modelBuilder.Entity<Entity>()
    .HasKey(c => new { c.AdministrationId, c.Number });

modelBuilder.Entity<Entity>()
    .HasRequired(p => p.EntityStatus)
    .WithMany(x => x.Entities)
    .HasForeignKey(p => new { p.AdministrationId, p.Status });
Posted
Updated 7-Sep-17 8:16am

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

I usually build tables/relationships for SQL in a SQL utility (such as SQL Server Management Studio).

That aside, I would still following something of the same route: create the tables, set primary key(s), and the relationships as discreet SQL Queries and not parametized with C# code.

I prefer letting SQL handle SQL (even if via C#, C++, php, ...)
Establish a SQL connection
Set the Query Strings
Execute them
   
Comments
Eduard Keilholz 8-Sep-17 3:00am
   
That is exactly what I do, I just need to define the relation within my datacontext so I can use the include ion my linq queries to also fetch the statusses related to the main entity. And instead of using the ID in the entity, I'd like to use the status number because most uses have a fixed and recognizable status number.
W Balboos 8-Sep-17 6:39am
   
Here I must profess some ignorance; the last time I used LINQ its SQL was a bit spotty and I defer to your expertise in LINQ over mine.

With that in mind, I wonder is it LINQ that loses your PK? Is it supposed to be aware of and make use of the table's primary key (and identity fields) - which would require it to remain connected.

Part of the excuse for my weakness w/LINQ is that I work with a DBA - we're mutually symbiotic - and have learned to create the original SQL so the returned data is what I need and how I need it. Probably, this lack will cost me at some point but my TSQL is much better than it used to be. Even for parent-child select lists in HTML (which could be handled very conveniently in a LINQ context).

But, for what things similar to what you are doing, problems have arisen

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Cookies | Terms of Service
Web01 | 2.8.190619.2 | Last Updated 7 Sep 2017
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100