Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 在嵌套字段中插入_Sql_Linq_Linq To Sql - Fatal编程技术网

Sql 在嵌套字段中插入

Sql 在嵌套字段中插入,sql,linq,linq-to-sql,Sql,Linq,Linq To Sql,我是LINQtoSQL的新用户,在使用它时遇到了一些问题。 我使用了LINQtoSQL设计器,并创建了映射到DB表的类。 特别是,我有一门课,叫做voice: [global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.voce")] public partial class voce : INotifyPropertyChanging, INotifyPropertyChanged { private static Prope

我是LINQtoSQL的新用户,在使用它时遇到了一些问题。 我使用了LINQtoSQL设计器,并创建了映射到DB表的类。 特别是,我有一门课,叫做voice:

[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.voce")]
public partial class voce : INotifyPropertyChanging, INotifyPropertyChanged
{

    private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);

    private int _id_voce;

    ... other private fields;



    private int _category;

    private EntityRef<category> _category1;
            public voce()
    {
        this._riepilogo = new EntitySet<riepilogo>(new Action<riepilogo>(this.attach_riepilogo), new Action<riepilogo>(this.detach_riepilogo));
        this._hera = default(EntityRef<hera>);
        this._category1 = default(EntityRef<category>);
        OnCreated();
    }

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_id_voce", AutoSync=AutoSync.OnInsert, DbType="Int NOT NULL IDENTITY", IsPrimaryKey=true, IsDbGenerated=true)]
    public int id_voce
    {
        get
        {
            return this._id_voce;
        }
        set
        {
            if ((this._id_voce != value))
            {
                this.Onid_voceChanging(value);
                this.SendPropertyChanging();
                this._id_voce = value;
                this.SendPropertyChanged("id_voce");
                this.Onid_voceChanged();
            }
        }
    }

    ......
    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_category", DbType="Int NOT NULL")]
    public int category
    {
        get
        {
            return this._category;
        }
        set
        {
            if ((this._category != value))
            {
                if (this._category1.HasLoadedOrAssignedValue)
                {
                    throw new System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException();
                }
                this.OncategoryChanging(value);
                this.SendPropertyChanging();
                this._category = value;
                this.SendPropertyChanged("category");
                this.OncategoryChanged();
            }
        }
    }
特别是,如果已经存在,则从DB检索类别字段;如果不存在,则在我插入语音之前插入类别字段

问题是,当我在数据库中添加语音时:

datacontext.InsertOnSubmit(v);
datacontext.SubmitChanges();
它再次插入类别,但使用唯一约束失败

如何在不添加所有嵌套对象的情况下添加语音


谢谢你,很抱歉我的英语不好。

我觉得问题可能出在
//创建或检索类别
。它可能总是在创建新的类别,或者不将已存档的类别返回到当前的
datacontext
。你能把那个方法的代码贴出来吗?
internal category GetCategoryFromDescription (string desc, Utility.VOICE_MODALITY mode)
    {
        bool type = mode == Utility.VOICE_MODALITY.ENTRATA ? true : false;
        var query = from cat in dc.category
                    where cat.description == desc && cat.type == type
                    select cat;

        if (query.Count() == 0)
        {
            category newC =  new category() { description = desc };
            dc.category.InsertOnSubmit(newC);
            dc.SubmitChanges();
            return newC;
        }
        else
            return query.Single();

    }
internal category GetCategoryFromDescription (string desc, Utility.VOICE_MODALITY mode)
    {
        bool type = mode == Utility.VOICE_MODALITY.ENTRATA ? true : false;
        var query = from cat in dc.category
                    where cat.description == desc && cat.type == type
                    select cat;

        if (query.Count() == 0)
        {
            category newC =  new category() { description = desc };
            dc.category.InsertOnSubmit(newC);
            dc.SubmitChanges();
            return newC;
        }
        else
            return query.Single();

    }