Linq to SQL System.InvalidOperationException : Member AutoSync failure
I've got a couple of Linq to SQL entities which are causing me problems:
[Table(Name = "Vi开发者_运维技巧ewName")]
[InheritanceMapping(Code = false, Type = typeof(Entity1), IsDefault = true)]
[InheritanceMapping(Code = true, Type = typeof(Entity2))]
public class Entity1
{
[Column(AutoSync = AutoSync.OnInsert, DbType = "uniqueidentifier NOT NULL", IsPrimaryKey = true, IsDbGenerated = true)]
public Guid Bssid { get; set; }
// other properties
[Column(AutoSync= AutoSync.OnInsert ,DbType = "Int NOT NULL IDENTITY", IsDbGenerated = true)]
public int NewSslid { get; set; }
}
public class Entity2 : Entity1
{
public Entity2()
{
Discriminator = true;
_options = new EntitySet<Entity3>();
}
}
The entities are using an updatable view rather than a database table as there are about 150 fields spread across two tables. The view runs with all CRUD functions working as they should, but I get the following error when trying to Insert either entity type to the database:
System.InvalidOperationException : Member AutoSync failure. For members to be Auto-Synced after insert, the type must either have an auto-generated identity, or a key that is not modified by the database after insert.
The DB table for Entity1 uses a PK and a separate field (NewSslid) as the identity - which in turn is used as the PK on Entity2 field.
Can anyone point me in the right direction to getting this error sorted?
What is generating the GUID on the Bssid field, it sounds like this is being set by a defaultvalue of newid() in the database. Link2SQL is complaining that this gets set by the DB after insert so it can't sync it because it does not know what value it has been set to. i.e. there is no equivelent of @@identity or similar.
By altering Entity1:
public class Entity1
{
public Entity1()
{
Guid = System.Guid.NewGuid();
}
[Column(IsDbGenerated = true)]
public Guid Bssid { get; set; }
[Column(IsPrimaryKey = true, IsDbGenerated = false, Name = "Guid")]
public Guid? Guid { get; set; }
// other properties
[Column(AutoSync = AutoSync.OnInsert , DbType = "Int NOT NULL IDENTITY", IsDbGenerated = true)]
public int NewSslid { get; set; }
}
And adding the 'Guid' column to the Entity1 table with a default value of newid()
精彩评论