|
I have the following models:
public class User
{
[Key]
public int UserId { get; set; }
public string Name { get; set; }
public string LastName { get; set; }
public string? ImageUrl { get; set; }
public virtual List<Request> Requests { get; set; }
public virtual List<Message> Messages { get; set; }
}
public class Request
{
[Key]
public int RequestId { get; set; }
public DateTime RequestTime { get; set; } = DateTime.Now;
public bool? AcceptStatus { get; set; }
public User TargetUserRef { get; set; }
public User SenderUserRef { get; set; }
}
I want the Request table to have two foreign keys. I tried to use the following fluent API:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Request>()
.HasOne(m => m.SenderUserRef)
.WithMany(t => t.Requests)
.HasForeignKey(m => m.RequestId)
.OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<Request>()
.HasOne(m => m.TargetUserRef)
.WithMany(t => t.Requests)
.HasForeignKey(m => m.RequestId)
.OnDelete(DeleteBehavior.Restrict);
}
When I try to create migration, the following error is shown:
Quote: Cannot create a relationship between 'User.Requests' and 'Request.TargetUserRef' because a relationship already exists between 'User.Requests' and 'Request.SenderUserRef'. Navigations can only participate in a single relationship. If you want to override an existing relationship call 'Ignore' on the navigation 'Request.TargetUserRef' first in 'OnModelCreating'.
How can I solve this problem?
|
|
|
|
|
This is where you build the (test) "database first" and have EF generate the model it undertstands; instead of having to guess what your "model building" code is trying to do. Then you can try again with having some idea of what works.
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
|
|
|
|
|
To me this is why relying on these sorts of APIs leads to problems.
Any API that attempts to simplify something always reduces complexity by removing features. That is of course the definition of simplification.
That works only as long as the needed usage is in fact simple. And more importantly that it is expected that over time it will remain simple.
If not then it will start to fail at some time. Then attempting to then use the API to solve the problem becomes a mess. Or perhaps even worse the API itself will introduce odd constructs with perhaps even odd restrictions and rules in an attempt to work around the original limitations. Those themselves will lead to problems over time.
|
|
|
|
|
You can't use the same collection navigation property to represent the requests where the user is the sender and the requests where the user is the target.
You also can't use the RequestId property as the foreign key to the users table - you're not creating a one-to-one relationship, so the request ID and the user ID will be different.
public class User
{
...
public List<Request> SentRequests { get; set; }
public List<Request> ReceivedRequests { get; set; }
...
}
...
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Request>()
.HasOne(m => m.SenderUserRef)
.WithMany(t => t.SentRequests)
.OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<Request>()
.HasOne(m => m.TargetUserRef)
.WithMany(t => t.ReceivedRequests)
.OnDelete(DeleteBehavior.Restrict);
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Is it possible to highlight all rows in listbox between two index values?
If it possible how is it done?
|
|
|
|
|
Member 14055879 wrote: Is it possible to highlight all rows in listbox between two index values? Yes
Member 14055879 wrote: If it possible how is it done? By writing code. Beyond that answer, we can't really support you as there are too many open questions. What technology are you using here? WPF? WinForms? Maui? Platform x...? What do you mean by highlighting? Bolding the text? Changing the background colour? Causing the rows to blink?
When you ask a question of us, you have to remember that, unless you give us details, we don't know exactly what you want to achieve. Try to add as much detail as you can and that should help you get an answer closer to the one you want.
|
|
|
|
|
Surely someone as proficient at getting a
@ tag to make his post text stick out would have no troubles translating VBNET into C#: 50[^]
Wowie!
|
|
|
|
|
Message Closed
modified 16-Mar-23 3:48am.
|
|
|
|
|
If I copy'n'paste your code into an online C# compiler:
using System;
public class Program
{
public static void Main()
{
string inputString = "A1+0001852Kg 054";
string weightValueString = inputString.Substring(3, inputString.IndexOf("Kg") - 3);
Console.WriteLine(weightValueString);
double weightKg = double.Parse(weightValueString) / 1000;
string formattedWeight = weightKg.ToString("N2");
Console.WriteLine(formattedWeight);
}
} And run it, I get what I expect:
0001852
1.85 Which means that the problem isn't the code: it's the input string that you are actually processing - and we have no access to that, to it's source hardware, or to the code that reads it from the machine.
So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. If you don't know how to use it then a quick Google for "Visual Studio debugger" should give you the info you need.
Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.
Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
modified 16-Mar-23 3:19am.
|
|
|
|
|
|
You do have to wonder sometimes, don't you?
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
I'd say for the OP to use N3, rather than N2, but as I can't see the question on the page right now, I can't directly answer it.
|
|
|
|
|
That bit of code without the initial string assignment and the usual "It don't work" message I'm afraid.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Ah, the usual "here are all the steps I've tried, debugging operations I've undertaken, logs and traces", where the OP has reached out as a last resort eh?
|
|
|
|
|
And even delete his own question! Cleans up after himself.
|
|
|
|
|
I have an update process in my app that downloads an installer and runs it. If the user cancels the installer it throws a Win32Exception. The problem is that in the Catch all I get is the Exception with the message "The operation was cancelled by the user". Other than the text of the message, there's really no way to know for sure what exception occurred. I don't want to rely on the exception message.
Any way to trap this specific issue?
Here's my code:
private static void StartInstaller()
{
try
{
_ = Process.Start(_localSetupFile);
Process[] processes;
do
{
processes = Process.GetProcessesByName(_processName);
Thread.Sleep(1000);
}
while (processes.Length == 0);
}
catch (Win32Exception e)
{
}
catch (Exception e)
{
throw;
}
}
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
Is the installer an .MSI?
|
|
|
|
|
Have you looked at ClickOnce? I have a Silent Updater for .Net Framework v3.5+ that takes out all of the hard work: Silent ClickOnce Installer for Winform & WPF in C# & VB[^]. If your app is Dot Net Core Framework, I have a new article that I will release very soon, just need to finish the article writeup.
Graeme
"I fear not the man who has practiced ten thousand kicks one time, but I fear the man that has practiced one kick ten thousand times!" - Bruce Lee
|
|
|
|
|
Win32Exception has a NativeErrorCode property which should return 1223 (0x4C7) when the user stops a process starting at a UAC prompt.
An example from my own code where I do something similar is probably clearer than more words!
private static void RestartWithRunAs(ProcessStartInfo psi) {
psi.UseShellExecute = true;
psi.ErrorDialog = true;
psi.Verb = "runas";
try {
Process.Start(psi);
} catch (System.ComponentModel.Win32Exception exc) {
const Int32 CancelledByUser = 1223;
if (exc.NativeErrorCode != CancelledByUser) {
throw;
}
}
}
See also
System Error Codes (1000-1299) (WinError.h) - Win32 apps | Microsoft Learn[^]
Alan.
|
|
|
|
|
Thanks. I'll give it a try
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
If this is not allowed please delete...
I'm looking for a programmer that can help me with a c# program I've started.
It's a CAD style program to design simple framework structures.
It's not a project for a larger development company, and I've tried websites that advertise professionals.
Mostly looking for help, but if someone is available to do more thats great too!!
Thanks,
Tom
|
|
|
|
|
This is not a recruitment site: I suggest you go to Freelancer.com and ask there.
But be aware: you get what you pay for. Pay peanuts, get monkeys.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Understood. That's why I began with delete if not permitted.
Not trying to recruit or poach. looking for help with a program I'm writing.
|
|
|
|
|
There is also Fiverr[^] and Triplebyte[^]. The last one looks better than Fiver or Freelancer.
Hope this helps.
Graeme
"I fear not the man who has practiced ten thousand kicks one time, but I fear the man that has practiced one kick ten thousand times!" - Bruce Lee
|
|
|
|
|
I've just noticed a new icon at the bottom right of CP messages. Just before the orange ribbon icon (bookmark this) and red flag icon (abusive / inappropriate / spam) there is now a bent black arrow icon. I haven't risked clicking it as there is no tooltip to say what it does. Does anyone know what is it for?
|
|
|
|