I've seen this question posted previously but I've implemented all the accepted answers in my code and I'm still getting the error.
I have a number of classes (Client, Supplier etc) that each have a List(of Address) so each class can have a number of different addresses. These classes are not inherited from each other but implement IAddresses which points to the ID, ClassType and List(of Address) properties. (My code is in VB but I understand enough C# that if you offer solutions in C# they will be useful).
The problem I'm having is that when I try to add a new address to a Client, I get the following error:
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 configurations:
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
I'm using the Repository Pattern for persistence, and here is my UnitOfWork class:
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
My Supplier 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
I can't understand why I'm trying to save an address to the Client class but EF wants a Supplier class. They should be completely independent?
EF created non-nullable FK fields on the Addresses table: Client_ID, Supplier_ID etc, so when saving an address for a Client the null value for Supplier_ID threw the error. Solution is to change the Client and Supplier configurations to WithOptional instead of WithRequired
HasMany(Function(x) x.Addresses).WithOptional(Function(x) x.Parent)
(Addition to the question moved to a proper answer)