Thursday, October 31, 2019

"see inner exception" runaround when trying to set the location of a TFS workspace to local

The suggestion I see at Stack Overflow is that if you see this error you are trying to pull down too much locally and you had better chunk it up some if you really need all of it.

Delete Shelveset

To destroy one of your shelvesets in TFS, "go a finding" for your own shelvesets like so and then right-click on a line item and pick: Delete Shelveset

C# 8 is finally here as of .NET Core 3.0.0.

Both were released timed with .NET Conf on September 23rd it would seem. I just heard about it last night myself.

Get rid of server sync with TFS.

In Visual Studio 2017 at the Team Explorer's main splash page, reachable by clicking on the "Home" icon at the top which looks like a house, expand "Solutions" (below the eight buttons for "Project") and at the Workspace setting click the downwards pointing blue arrow. Next click Manage Workspaces... Edit a workspace here. At the dialog box for editing a workspace click Advanced > > and then set "Location:" from "Server" to "Local" before pressing the OK button.

The L.I.O.N. acronym that people put by their names on LinkedIn stands for LinkedIn Open Networker it would seem.

You know people and you're going places, get it? This is some wacky pseudostreetcred. Fun, fun.

Wednesday, October 30, 2019

At the Pending Changes tab in the Team Explorer in Visual Studio 2107, click on Pending Changes to pick one of the other top tier menu items of the Team Explorer.

You don't have to go back to the "Home" view for everything as it turns out. A little menu of items, such as "Builds" for example, will materialize when you take this step. At Builds you may click Builds to similarly go back to Pending Changes. (Builds shows you if the most recent build passed or failed.)

Cannot insert explicit value for identity column in table 'Players' when IDENTITY_INSERT is set to OFF.

What are we to do about this error in Entity Framework Core implementations? Well, this might be an actual opportunity for me to provide some value with my blog as all of the Stack Overflow stuff I see just suggests decorating the field which cannot be written to with...

[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]

 
 

...but that does not get at what was going on for me. The problem lay in attempting to add children of a parent and the column in question was to hold the reference key to the parent. In my case the parent was a Player which looked like so:

using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace FluffyNothing.Core.Objects
{
   public class Player
   {
      [Key]
      [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
      public long PlayerId { get; set; }
      [Required]
      [MaxLength(15)]
      public string PlayerIp { get; set; }
      public virtual ICollection<Message> Messages { get; set; }
   }
}

 
 

The players have messages and the Message object looks like this:

using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace FluffyNothing.Core.Objects
{
   public class Message
   {
      [Key]
      [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
      public long MessageId { get; set; }
      [Required]
      [ForeignKey("PlayerId")]
      public Player Player { get; set; }
      [Required]
      public string Copy { get; set; }
      public DateTime Time { get; set; }
   }
}

 
 

Alright, the thing that threw the error looked like so:

public void WriteMessages(Message player1Message, Message player2Message)
{
   var optionsBuilder = new DbContextOptionsBuilder<PlayerContext>();
   using (var context = new PlayerContext(optionsBuilder.Options))
   {
      using (var transaction = context.Database.BeginTransaction())
      {
         context.Messages.Add(player1Message);
         context.Messages.Add(player2Message);
         context.SaveChanges();
         transaction.Commit();
      }
   }
}

 
 

I'm just trying to jam the messages in the database. That won't work. Instead I needed to query the parents and make an association to the parents. I did that like this:

public void WriteMessages(Message player1Message, Message player2Message)
{
   var optionsBuilder = new DbContextOptionsBuilder<PlayerContext>();
   using (var context = new PlayerContext(optionsBuilder.Options))
   {
      using (var transaction = context.Database.BeginTransaction())
      {
         List<Player> twoPlayers = context.Players.Where(p => p.PlayerId ==
               player1Message.Player.PlayerId || p.PlayerId ==
               player2Message.Player.PlayerId).ToList();
         foreach (Player player in twoPlayers)
         {
            if (player.Messages == null) player.Messages = new List<Message>();
            if (player.PlayerId == player1Message.Player.PlayerId)
            {
               player.Messages.Add(player1Message);
            }
            if (player.PlayerId == player2Message.Player.PlayerId)
            {
               player.Messages.Add(player2Message);
            }
         }
         context.SaveChanges();
         transaction.Commit();
      }
   }
}