簡體   English   中英

Linq to SQL System.InvalidOperationException:成員自動同步失敗

[英]Linq to SQL System.InvalidOperationException : Member AutoSync failure

我有幾個Linq to SQL實體,這些實體導致我遇到問題:

[Table(Name = "ViewName")]
[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>();
    }
}

實體使用的是可更新視圖,而不是數據庫表,因為在兩個表之間分布着約150個字段。 該視圖運行時所有CRUD函數均按其應有的方式運行,但是在嘗試將任一實體類型插入數據庫時​​出現以下錯誤:

System.InvalidOperationException:成員自動同步失敗。 若要使成員在插入后自動同步,該類型必須具有自動生成的標識,或具有插入后數據庫未修改的密鑰。

Entity1的DB表使用PK和單獨的字段(NewSslid)作為標識-依次用作Entity2字段上的PK。

誰能指出我正確的方向以解決此錯誤?

是什么在Bssid字段上生成GUID,聽起來像是由數據庫中newid()的默認值設置的。 Link2SQL抱怨說這是在插入后由數據庫設置的,因此它無法同步,因為它不知道它已設置為什么值。 即沒有@@ identity或類似的東西。

通過更改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; }

 }

並將'Guid'列添加到Entity1表中,其默認值為newid()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM