是否有一个秘密使用LINQ to SQL添加记录时,对象有关系? [英] Is there a secret to using LINQ to SQL to add records when the object has relationships?

查看:91
本文介绍了是否有一个秘密使用LINQ to SQL添加记录时,对象有关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在工作LINQ到SQL,ASP.NET MVC,和C#。我有一个名为 genesisRepository 库连接到数据库。

I am working in LINQ to SQL, ASP.NET MVC, and C#. I have a repository called genesisRepository to connect to the database.

我在叫流的对象psented表重新$ P $。它的定义是这样的:

I have a table represented in an object called Stream. It is defined like this:

[Table]
public class Stream
{
    [HiddenInput(DisplayValue = false)]
    [Column(IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert)]
    public long StreamID { get; set; }

    [Required(ErrorMessage = "Please enter a stream name")]
    [Column]
    public string StreamName { get; set; }

   /* Other columns removed for brevity */ 

    [Required(ErrorMessage = "Please enter a stream URL")]
    [Column]
    public string StreamUrl { get; set; }

    private EntitySet<StreamEntry> _StreamEntry;
    [System.Data.Linq.Mapping.Association(Storage = "_StreamEntry", OtherKey = "StreamID")]
    public EntitySet<StreamEntry> StreamEntry
    {
        get { return this._StreamEntry; }
        set { this._StreamEntry.Assign(value); }
    }

    private EntitySet<Stream2FieldTypes> _Stream2FieldTypes;
    [System.Data.Linq.Mapping.Association(Storage = "_Stream2FieldTypes", OtherKey = "StreamID")]
    public EntitySet<Stream2FieldTypes> Stream2FieldTypes
    {
        get { return this._Stream2FieldTypes; }
        set { this._Stream2FieldTypes.Assign(value); }
    }

}

我在我的控制器中的测试动作对我的表内创造新的纪录。

    public ActionResult StreamTest()
    {
        // create new stream
        var stream = new Genesis.Domain.Entities.Stream();

        stream.StreamUrl = "url";
        stream.StreamName = "name";
        stream.StreamBody = null;
        stream.StreamTitle = null;
        stream.StreamKeywords = null;
        stream.StreamDescription = null;

        genesisRepository.CreateStream(stream); 

        return View("Index");
    }

功能 genesisRepository()是这样的:

    public long CreateStream(Stream stream)
    {            
        streamTable.InsertOnSubmit(stream);
        streamTable.Context.SubmitChanges();
        return stream.StreamID;
    }

我得到一个空引用错误,当我执行的ActionResult StreamTest()。 genesisRepository不为空。 streamTable 不为空,而新创建对象也显然不为空。我能想到的唯一的事情是空是的EntitySet&LT; T&GT; 它定义了对外关系的属性。

I get a null reference error when I execute the ActionResult StreamTest(). genesisRepository is not null. streamTable is not null, and the newly create object stream is also obviously not null. The only thing I can think of that would be null are the EntitySet<T> properties which define the foreign relationships.

所以,我修改了的ActionResult code是这样的:

So, I modified the ActionResult code to be this:

    public ActionResult StreamTest()
    {
        // create new stream
        var stream = new Genesis.Domain.Entities.Stream();

        stream.Stream2FieldTypes = new EntitySet<Stream2FieldTypes>(); // new
        stream.StreamEntry = new EntitySet<StreamEntry>(); // new
        stream.StreamUrl = "url";
        stream.StreamName = "name";
        stream.StreamBody = null;
        stream.StreamTitle = null;
        stream.StreamKeywords = null;
        stream.StreamDescription = null;

        genesisRepository.CreateStream(stream); // CreateStream() returns ID as long

        return View("Index");
    }

但是,当我尝试了,我得到这个错误:

But when I tried that, I got this error:

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error: 
Line 58:         {
Line 59:             get { return this._Stream2FieldTypes; }
Line 60:             set { this._Stream2FieldTypes.Assign(value); } <-- the offending line
Line 61:         }
Line 62: 
Source File: C:\path\to\Stream.cs    Line: 60 

我可以通过创建一个新的纪录 streamTable.Context.ExecuteCommand(INSERT INTO genesis.dbo.Stream(streamName中,StreamUrl)VALUES('名','链接'););

我不明白如何从这里向前迈进。我怎么可以简单地创建一个新的记录

I don't understand how to move forward from here. How can I simply create a new record in Stream?

推荐答案

请尝试以下方法。在你的类声明替换这样的:

Try the following. Replace this in your class declaration:

public class Stream
{   
    private EntitySet<StreamEntry> _StreamEntry;
    private EntitySet<Stream2FieldTypes> _Stream2FieldTypes;
}

本:

public class Stream
{   
    private EntitySet<StreamEntry> _StreamEntry = new EntitySet<StreamEntry>();
    private EntitySet<Stream2FieldTypes> _Stream2FieldTypes = new EntitySet<Stream2FieldTypes>();
}

这篇关于是否有一个秘密使用LINQ to SQL添加记录时,对象有关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