Note that there are some explanatory texts on larger screens.

plurals
  1. POIs there a secret to using LINQ to SQL to add records when the object has relationships?
    text
    copied!<p>I am working in LINQ to SQL, ASP.NET MVC, and C#. I have a repository called <code>genesisRepository</code> to connect to the database. </p> <p>I have a table represented in an object called Stream. It is defined like this:</p> <pre><code>[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&lt;StreamEntry&gt; _StreamEntry; [System.Data.Linq.Mapping.Association(Storage = "_StreamEntry", OtherKey = "StreamID")] public EntitySet&lt;StreamEntry&gt; StreamEntry { get { return this._StreamEntry; } set { this._StreamEntry.Assign(value); } } private EntitySet&lt;Stream2FieldTypes&gt; _Stream2FieldTypes; [System.Data.Linq.Mapping.Association(Storage = "_Stream2FieldTypes", OtherKey = "StreamID")] public EntitySet&lt;Stream2FieldTypes&gt; Stream2FieldTypes { get { return this._Stream2FieldTypes; } set { this._Stream2FieldTypes.Assign(value); } } } </code></pre> <p>I have a test action in my controller for creating new records inside my <code>Stream</code> table.</p> <pre><code> 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"); } </code></pre> <p>The function <code>genesisRepository()</code> looks like this:</p> <pre><code> public long CreateStream(Stream stream) { streamTable.InsertOnSubmit(stream); streamTable.Context.SubmitChanges(); return stream.StreamID; } </code></pre> <p>I get a null reference error when I execute the ActionResult <code>StreamTest()</code>. genesisRepository is not null. <code>streamTable</code> is not null, and the newly create object <code>stream</code> is also obviously not null. The only thing I can think of that would be null are the <code>EntitySet&lt;T&gt;</code> properties which define the foreign relationships.</p> <p>So, I modified the ActionResult code to be this:</p> <pre><code> public ActionResult StreamTest() { // create new stream var stream = new Genesis.Domain.Entities.Stream(); stream.Stream2FieldTypes = new EntitySet&lt;Stream2FieldTypes&gt;(); // new stream.StreamEntry = new EntitySet&lt;StreamEntry&gt;(); // 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"); } </code></pre> <p>But when I tried that, I got this error:</p> <pre><code>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); } &lt;-- the offending line Line 61: } Line 62: Source File: C:\path\to\Stream.cs Line: 60 </code></pre> <p>I can create a new record using <code>streamTable.Context.ExecuteCommand("INSERT INTO genesis.dbo.Stream (StreamName, StreamUrl) VALUES ('Name', 'url');");</code>.</p> <p>I don't understand how to move forward from here. How can I simply create a new record in <code>Stream</code>?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload