Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Entity framework EF-我无法通过创建将值指定给外键_Entity Framework - Fatal编程技术网

Entity framework EF-我无法通过创建将值指定给外键

Entity framework EF-我无法通过创建将值指定给外键,entity-framework,Entity Framework,我是C#的新手(总共1年的培训)。 我的任务是通过VisualStudio2010在数据库中创建表——我是通过EF创建的。现在我想编写一个简单的控制台程序,用值填充这些表。在SQL MS中,它们看起来是正确的。在代码的最后几行中,我得到了错误消息,CreateBestellung()方法只接受两个参数。这就是我在这个表中创建的两列。但是如何为外键字段赋值呢?到目前为止找不到关于这个的任何信息 private static void bestellungAnlegen(playgroundEnti

我是C#的新手(总共1年的培训)。 我的任务是通过VisualStudio2010在数据库中创建表——我是通过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);
    }
这只能是一个疯狂(但受过教育)的猜测。我认为方法调用应该是

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

neueBestellung.kdId = kdId;
neueBestellung.wrId = wrId;

您是否在数据模型类中显式声明了外部字段?如果您发布数据模型类代码,任何人都会更容易回答您的问题。您能否向我们展示
Bestellung.CreateBestellung(…)
?您的错误消息表明没有
CreateBestellung
接受您尝试传入的四个参数。非常感谢!它被标记为错误(在bestellung中没有对kdId的定义),但事实并非如此:neueBestellung.Kunde.Id=kdId;奇怪的是,我现在得到一个空引用异常。请显示方法
CreateBestellung
和类
Bestellung
(通过编辑您的帖子)。正如我所说,我们现在只能猜测。亲爱的格特,我在周末找到了帮助。我会在收到文件后立即发布结果,以防对其他人有所帮助,也便于您查看。再次感谢您随时准备提供帮助!neueBestellung.Kunde=Kunde;kunde通过前面调用的搜索方法接收其值,并返回kunde对象-显然,neueBestellung只能处理完整的对象,而不仅仅是Id。这取决于,如果您也给它一个
KdId
,它可以同时处理这两个对象。在实体框架中,带有原始FK值的引用称为外键关联。与独立关联相比,使用这些关联有许多优点(=仅参考,或:
.Kunde
仅参考)。