小编Jim*_*imi的帖子

DbInitializer失败,必须在根类型上配置密钥

最近,我将.NET 1.1.4代码迁移到.NET 2.0.0。一切似乎都正常。现在,我尝试通过执行以下操作添加新的模型/表:

dotnet ef update

我得到以下异常:

DbInitializer失败!无法在“ ApplicationUser”上配置密钥,因为它是派生类型。密钥必须在根类型“ IdentityUser”上配置

我有一个扩展IdentityUser的ApplicationUser和扩展IdentityRole的ApplicationRole,这是其他一些教程/ stackoverflow帖子所说的。

我不确定我可能还会错过什么。

在我的ApplicationUser类中,我有:
ICollection<IdentityUserRole<string>>

是否必须是像ApplicationUserRole这样的自定义类型?IdentityUserRole和IdentityRole有什么区别?


编辑:
我认为这可能是因为在我的context.cs文件中,OnModelCreating我有:

builder.Entity<ApplicationUser>()
    .ToTable("AspNetUsers")
    .HasKey(x => x.Id);
Run Code Online (Sandbox Code Playgroud)

Id是继承自IdentityUser


EDIT2:
所以我注释掉了这一行,该错误消失了。转到下一行,这基本上是相同的,但是对于我的ApplicationRole来说,它扩展了IdentityRole。我不太清楚这是什么意思。我是否需要覆盖id属性?


EDIT3:
好的,所以我将builder.Entity中的类型更改为IdentityUserIdentityRole。为什么在我必须使用的startup.cs中ApplicationUser以及ApplicationRole调用时可以解决此问题services.AddIdentity<ApplicationUser, ApplicationRole>

c# asp.net entity-framework asp.net-identity

2
推荐指数
1
解决办法
2074
查看次数

将SetWindowPos与多个监视器一起使用

使用user32.dllC#,我编写了下面看到的方法。使用窗口的处理手柄,它将在指定{x,y}位置设置窗口位置。

但是,在多监视器环境中,下面的代码仅将窗口位置设置为主监视器。我也希望能够选择哪个显示器。
有人可以解释一下如何使用SetWindowPos或结合其他user32.dll功能来实现吗?

[DllImport("user32.dll", SetLastError = true)]
static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, int uFlags);

private const int SWP_NOSIZE = 0x0001;
private const int SWP_NOZORDER = 0x0004;
private const int SWP_SHOWWINDOW = 0x0040;

