0

我是 C# 的一个相当绿色的初学者(总共培训 1 年)。我的任务是通过 Visual Studio 2010 在数据库中创建表——我是通过 EF 完成的。现在我想编写一个简单的控制台程序来用值填充这些表。在 SQL MS 中,它们看起来是正确的。在我的代码的最后几行中,我得到了 CreateBestellung() 方法只接受两个参数的错误消息。这将是我在此表中创建的两列。但是如何为外键字段赋值呢?到目前为止找不到任何关于此的信息。

private static void bestellungAnlegen(playgroundEntities context)
    {
        Console.Clear();
        Console.WriteLine("Neue Bestellung anlegen");

        int kdId = -1;
        int wrId = -1;
        int anzahl = 0;
        int id = -1;

        // Validierung der Werte
        do
        {
            Console.Write("Kunden ID: ");
            if (int.TryParse(Console.ReadLine(), out kdId))
            {
                kdId = int.Parse(Console.ReadLine());
            }

        } while (kdId == -1);

        do
        {
            Console.Write("Waren ID: ");
            if (int.TryParse(Console.ReadLine(), out kdId))
            {
                kdId = int.Parse(Console.ReadLine());
            }

        } while (wrId == -1);

        do
        {
            Console.Write("Anzahl: ");
            if (int.TryParse(Console.ReadLine(), out anzahl))
            {
                if (anzahl >= 0)
                {
                    anzahl = int.Parse(Console.ReadLine());

                }
                else anzahl = 0;

            }

        } while (anzahl == 0);

        Bestellung neueBestellung =
            Bestellung.CreateBestellung(id, anzahl, kdId, wrId);
        context.Bestellungen.AddObject(neueBestellung);
    }
4

1 回答 1

0

这只能是一个疯狂的(但有根据的)猜测。我认为方法调用应该是

Bestellung neueBestellung = Bestellung.CreateBestellung(id, anzahl);

其次是

neueBestellung.kdId = kdId;
neueBestellung.wrId = wrId;
于 2013-06-26T08:30:28.357 回答