Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3216764
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:19:58+00:00 2026-05-17T15:19:58+00:00

This is my first attempt at updating a database using LINQtoSQL. At least, it’s

  • 0

This is my first attempt at updating a database using LINQtoSQL. At least, it’s my first if you don’t count the tutorials I’ve followed. Unfortunately, The tutorials that I have found don’t offer much more than updating a single table. I’m attempting to update a DB Model that’s a bit more complex.

I have a Stream table:

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

    /** Other columns removed for brevity **/

    // relationship:
    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); }
    }

And I have a Stream2FieldTypes table:

[Table]
public class Stream2FieldTypes
{
    [Column(IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert)]
    public long s2fID { get; set; }
    public long StreamID { get; set; }     // FK

    /** other columns removed for brevity **/

    // relationship (one Stream2FieldTypes to many Streams) 
    private EntitySet<Stream> _Stream;
    [Association(Storage = "_Stream", ThisKey = "StreamID")]
    public EntitySet<Stream> Stream
    {
        get { return this._Stream; }
        set { this._Stream.Assign(value); }
    }

Now, I am trying to update the model so I can send updates to the repository to persist to DataContext. I can’t update Stream.Stream2FieldTypes because the get is set to a private EntitySet.

How do I update Stream.Stream2FieldTypes when I can’t change Stream.Stream2FieldTypes because it’s a private EntitySet<>?

Edit: psuedo code

Basically, I think I should be able to update the Stream and Stream2FieldTypes tables by using a command in my Edit Action like this:

myRepository.SaveStream(stream);

I have been trying to do something like this:

        if (ModelState.IsValid)
        {
            // Convert StreamEditModel to Stream
            var stream = new Genesis.Domain.Entities.Stream
            {
                StreamID = form.StreamID,
                StreamUrl = form.StreamUrl,
                StreamName = form.StreamName,
                StreamBody = form.StreamBody,
                StreamTitle = form.StreamTitle,
                StreamKeywords = form.StreamKeywords,
                StreamDescription = form.StreamDescription,
                Stream2FieldTypes = new EntitySet<Stream2FieldTypes>()
            };

            // Loop to convert Stream2FieldTypes to Steam2FieldTypesEditModel
            foreach (var item in form.Stream2FieldTypes)
            {
                var fieldTypeEntry = new Stream2FieldTypes
                {
                    FieldTypeID = item.FieldTypeID,
                    s2fID = item.s2fID,
                    s2fIsRequired = item.s2fIsRequired,
                    s2fLabel = item.s2fLabel,
                    StreamID = item.StreamID,
                };
                stream.Stream2FieldTypes.Add(fieldTypeEntry); // Add to list
            }

            genesisRepository.SaveStream(stream);

            return RedirectToAction("Index");
        }
        else 
        {
            return View(form);
        }

When I try to run this code, I get 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 with error: Line 60: set { this._Stream2FieldTypes.Assign(value); }

Line 61: }

Line 62:

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T15:19:58+00:00Added an answer on May 17, 2026 at 3:19 pm

    In your SaveStream method, you set the StreamID property like this StreamID = item.StreamID. Because you have a Stream id I expect you are trying to change an existing Stream class. However, when looking at your code, you are creating a new one. I think the problem you are experiencing is caused by that.

    What you should do is the following:

    • Differentiate between creating a new one and mutating an existing entity.
    • Try to minimize the use of ID properties as much as you can. Work with the entities itself. This makes your code much cleaner and readable. (I always make the ID properties internal and the EntitySet and EntityRef public).

    For instance, I think your SaveStream should look more like the next code. If you look closely, this code lacks lines such as:

    • FieldTypeID = item.FieldTypeID and
    • StreamID = form.StreamID.

    This is because this simply wont work. You need to retrieve an existing entity from the database first and update it. You can’t create a new object, set its ID to an existing record in the database and expect LINQ to SQL to update that record for you. This is not how LINQ to SQL is designed.

    Here is an example of what might work for you:

    public void SaveStream(StreamEditModel stream)
    {
        if (!ModelState.IsValid)
        {
            return;
        }
    
        if (stream.Id == 0)
        {
            CreateStream(stream);
        }
        else
        {
            UpdateStream(stream);
        }
    }
    
    private void CreateStream(StreamEditModel form)
    {
        var stream = new Stream();
    
        FillStream(stream, form);
    
        UpdateStream2FieldTypes(stream, form);
    
        genesisRepository.SubmitChanges();
    }
    
    private void UpdateStream(StreamEditModel form)
    {
        var stream = genesisRepository.GetById(stream.StreamID);
    
        FillStream(stream, form);
    
        UpdateStream2FieldTypes(stream, form);
    
        genesisRepository.SubmitChanges();
    }
    
    private void FillStream(Stream stream, StreamEditModel form)
    {
        stream.StreamUrl = form.StreamUrl;
        stream.StreamName = form.StreamName;
        stream.StreamBody = form.StreamBody;
        stream.StreamTitle = form.StreamTitle;
        stream.StreamKeywords = form.StreamKeywords;
        stream.StreamDescription = form.StreamDescription;
    }
    
    private void UpdateStream2FieldTypes(Stream stream,
        StreamEditModel form)
    {
        var typesToDelete =
            from type in stream.Stream2FieldTypes
            let ids = form.Stream2FieldTypes.Select(t => t.FieldTypeID)
            where !ids.Contains(type.FieldTypeID)
            select type;
    
        genesisRepository.RemoveStream2FieldTypes(typesToDelete);
    
        var typesToAdd =
            from type in form.Stream2FieldTypes
            where type.FieldTypeID == 0
            select CreateStream2FieldTypes(type);
    
        foreach (var typeToAdd in typesToAdd)
        {
            stream.Stream2FieldTypes.Add(typeToAdd);
        }
    
        var formTypesToUpdate = 
            from type in form.Stream2FieldTypes
            where type.FieldTypeID != 0
            select type;
    
        foreach (var modelToUpdate in formTypesToUpdate)
        {
            var typeToUpdate = stream.Stream2FieldTypes.Single(
                t => t.FieldTypeID == modelToUpdate.FieldTypeID);
    
            FillStream2FieldTypes(typeToUpdate, typeToUpdate);
        }
    }
    
    private static  Stream2FieldTypes CreateStream2FieldTypes(
        Stream2FieldTypesEditModel form)
    {
        var fieldType = new Stream2FieldTypes();
    
        FillStream2FieldTypes(fieldType, form);
    
        return fieldType;
    }
    
    private static void FillStream2FieldTypes(
        Stream2FieldTypes type, 
        Stream2FieldTypesEditModel item)
    {
        type.s2fID = item.s2fID;
        type.s2fIsRequired = item.s2fIsRequired;
        type.s2fLabel = item.s2fLabel;
    }
    

    Cheers

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my first time using joomla. I don't know if I'm using the
This is my first attempt at using Backbone.js, so I decided to make a
problem euler #5 i found the solution but i don't know why this first
This is my first experience using the Zend Framework. I am attempting to follow
This is my first crack at a method that is run periodically during the
To preface, this is my first attempt at MVVM... I buy it, I'm just
This is my first attempt to throw data back and forth between a local
This is my first attempt to inline Java code in Perl. We cannot use
This is my first attempt at writing a QT app, and I'm just trying
This is my first post here and I wanted to get some input from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.