Link to home
Start Free TrialLog in
Avatar of Veonik
VeonikFlag for United States of America

asked on

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.
[Table(Name="Users")]
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

stacktrace.txt
ASKER CERTIFIED SOLUTION
Avatar of Ravi Vaddadi
Ravi Vaddadi
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Veonik

ASKER

I took your advice and kept trying different things. There was a problem mapping one of the properties to the database.