I am working in LINQ to SQL, ASP.NET MVC, and C#. I have a repository called genesisRepository
to connect to the database.
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); }
}
}
I have a test action in my controller for creating new records inside my Stream
table.
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");
}
The function genesisRepository()
looks like this:
public long CreateStream(Stream stream)
{
streamTable.InsertOnSubmit(stream);
streamTable.Context.SubmitChanges();
return stream.StreamID;
}
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.
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
I can create a new record using streamTable.Context.ExecuteCommand("INSERT INTO genesis.dbo.Stream (StreamName, StreamUrl) VALUES ('Name', 'url');");
.
I don't understand how to move forward from here. How can I simply create a new record in Stream
?