0

我对 ADO.NET 相当陌生。我对所有基本的 INSERT 等都很好。但是现在,我在将记录插入包含外键的表中时遇到了问题。我已经做了一些研究,但仍然卡住了......所以这里是:

我想在名为 Professionals 的表中插入一条新记录。它有一个映射到不同表的外键。FK 是 WAPublicUserID。

见图片:

在此处输入图像描述

当我创建数据模型时,WAPublicUserID 未列在 Professional 数据模型的属性中。

见图片:

在此处输入图像描述

因此,当我尝试在我的代码中创建 INSERT 时,找不到 WAPublicUserID 字段,也无法插入记录。我希望使用的 WAPublicUserID 已经存在于 FK 映射到的 WAPublicUser 表中。

见图片:

在此处输入图像描述

如何在包含 WAPublicUser 表中现有记录的外键的 Professionals 表中插入新记录?谢谢!

4

3 回答 3

1

Someone has set "Include Foreign Key Properties in Model" to false.

Hence you have the navigation property of WAPublicUser but not the ForeignKey property.

This means you will have to Attach the relevant WAPublicUser object to the WAPublicUser property on the object you are trying to save.

I'd need a lot more code to know exactly what you are doing, but the basics of it are as follows:

If the WAPublicUser already exists:

  • Grab the existing entity from the database - OldEntity.

  • Update the OldEntity with the properties of the new one you are currently trying to save.

  • Save the (now updated) Old entity back to the database - because you have just read it, it should have the WAPublicUser reference already set.

If it doesn't:

  • Create a new WAPublicUser

  • Set the WAPublicuser property of the Professional object to the newly created WAPublicUser - that line goes where your code stops above.

  • myEnt.AddToProfessionals(pro);

  • myEnt.SaveChanges();
于 2012-07-13T18:48:46.560 回答
0

知道了。以下是它的工作原理,以防其他人阅读。#1 和#2 是焦点。

非常感谢@BonyT 让我走上了正确的道路......

using (JONDOEntities myEnt = new JONDOEntities())
        {    
            // #1) Need to create WAPublicUser object first
            var wap = (from w in myEnt.WAPublicUsers
                       where w.WAPublicUserID == 981
                       select w).FirstOrDefault();

            var proUser = (from p in myEnt.Professionals
                           where p.WAPublicUser.WAPublicUserID == wap.WAPublicUserID
                           select p).FirstOrDefault();

            // If the record does not exist in the Professional table, insert new record. 
            if (proUser == null)
            {
                JONDOModel.Professional pro = new JONDOModel.Professional()
                {                        
                    ProfessionalType = "unknown",
                    FirstName = "unknown",
                    LastName = "unknown",
                    PhoneNumber = "unknown",
                    WebsiteUrl = "unknown",
                    TaxID = "unknown",
                    BusinessInfo = "unknown",
                    ProfessionalLogo = "unknown",
                    IsApproved = true,
                    CATaxExempt = false,                       
                    WAPublicUser = wap     // #2) Plug in the WAPublicUser object here
                };

                myEnt.AddToProfessionals(pro);
                myEnt.SaveChanges();
            }
于 2012-07-16T06:34:21.127 回答
0

好的,这是我的 OP 的真正答案:

我接手管理的 asp.net 网站的目标是 .NET 3.5。显然,3.5 和实体框架存在一些问题。

我将网站转换为目标 .NET 4.0(*见下文了解如何)。当我去创建实体数据模型时,瞧,默认情况下,它现在包含外键,因此,我没有 OP 中描述的任何问题。

如果遇到这种情况,您必须确保 Web 服务器也升级到 .NET 4.0。因为如果您将网站文件升级/转换为目标 .NET 4.0,但您的 Web 服务器尚未升级,那么尽管该网站在您的开发机器上运行流畅(假设它具有 .NET 4.0 框架),它会崩溃实时网络服务器。

顺便说一句,.NET 4.0 框架将运行使用以前版本的 .NET(向后兼容性)构建的应用程序......但是,以 .NET 4.0 为目标的应用程序将无法在具有 .NET 3.5 框架或更早版本的环境中运行。

将网站转换为 .NET 4.0:

将网站转换/升级到 .NET 4.0 的两种方法。1) 通常,当您打开一个新副本并且它的目标是 4.0 时,Visual Studio 会询问您是否要转换/升级(选择是)。2)在visual studio(商业版)中,单击WEBSITE选项卡,START OPTIONS,BUILD ...然后您应该看到更改“Target Framework”的选项...

于 2012-08-02T17:24:38.243 回答