kianyuen
kianyuen
CC#
Created by kianyuen on 11/11/2024 in #help
Migration not reflecting Model correctly
Context: I'm using .NET core 6.0, ASP.NET MVC, trying to initiate the db with Migrations Got this error:
PostgresException: 23502: null value in column "parent_id" of relation "post" violates not-null constraint DETAIL: Detail redacted as it may contain sensitive data. Specify 'Include Error Detail' in the connection string to include this information.
PostgresException: 23502: null value in column "parent_id" of relation "post" violates not-null constraint DETAIL: Detail redacted as it may contain sensitive data. Specify 'Include Error Detail' in the connection string to include this information.
I clearly defined the ParentId to be nullable here:
public class Post
{
public int Id { get; set; }
public PostType Type { get; set; }
public int? ParentId { get; set; }
public virtual Post Parent { get; set; }
[InverseProperty("Parent")]
public virtual ICollection<Post> Answers { get; set; }
public int? AcceptedAnswerId { get; set; }
public virtual Post AcceptedAnswer { get; set; }
public int UserId { get; set; }
public User User { get; set; }
[StringLength(255)]
public string Title { get; set; }
[Column(TypeName = "text")]
public string Body { get; set; }
public virtual ICollection<Tag> Tags { get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
public Post()
{
Tags = new HashSet<Tag>();
Answers = new HashSet<Post>();
}
}
public class Post
{
public int Id { get; set; }
public PostType Type { get; set; }
public int? ParentId { get; set; }
public virtual Post Parent { get; set; }
[InverseProperty("Parent")]
public virtual ICollection<Post> Answers { get; set; }
public int? AcceptedAnswerId { get; set; }
public virtual Post AcceptedAnswer { get; set; }
public int UserId { get; set; }
public User User { get; set; }
[StringLength(255)]
public string Title { get; set; }
[Column(TypeName = "text")]
public string Body { get; set; }
public virtual ICollection<Tag> Tags { get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
public Post()
{
Tags = new HashSet<Tag>();
Answers = new HashSet<Post>();
}
}
Any suggestion on how I can fix this? I think I can just modify the generated migration file, but I'm afraid that would break the migrations.
6 replies
CC#
Created by kianyuen on 8/14/2024 in #help
Controls from WPF.UI can't render properly on WPF, .NET Framework 4.8
No description
20 replies
CC#
Created by kianyuen on 8/6/2024 in #help
How to stream bytes from SerialPort (System.IO.Ports) into a chart.
I'm using WPF on .NET Framework 4.8. The packet that I'm trying to catch is sent at 250Hz. I was under the assumption My current basic implementation is as follow: On port.DataReceived, I will collect the data then store it to another buffer to further process it later (to deal with potential last unfinished packet by just queueing them).
SerialPort spL = (SerialPort)sender;
byte[] buf = new byte[spL.BytesToRead];
spL.Read(buf, 0, buf.Length);
_receiveBuffer.AddRange(buf);
SerialPort spL = (SerialPort)sender;
byte[] buf = new byte[spL.BytesToRead];
spL.Read(buf, 0, buf.Length);
_receiveBuffer.AddRange(buf);
I personally think this is hacky and non-efficient, is there a better existing approach that I'm not aware of?
13 replies
CC#
Created by kianyuen on 7/26/2024 in #help
How should Error Status Code returned from a DLLImport function be handled?
//Output: None
//return: 0 - success, <0 - failure(refer to error code definition)
//Note: None
[DllImport("./libraries/something.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int OpenDevice(int nPort, int nBaud);
//Output: None
//return: 0 - success, <0 - failure(refer to error code definition)
//Note: None
[DllImport("./libraries/something.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int OpenDevice(int nPort, int nBaud);
I am thinking defining a different function that wrap it and throw exception. Or should I just use the function as is?
8 replies
CC#
Created by kianyuen on 7/18/2024 in #help
ViewModel Command ran when I debugged, but the View is not updated
This is my ViewModel:
public class MainViewModel : ViewModelBase
{
private ViewModelBase _currentChildView;
private string _title;

public ViewModelBase CurrentChildView
{
get
{
return _currentChildView;
}
set
{
_currentChildView = value;
OnPropertyChanged(nameof(CurrentChildView));
}
}
public string Title
{
get => _title;
set
{
_title = value;
OnPropertyChanged(nameof(Title));
}
}

// Commands for user interactions
public ICommand ShowBloodPressureViewCommand { get; }
public ICommand ShowBMIViewCommand { get; }
public ICommand ShowHeightWeightViewCommand { get; }


public MainViewModel()
{
ShowBloodPressureViewCommand = new RelayCommand(ExecuteShowBloodPressureViewCommand);
ShowBMIViewCommand = new RelayCommand(ExecuteShowBMIViewCommand);
ShowHeightWeightViewCommand = new RelayCommand(ExecuteShowHeightWeightViewCommand);

ExecuteShowBloodPressureViewCommand(null);
}

private void ExecuteShowHeightWeightViewCommand(object obj)
{
CurrentChildView = new HeightWeightViewModel();
Title = "Height Weight";

}

private void ExecuteShowBMIViewCommand(object obj)
{
CurrentChildView = new BMIViewModel();
Title = "BMI";
}

private void ExecuteShowBloodPressureViewCommand(object obj)
{
CurrentChildView = new BloodPressureViewModel();
Title = "Blood Pressure";
}
}
public class MainViewModel : ViewModelBase
{
private ViewModelBase _currentChildView;
private string _title;

public ViewModelBase CurrentChildView
{
get
{
return _currentChildView;
}
set
{
_currentChildView = value;
OnPropertyChanged(nameof(CurrentChildView));
}
}
public string Title
{
get => _title;
set
{
_title = value;
OnPropertyChanged(nameof(Title));
}
}

// Commands for user interactions
public ICommand ShowBloodPressureViewCommand { get; }
public ICommand ShowBMIViewCommand { get; }
public ICommand ShowHeightWeightViewCommand { get; }


public MainViewModel()
{
ShowBloodPressureViewCommand = new RelayCommand(ExecuteShowBloodPressureViewCommand);
ShowBMIViewCommand = new RelayCommand(ExecuteShowBMIViewCommand);
ShowHeightWeightViewCommand = new RelayCommand(ExecuteShowHeightWeightViewCommand);

ExecuteShowBloodPressureViewCommand(null);
}

private void ExecuteShowHeightWeightViewCommand(object obj)
{
CurrentChildView = new HeightWeightViewModel();
Title = "Height Weight";

}

private void ExecuteShowBMIViewCommand(object obj)
{
CurrentChildView = new BMIViewModel();
Title = "BMI";
}

private void ExecuteShowBloodPressureViewCommand(object obj)
{
CurrentChildView = new BloodPressureViewModel();
Title = "Blood Pressure";
}
}
When initialized, ExecuteShowBloodPressureViewCommand ran normally, but when I use UI to run other commands, the Execute function still ran, but the View doesn't update.
18 replies
CC#
Created by kianyuen on 7/10/2024 in #help
Managed Debugging Assistant 'PInvokeStackImbalance'
I have a dll file that is originally used in a .NET Framework 3.5 Project. I am trying to port that module to .NET Framework 4.8, but Managed Debugging Assistant keeps throwing the error mentioned above. Turning off the MDA allowed the program to run, but is that recommended? This is how I import a function from the dll file
[DllImport("CsnPrinterLibs.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
public static extern uint Port_EnumCOM(byte[] buffer, int length);
[DllImport("CsnPrinterLibs.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
public static extern uint Port_EnumCOM(byte[] buffer, int length);
11 replies