我以前见过这个问题,但我已经在我的代码中实现了所有接受的答案,我仍然得到错误。
我有许多类(客户端,供应商等),每个都有一个列表(地址),所以每个类可以有许多不同的地址。这些类不是相互继承的,而是实现了指向ID、ClassType和List(of Address)属性的IAddresses。(我的代码是在VB,但我了解足够的c#,如果你在c#中提供解决方案,他们将是有用的)。
我遇到的问题是,当我尝试向客户端添加新地址时,我得到以下错误:Entities in 'DataContext.Addresses' participate in the 'Supplier_Addresses' relationship. 0 related 'Supplier_Addresses_Source' were found. 1 'Supplier_Addresses_Source' is expected.
My Supplier配置:
Public Class SupplierConfiguration
Inherits EntityTypeConfiguration(Of Supplier)
Public Sub New()
[Property](Function(x) x.Email).HasMaxLength(200)
[Property](Function(x) x.GivenName).HasMaxLength(50).IsRequired()
[Property](Function(x) x.PhoneH).HasMaxLength(20)
[Property](Function(x) x.PhoneM).HasMaxLength(20)
[Property](Function(x) x.Status).IsRequired()
[Property](Function(x) x.Surname).HasMaxLength(50).IsRequired()
[Property](Function(x) x.Title).HasMaxLength(5)
HasMany(Function(x) x.Comments).WithRequired(Function(x) x.Parent)
HasMany(Function(x) x.Addresses).WithRequired(Function(x) x.Parent)
Ignore(Function(x) x.ParentType)
End Sub
End Class
My Client Configurations:
Public Class ClientConfiguration
Inherits EntityTypeConfiguration(Of Client)
Public Sub New()
[Property](Function(x) x.Alert).IsMaxLength()
[Property](Function(x) x.Email).HasMaxLength(200)
[Property](Function(x) x.EntityName).HasMaxLength(100).IsRequired()
[Property](Function(x) x.Status).IsRequired()
HasMany(Function(x) x.Addresses).WithRequired(Function(x) x.Parent)
HasMany(Function(x) x.Comments).WithRequired(Function(x) x.Parent)
HasMany(Function(x) x.Invoices).WithOptional(Function(x) x.Client)
HasMany(Function(x) x.Jobs).WithRequired(Function(x) x.Client)
Ignore(Function(x) x.ParentType)
End Sub
End Class
我使用存储库模式进行持久化,下面是我的UnitOfWork类:
Imports System.Data.Entity
Public Class UnitOfWork
Implements IUnitOfWork
Private ReadOnly Property _context As DataContext
Private _Addresses As AddressRepository
Private _Clients As ClientRepository
Private _Comments As CommentRepository
Private _Suppliers As SupplierRepository
Public ReadOnly Property Addresses As IAddressRepository Implements IUnitOfWork.Addresses
Get
Return _Addresses
End Get
End Property
Public ReadOnly Property Clients As IClientRepository Implements IUnitOfWork.Clients
Get
Return _Clients
End Get
End Property
Public ReadOnly Property Comments As ICommentRepository Implements IUnitOfWork.Comments
Get
Return _Comments
End Get
End Property
Public ReadOnly Property Suppliers As ISupplierRepository Implements IUnitOfWork.Suppliers
Get
Return _Suppliers
End Get
End Property
Public Sub New(context As DataContext)
_context = context
_Addresses = New AddressRepository(_context)
_Clients = New ClientRepository(_context)
_Comments = New CommentRepository(_context)
_Suppliers = New SupplierRepository(_context)
End Sub
Public Function Context() As DbContext
Return _context
End Function
Public Function Complete() As Integer Implements IUnitOfWork.Complete
Dim log As String = vbNullString
Dim errors = _context.GetValidationErrors
If errors.Count > 0 Then Stop
Dim ReturnValue As Integer
Try
ReturnValue = _context.SaveChanges
Catch ex As System.Data.Entity.Validation.DbEntityValidationException
ShowException(ex)
ReturnValue = 0
Catch ex2 As Exception
ShowException(ex2)
ReturnValue = 0
End Try
Return ReturnValue
End Function
Public Sub Dispose() Implements IDisposable.Dispose
_context.Dispose()
End Sub
End Class
我的供应商类:
Public Class Supplier
Implements IAddresses, IComments
Property ID As Integer Implements IAddresses.ID, IComments.ID
Property Email As String
Property EntityName As String
Property GivenName As String
Property PhoneH As String
Property PhoneM As String
Property Status As eSupplierStatus
Property Surname As String
Property Title As String
Property Addresses As Addresses Implements IAddresses.Addresses
Property Comments As Comments Implements IComments.Comments
ReadOnly Property ParentType As String Implements IComments.ParentType, IAddresses.ParentType
Get
Return Me.GetType.Name
End Get
End Property
End Class
我不明白为什么我试图保存一个地址到客户端类,但EF想要一个供应商类。它们应该完全独立吗?
EF在地址表上创建了非空的FK字段:Client_ID, Supplier_ID等,所以当为客户端保存地址时,Supplier_ID的空值会抛出错误。解决方案是将客户端和供应商配置更改为WithOptional而不是WithRequired
HasMany(Function(x) x.Addresses).WithOptional(Function(x) x.Parent)
(附加问题移至正确答案)