c# - 对象有关系时使用LINQ to SQL添加记录有什么秘诀吗?

标签 c# asp.net asp.net-mvc linq linq-to-sql

我正在使用 LINQ to SQL、ASP.NET MVC 和 C#。我有一个名为 genesisRepository 的存储库连接到数据库。

我有一个在名为 Stream 的对象中表示的表。它是这样定义的:

[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); }
    }

}

我在我的 Controller 中有一个测试操作,用于在我的 Stream 中创建新记录表。

    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不为空,新创建的对象stream显然也不为空。我唯一能想到的是 null EntitySet<T>定义外部关系的属性。

因此,我将 ActionResult 代码修改为:

    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");
    }

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

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 ('Name', 'url');"); 创建新记录.

我不明白如何从这里前进。我怎样才能简单地在 Stream 中创建一条新记录? ?

最佳答案

尝试以下操作。在您的类声明中替换它:

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

关于c# - 对象有关系时使用LINQ to SQL添加记录有什么秘诀吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3876360/

相关文章:

c# - Linq 扩展方法 select not in

c# - 如何在C#中通过字符串访问类成员?

c# - GridView 和 objectDataSource

c# - 如何检查每个列表成员的属性值是否相同

ASP.NET - 设计者和开发者之间的协作

ASP.net 多数据库链接

c# - 如何在 Windows 窗体中平滑地重新绘制面板

jquery - 如何使用 JQuery、通过 ID 选择元素和 ASP.NET,而不在代码中到处放置 ctl00_

c# - 该类型定义在一个未被引用的程序集中,如何查找原因?

asp.net-mvc - MVC模型未成功更新但找不到原因