sql-server - VB.NET 似乎无法在两个 EF 代码第一表之间链接数据
问题描述
我正在尝试首先使用 EF 代码创建一个 Bookings/Accounts 数据库,但在链接数据时遇到了问题
一点背景
数据库目前在此上下文中汇集了 6 个表,但目前我只是试图对以下关联进行排序
Public Class CompanyContext
Inherits DbContext
Public Property Drivers As DbSet(Of Driver)
Public Property Addresses As DbSet(Of Address)
Public Property Contacts As DbSet(Of ContactDetail)
Public Property Bookings As DbSet(Of Booking)
Public Property Accounts As DbSet(Of Account)
Public Property Vehicles As DbSet(Of Vehicle)
End Class
后面会有更多的数据链接,但我最初的问题如下。我可以创建一个具有以下属性的帐户..
Public Class Account
Public Property Id As Integer
Public Property Shortcut As String = ""
Public Property PaymentMethod As PaymentType
Public Property CardNumber As Integer
Public Property CardExpiry As New Date(2000, 1, 1)
Public Property CardSecurity As Integer
Public Property AccountAddress As Address
Public Property Contact As ContactDetail
Public Property Active As Boolean = True
End Class
Public Class ContactDetail
Public Property Id As Integer
Public Property Telephone1 As String = ""
Public Property Telephone2 As String = ""
Public Property Email1 As String = ""
Public Property Email2 As String = ""
Public Property Fax As String = ""
End Class
Public Class Address
Public Property AddressId As Integer
Public Property Company As String = ""
Public Property FlatBuilding As String = ""
Public Property Road As String = ""
Public Property Town As String = ""
Public Property County As String = ""
Public Property Country As String = ""
Public Property PostCode As String = ""
End Class
TextBoxes
我使用来自等的数据创建每个帐户ComboBoxes
。在保存数据时,所有内容都保存在相应的表中。该表为和表Accounts
中的适当行存储了一个 Id,并且来自 的和数据正确存储在那里。Addresses
Contacts
Address
Contact
Account
问题
在检索特定的数据时Account
,不会返回Contact
orAddress
数据。我得到的只是具有默认值的属性。我尝试按照 MS 网站上的教程进行操作,但我总是会收到错误,具体取决于我尝试创建导航的方式。恐怕我不完全了解需要什么。这些教程都是用 C# 编写的,但我确定我明白了。
最终,我希望地址和联系数据成为一种可以从其他表链接到的公共池。但是,一旦我发现我现在哪里出错了,这可能会成为另一个问题。这些是我用来保存和检索数据的方法。
selectedAccount
是Account
类的一个实例
Private Sub AccountSave()
Using db As New CompanyContext
If newAccount = True Then
selectedAccount = New Account
End If
With selectedAccount
.AccountAddress.Company = TxtAccountCompany.Text
.Shortcut = TxtAccountShortcut.Text
Select Case CboAccountPaymentMethod.SelectedItem.ToString
Case "Account"
.PaymentMethod = PaymentType.Account
Case "Cash"
.PaymentMethod = PaymentType.CashTopay
Case "Card"
.PaymentMethod = PaymentType.CCToPay
End Select
.CardNumber = Integer.Parse(TxtAccountCardNumber.Text)
.CardExpiry = Date.Parse(MtbAccountCardExpiry.Text)
.CardSecurity = Integer.Parse(TxtAccountCardSecurity.Text)
.AccountAddress.FlatBuilding = TxtAccountFlatBuilding.Text
.AccountAddress.Road = TxtAccountRoad.Text
.AccountAddress.Town = TxtAccountTown.Text
.AccountAddress.County = TxtAccountCounty.Text
.AccountAddress.PostCode = TxtAccountPostcode.Text
.AccountAddress.Country = TxtAddressCountry.Text
.Contact.Telephone1 = TxtAccountTelephone1.Text
.Contact.Telephone2 = TxtAccountTelephone2.Text
.Contact.Email1 = TxtAccountEmail1.Text
.Contact.Email2 = TxtAccountEmail2.Text
End With
If selectedAccount.Id = 0 Then
db.Accounts.Add(selectedAccount)
End If
db.SaveChanges()
End Using
AccountClearInfo()
AccountDisableEditing()
AccountRefreshList()
End Sub
Private Sub AccountDisplayInfo()
If selectedAccount IsNot Nothing Then
TxtAccountCompany.Text = selectedAccount.AccountAddress.Company
TxtAccountShortcut.Text = selectedAccount.Shortcut
TxtAccountOustandingBalance.Text = selectedAccount.OustandingBalance.ToString
Select Case selectedAccount.PaymentMethod
Case PaymentType.Account
CboAccountPaymentMethod.SelectedText = "Account"
Case PaymentType.CCToPay
CboAccountPaymentMethod.SelectedText = "Card"
Case PaymentType.CashTopay
CboAccountPaymentMethod.SelectedText = "Cash"
End Select
TxtAccountCardNumber.Text = selectedAccount.CardNumber.ToString
MtbAccountCardExpiry.Text = selectedAccount.CardExpiry.ToShortDateString
TxtAccountCardSecurity.Text = selectedAccount.CardSecurity.ToString
TxtAccountFlatBuilding.Text = selectedAccount.AccountAddress.FlatBuilding.ToString
TxtAccountRoad.Text = selectedAccount.AccountAddress.Road.ToString
TxtAccountTown.Text = selectedAccount.AccountAddress.Town.ToString
TxtAccountCounty.Text = selectedAccount.AccountAddress.County.ToString
TxtAccountPostcode.Text = selectedAccount.AccountAddress.PostCode.ToString
TxtAccountCountry.Text = selectedAccount.AccountAddress.Country.ToString
TxtAccountTelephone1.Text = selectedAccount.Contact.Telephone1
TxtAccountTelephone2.Text = selectedAccount.Contact.Telephone2
TxtAccountEmail1.Text = selectedAccount.Contact.Email1
TxtAccountEmail2.Text = selectedAccount.Contact.Email2
End If
End Sub
如果这里有用的话,是在幕后生成的用于创建每个表的代码
CREATE TABLE [dbo].[Accounts] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[Shortcut] NVARCHAR (MAX) NULL,
[PaymentMethod] INT NOT NULL,
[CardNumber] INT NOT NULL,
[CardExpiry] DATETIME NOT NULL,
[CardSecurity] INT NOT NULL,
[Active] BIT NOT NULL,
[AccountAddress_AddressId] INT NULL,
[Contact_Id] INT NULL,
CONSTRAINT [PK_dbo.Accounts] PRIMARY KEY CLUSTERED ([Id] ASC),
CONSTRAINT [FK_dbo.Accounts_dbo.Addresses_AccountAddress_AddressId] FOREIGN KEY ([AccountAddress_AddressId]) REFERENCES [dbo].[Addresses] ([AddressId]),
CONSTRAINT [FK_dbo.Accounts_dbo.ContactDetails_Contact_Id] FOREIGN KEY ([Contact_Id]) REFERENCES [dbo].[ContactDetails] ([Id])
);
GO
CREATE NONCLUSTERED INDEX [IX_AccountAddress_AddressId]
ON [dbo].[Accounts]([AccountAddress_AddressId] ASC);
GO
CREATE NONCLUSTERED INDEX [IX_Contact_Id]
ON [dbo].[Accounts]([Contact_Id] ASC);
CREATE TABLE [dbo].[Addresses] (
[AddressId] INT IDENTITY (1, 1) NOT NULL,
[Company] NVARCHAR (MAX) NULL,
[FlatBuilding] NVARCHAR (MAX) NULL,
[Road] NVARCHAR (MAX) NULL,
[Town] NVARCHAR (MAX) NULL,
[County] NVARCHAR (MAX) NULL,
[Country] NVARCHAR (MAX) NULL,
[PostCode] NVARCHAR (MAX) NULL,
[Booking_Id] INT NULL,
[Booking_Id1] INT NULL,
CONSTRAINT [PK_dbo.Addresses] PRIMARY KEY CLUSTERED ([AddressId] ASC),
CONSTRAINT [FK_dbo.Addresses_dbo.Bookings_Booking_Id] FOREIGN KEY ([Booking_Id]) REFERENCES [dbo].[Bookings] ([Id]),
CONSTRAINT [FK_dbo.Addresses_dbo.Bookings_Booking_Id1] FOREIGN KEY ([Booking_Id1]) REFERENCES [dbo].[Bookings] ([Id])
);
GO
CREATE NONCLUSTERED INDEX [IX_Booking_Id]
ON [dbo].[Addresses]([Booking_Id] ASC);
GO
CREATE NONCLUSTERED INDEX [IX_Booking_Id1]
ON [dbo].[Addresses]([Booking_Id1] ASC);
CREATE TABLE [dbo].[ContactDetails] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[Telephone1] NVARCHAR (MAX) NULL,
[Telephone2] NVARCHAR (MAX) NULL,
[Email1] NVARCHAR (MAX) NULL,
[Email2] NVARCHAR (MAX) NULL,
[Fax] NVARCHAR (MAX) NULL,
CONSTRAINT [PK_dbo.ContactDetails] PRIMARY KEY CLUSTERED ([Id] ASC)
);
希望有人能告诉我我的方式的错误。
解决方案
推荐阅读
- r - 如何在 R 中生成随机图?
- android - Firebase 电话号码验证并不总是发送短信
- sql - 使用日期模式 mm/dd/yyyy HH:mm 将 Varbinary 转换为 XML SQL Server
- python - 由于 Cookie 处理,Python 请求 AJAX 响应与浏览器不同
- kernel - 按照 jcoder 的建议,只安装一次未签名的驱动程序
- sql - 满足条件时计算每个组中的行数Sql Server
- python - 当我尝试将查询集放在上下文中时,Django 上的 HTTP 代码 500
- google-chrome - 是否可以加速 Chrome 上的动画?
- javascript - react, setState 并让组件重新渲染
- python - 如何在 jupyter notebook 上自动完成?