• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1396
  • Last Modified:

LINQ to SQL throwing exception when IsPrimaryKey = true

I have manually created a LINQ to SQL entity, however when I specifiy IsPrimaryKey = true, I get this exception:

System.TypeLoadException: GenericArguments[2], 'System.Int32', on 'System.Data.Linq.Mapping.PropertyAccessor+Accessor`3[T,V,V2]' violates the constraint of type parameter 'V2'.

The exception is thrown any time a User entity is attempted to be used, for ex. DataContext.GetTable<User>()

The column is defined correctly as the primary key in SQL server. There are other entities which have Primary Keys set up exactly this way and they work just fine.

If I drop the IsPrimaryKey decoration, it generally works fine... except you cant Update, Insert or Delete without a primary key ;)

See the attached file for the stack trace.
public partial class User
    [Column(IsPrimaryKey = true, Name = "UserID", IsDbGenerated = true, AutoSync = AutoSync.OnInsert, DbType = "int NOT NULL IDENTITY")]
    public int ID { get; set; }

    // more columns, methods, etc...
    // ...

Open in new window

1 Solution
Seems like there is something wrong with your mapping. Verify the xml mapping file for this entity object and other entities with primary key and you should be able to fix it.
VeonikAuthor Commented:
I took your advice and kept trying different things. There was a problem mapping one of the properties to the database.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now