带有Db生成属性的SubmitChanges上的NullReferenceException

时间:2013-09-18 01:55:28

标签: c# sql-server linq-to-sql

我在这里结束了。问题是我正在尝试在LINQ to SQL中使用db生成的标识属性在数据库中创建一个新实体,然后创建与第一个实体关联的另一个实体。我猜测问题是LINQ to SQL无法在没有第一个实体的id的情况下插入第二个实体,直到数据库生成它才会知道。

有没有其他人有这个问题...如果是这样你是如何解决的?我知道我可以在创建第一个和第二个实体之间调用SubmitChanges,但这会破坏程序的事务完整性。

这是一个具体的例子:

[Table(Name = "Searches")]
public class Search
{
    // Db Generated Key
    [Column(IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert)]
    public Int32 SearchID { get; set; }

    // Each search can have multiple search parameters
    private EntitySet<SearchParam> searchParams;

    [Association(Storage = "searchParams", ThisKey = "SearchID", OtherKey = "SearchID")]
    public EntitySet<SearchParam> SearchParams
    {
        get
        {
            return searchParams;
        }
        set
        {
            searchParams.Assign(value);
        }
    }
}

[Table(Name = "SearchParams")]
public class SearchParam
{
    [Column(IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert)]
    public Int32 SearchParamID { get; set; }

    [Column]
    public String ParamValue { get; set; }

    // Each search param is associated with one search
    [Column]
    public Int32 SearchID { get; set; }

    private EntityRef<Search> search = new EntityRef<Search>();

    [Association(Storage = "search", ThisKey = "SearchID", OtherKey = "SearchID", IsForeignKey = true)]
    public Search Search
    {
        get
        {
            return search.Entity;
        }
        set
        {
            search.Entity = value;
        }
    }
}

因此,使用上面的代码,如果我要执行以下操作,.NET会在NullReferenceException上为我提供SubmitChanges

using (SampleDataContext context = new SampleDataContext())
{
    Search search = new Search();
    search.SearchParams.Add(new SearchParam() { ParamValue = "...paramvalue..." });
    context.Searches.InsertOnSubmit(search);
    context.SubmitChanges();
}

3 个答案:

答案 0 :(得分:0)

不知道为什么你的工作不起作用,但这应该有用

using (SampleDataContext context = new SampleDataContext())
{
     Search search = new Search();
     SearchParam param = new SearchParam() { ParamValue = "...paramvalue..." })
     param.search = search
     context.searchparams.InsertOnSubmit (param ) // Not sure if this is necessary
     context.Searches.InsertOnSubmit(search);
     context.SubmitChanges();
}

全部在一次交易中。

答案 1 :(得分:0)

我不认为你在调用SearchParams之前正在初始化add列表。这有用吗:

using (SampleDataContext context = new SampleDataContext()) {
    Search search = new Search();
   search.SearchParams = new EntitySet<SearchParam>(); //<-- THIS LINE
    search.SearchParams.Add(new SearchParam() { ParamValue = "...paramvalue..." });
    context.Searches.InsertOnSubmit(search);
    context.SubmitChanges();
}

答案 2 :(得分:0)

尝试将以下内容添加到您的搜索类

    public Search()
    {
        this.SearchParams = new EntitySet<SearchParams >(
         new Action<SearchParams >(this.attach_SearchParams ), 
         new Action<SearchParams >(this.detach_SearchParams ));
    }

    private void attach_SearchParams(SearchParams sp)
    {
        sp.Search = this;
    }

    private void detach_Search(SearchParams sp)
    {
        sp.Search = null;
    }