Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

Linq to SQL Warning DBML1062

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
SftwDude - 16 Mar 2008 21:02 GMT
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
Jon Skeet [C# MVP] - 16 Mar 2008 21:15 GMT
> I get the following warning in my C# program:
>
[quoted text clipped - 11 lines]
> 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.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

SftwDude - 16 Mar 2008 21:32 GMT
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.
Jon Skeet [C# MVP] - 16 Mar 2008 21:50 GMT
> 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.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Jialiang Ge [MSFT] - 17 Mar 2008 08:29 GMT
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 (jialge@online.microsoft.com, 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:
msdnmg@microsoft.com.

==================================================
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.
SftwDude - 17 Mar 2008 13:07 GMT
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
Jialiang Ge [MSFT] - 18 Mar 2008 03:49 GMT
No problem. You are welcome. :-)

Jialiang Ge  (jialge@online.microsoft.com, 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:
msdnmg@microsoft.com.

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

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.