public static void SetWindowPosition(Process p, int x, int y)
{
    IntPtr handle = p.MainWindowHandle;
    if (handle != IntPtr.Zero)
    {
        SetWindowPos(handle, IntPtr.Zero, x, y, 0, 0, SWP_NOZORDER | SWP_NOSIZE | SWP_SHOWWINDOW); …
Run Code Online (Sandbox Code Playgroud)

c# windows user32

2
推荐指数
1
解决办法
1099
查看次数

使用 Windows 默认应用程序打开文件

我正在使用 C# .NET 5.0 列出文件夹中的 PDF 文件,当双击某个项目时,它会使用 Windows 默认 PDF 查看器打开选定的 PDF。
我已经搜索了如何执行此操作,但解决方案代码引发了异常。

System.Diagnostics.Process.Start(@"C:\path\to\pdf\file.pdf");
Run Code Online (Sandbox Code Playgroud)

错误:

System.ComponentModel.Win32Exception: 'The specified executable is not a valid application for this OS platform.'
Run Code Online (Sandbox Code Playgroud)

有人能帮我吗?

c# pdf .net-5

2
推荐指数
1
解决办法
4150
查看次数

如何从命令中获取输出以实时显示在Form的控件中?

从网络上的各种来源中,我整理了以下代码,用于通过CMD.exe和捕获来自STDOUT和的输出来执行命令STDERR

public static class Exec
{
    public delegate void OutputHandler(String line);

    // <summary>
    /// Run a command in a subprocess
    /// </summary>
    /// <param name="path">Directory from which to execute the command</param>
    /// <param name="cmd">Command to execute</param>
    /// <param name="args">Arguments for command</param>
    /// <param name="hndlr">Command output handler (null if none)</param>
    /// <param name="noshow">True if no windows is to be shown</param>
    /// <returns>Exit code from executed command</returns>
    public static int Run(String path, String cmd, String args, …
Run Code Online (Sandbox Code Playgroud)

.net c# process winforms

1
推荐指数
1
解决办法
230
查看次数

如何使用面板上的图形类绘制多色文本?

我想在面板上绘制以下文本:

示例文本

这是一个多颜色的文本。

我找到了这篇关于绘制彩色文本的文章

我用单词替换了字符,但它不起作用。

输出截图

(我使用FillPath/DrawPath来绘制文本)

我的代码:

private void Form1_Paint(object sender, PaintEventArgs e)
    {
        const string txt = "C# Helper! Draw some text with each letter in a random color.";

        // Make the font.
        using (Font the_font = new Font("Times New Roman", 40,
            FontStyle.Bold | FontStyle.Italic))
        {
            // Make a StringFormat object to use for text layout.
            using (StringFormat string_format = new StringFormat())
            {
                // Center the text.
                string_format.Alignment = StringAlignment.Center;
                string_format.LineAlignment = StringAlignment.Center;
                string_format.FormatFlags = StringFormatFlags.NoClip;

                // Make …
Run Code Online (Sandbox Code Playgroud)

.net graphics drawstring winforms graphicspath

1
推荐指数
1
解决办法
2896
查看次数

如何使用 Paint 事件在鼠标坐标处绘制形状

我最近开始C#明显地编程,并试图做一个简单的WinForms应用程序,它采用鼠标坐标并根据坐标缩放矩形。

我面临的问题是我不知道如何调用使用更多参数的方法(在这种情况下是x,yPaintEventArgs)。或者,我确实知道如何处理PaintEvent.

这是整个代码,因为它很短而且很简单:

using System;
using System.Drawing;
using System.Windows.Forms;

public partial class Form1 : Form
{
    public void Form1_MouseMove(object sender, MouseEventArgs e)
    {
        int x = e.X; 
        int y = e.Y;
        String data = (x.ToString() + " " + y.ToString());
        DrawRect(Something, x, y);
    }

    PaintEventArgs pEventArgs;
    private void Form1_Paint(object sender, PaintEventArgs e)
    {

    }

    public void DrawRect(PaintEventArgs e, int rey, int rex)
    {
        Graphics gr = e.Graphics;
        Pen …
Run Code Online (Sandbox Code Playgroud)

.net c# graphics paintevent winforms

1
推荐指数
1
解决办法
1768
查看次数

如何绘制签名并将其作为位图保存到光盘?

我正在尝试执行签名捕获程序并将客户签名保存为PNGBMP格式。我的 Picturebox 代码运行良好,结果看起来比使用 draw 更好。我无法获取要保存的图像。

Imports System.Drawing
Public Class Form1
    Dim color As System.Drawing.Pen = Pens.Black
    Dim bmp As Bitmap

    Private Sub form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        bmp = New Bitmap(PictureBox1.Width, PictureBox1.Height)
        PictureBox1.Image = bmp
    End Sub

    Private Sub PictureBox1_MouseMove(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseMove
        Static last As New Point
        If e.Button = Windows.Forms.MouseButtons.Left Then
            PictureBox1.CreateGraphics.DrawLine(color, last.X, last.Y, e.X, e.Y)
        End If
        last = e.Location
    End Sub

    Private Sub CmdClear_Click(sender As …
Run Code Online (Sandbox Code Playgroud)

.net vb.net graphics gdi+ winforms

1
推荐指数
1
解决办法
1633
查看次数

如何更改搜索分隔符号?

作为参考,此代码来自对这个问题的回答:

突出显示颜色与 RichTextBox 文本中所有其他选择不同的单词或短语?

using System.Collections.Generic;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Windows.Forms;

private class TextSearcher
{
    private BindingSource m_bsMatches = null;
    private RichTextBox m_Rtb = null;

    public TextSearcher(RichTextBox rtb) : this(rtb, Color.Yellow, Color.Red) { }
    public TextSearcher(RichTextBox rtb, Color selectionColor, Color currentColor)
    {
        this.m_Rtb = rtb;
        SelectionColor = selectionColor;
        CurrentColor = currentColor;
    }

    public string CurrentKeywords { get; private set; } = string.Empty;
    public bool CaseSensitive { get; set; } = true;
    public int CurrentIndex => m_bsMatches.Position;
    public Match CurrentMatch …
Run Code Online (Sandbox Code Playgroud)

c# winforms

1
推荐指数
1
解决办法
53
查看次数

Option Strict On 不允许在 String 和 Object {String} 之间进行隐式转换

我正在尝试通过使用Option Strict On. 我设法清除了除此之外的所有错误。
我正在使用 ToolStrip 的 Tag 属性获取一些文本信息。单击 ToolStrip,我需要记住字符串中 Tag 的值并更改该 Tag 的值。

如何将Object {String} sender.tag 转换为String以及String myString 和Object {String}

Private Sub ToolStrip_ItemClicked(sender As Object, e As ToolStripItemClickedEventArgs) Handles ToolStrip.ItemClicked
    Dim myString As String = sender.tag
    sender.tag = "It is selected"
    'more code...
End Sub
Run Code Online (Sandbox Code Playgroud)

编辑:在此处查看代码相关部分的屏幕截图:

在此处输入图片说明

vb.net toolstripitem toolstrip winforms

1
推荐指数
1
解决办法
106
查看次数

String.Split() 使用 VbCrLf 不起作用,但首先用 "" 替换 VbCr 然后在 VbLf 上拆分确实有效。为什么?

我有一个嵌入的文本文件作为资源。内容是:

Apple
Pear
Orange
Run Code Online (Sandbox Code Playgroud)

我试图把它拉成一个List(Of String)但回车和换行符把它搞砸了。
例如我试试这个:

Dim myNames As List(Of String) = My.Resources.TreeNames.Split(CChar(vbCrLf)).ToList
Run Code Online (Sandbox Code Playgroud)

但是正在传递换行符:

"Apple"
vbLf & "Pear"
vbLf & "Orange"
Run Code Online (Sandbox Code Playgroud)

所以我尝试使用环境变量:

Dim myNames As List(Of String) = My.Resources.TreeNames.Split(CChar(Environment.NewLine)).ToList
Run Code Online (Sandbox Code Playgroud)

但这会导致完全相同的输出。
所以我尝试在换行符上拆分它:

Dim myNames As List(Of String) = My.Resources.TreeNames.Split(CChar(vbLf)).ToList
Run Code Online (Sandbox Code Playgroud)

现在正在传递回车符:

"Apple" & vbCr
"Pear" & vbCr
"Orange"
Run Code Online (Sandbox Code Playgroud)

所以我让这个工作的唯一方法是首先用空替换vbCr然后在左边拆分vbLf

Dim myNames As List(Of String) = Replace(My.Resources.TreeNames, vbCr, "").Split(CChar(vbLf)).ToList
Run Code Online (Sandbox Code Playgroud)

谁能解释为什么?如果我使用以下命令将文件直接拉入字符串:

Dim myNames as String = My.Resources.TreeNames
Run Code Online (Sandbox Code Playgroud)

我得到:

"Apple" & vbCrLf & "Pear" & vbCrLf & "Orange" …
Run Code Online (Sandbox Code Playgroud)

vb.net split carriage-return winforms linefeed

1
推荐指数
1
解决办法
130
查看次数