在嵌套字段中插入

时间:2013-01-21 21:25:58

标签: sql linq linq-to-sql

我是LINQ to SQL的新用户,使用它时遇到了一些问题。 我使用过LINQ to SQL Designer,并创建了我的类,映射到数据库表。 特别是,我有一个名为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();
            }
        }
    }

如您所见,voce类有一个名为category的字段,它引用一个名为category的表。

当我向我的数据库中添加一个新的voce时,我创建了一个新的voce istance,使用DataContext,我只需添加它,使用:

voce v = new voce(){...field, category1 = //create or retrieve category};

特别是,如果已经存在,则从DB中检索类别字段,如果不存在,则在插入语音之前将其插入。

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

datacontext.InsertOnSubmit(v);
datacontext.SubmitChanges();

它再次插入类别,失败并带有唯一约束。

如何在不添加每个嵌套对象的情况下添加语音?

谢谢你,对不起我的英语不好。

1 个答案:

答案 0 :(得分:0)

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();

    }
相关问题