Linq to SQL Warning DBML1062

S

SftwDude

I get the following warning in my C# program:

DBML1062: The Type attribute 'tblPart' of the Association element
'tblAssembly_tblPart' of the Type element 'tblAssembly' does not have a
primary key. No code will be generated for the association.

tblAssembly and tblPart form a one-to-many relationship with the Primary Key
in tblAssembly. I built the relationship by dragging both tables onto the
Object Relational Designer from the Server Explorer. The relationship already
exists in the SQL database, so the association was automatically drawn on the
designer. I've tried deleting the association and rebuilding it by hand using
the Association in the Toobox. Still get the same warning.

How can I resolve this warning? It is preventing me from assessing the Part
class entity directly from the Assembly class entity.

Thanks
 
J

Jon Skeet [C# MVP]

SftwDude said:
I get the following warning in my C# program:

DBML1062: The Type attribute 'tblPart' of the Association element
'tblAssembly_tblPart' of the Type element 'tblAssembly' does not have a
primary key. No code will be generated for the association.

tblAssembly and tblPart form a one-to-many relationship with the Primary Key
in tblAssembly. I built the relationship by dragging both tables onto the
Object Relational Designer from the Server Explorer. The relationship already
exists in the SQL database, so the association was automatically drawn on the
designer. I've tried deleting the association and rebuilding it by hand using
the Association in the Toobox. Still get the same warning.

How can I resolve this warning? It is preventing me from assessing the Part
class entity directly from the Assembly class entity.

Well, do both tables have a primary key? If not, fixing that seems the
best solution.
 
S

SftwDude

No the second table does not have a primary key. It does not contain any
fields that are unique.

The Part field in the tblPart can contain the same part number several times
in the table.
 
J

Jon Skeet [C# MVP]

SftwDude said:
No the second table does not have a primary key. It does not contain any
fields that are unique.

The Part field in the tblPart can contain the same part number several times
in the table.

I suspect that LINQ to SQL won't like that. Many ORMs have difficulty
with situations like that, particular because it makes it basically
impossible to update the entity. I can see how you may not need a PK
when using SQL directly if you're just reading from the table, but I'd
advise including one anyway.
 
J

Jialiang Ge [MSFT]

Hello SftwDude and Jon,

Yes, indeed. You are right. The warning message "DBML1062" is due to the
lack of PK in the table tblPart. We also received several reports of this
problem in MSDN forum:
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2510382&SiteID=1
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2896891&SiteID=1
And the resolution is to, as Jon said, add a PK to the table.

SftwDude, though the second table (tblPart) does not contain any fields
that are unique, we can add a PK to it in one of the following ways:

Method 1. Add an int-typed column (e.g. tblPartID) to the table tblPart,
and fill the value 1, 2, 3, 4¡­ to this column for the existing items. Then
set the PartID column as PK, and set its Identity Specification as True in
SQL Server Table designer so that when new item is inserted into the table,
the value of PartID could be automatically generated.

Method 2. In the table tblPart, if multiple columns together (e.g. the
combination of part number and part name) can identify one item, we can
turn the fields into a PK:
alter table tblPart add constraint applied_tags_PK primary key
(part_number, part_name);

If you have any other concerns or questions, please feel free to let me
know.

Regards,
Jialiang Ge ([email protected], remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
S

SftwDude

Thanks Jon and Jialiang.

It added an Identity field and made it a Primary Field as you suggested.
This works, now I get the power of having non flat entity and having the
DataContext managing CRUD and concurrency, etc...

I was reluctant to add a field to the table that is not necessary, but the
benefit out ways the additional field.

Thanks again
 
J

Jialiang Ge [MSFT]

No problem. You are welcome. :)

Jialiang Ge ([email protected], remove 'online.')
Microsoft Online Community Support

=================================================
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no rights.
=================================================
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top