Varias instancias de IEntityChangeTracker no pueden hacer referencia a un objeto de entidad.
-
2012年3月13日 下午 01:21
Buenas...
Me encuentro con el siguiente problema (no se si a la hora de generar mis tablas en EF Code FIrst), pero que me esta llevando un poco de cabeza.
Para empezar.. Mi idea es generar una estructura en la Base de datos con el siguiente diagrama
Para ello, genero las siguientes entidades..
Namespace Entities <Table("Empresas")> Public Class Empresa Public Property Id As Integer Public Property Nombre As String Public Overridable Property Reglasempresas As ICollection(Of ReglasEmpresa) = New HashSet(Of ReglasEmpresa) End Class <Table("ReglasEmpresas")> Public Class ReglasEmpresa Public Property Id As Integer <Required()>Public Property Empresa As Empresa <Required()>Public Property Producto As Producto <Required()>Public Property Proveedores As Proveedor Public Property EnVigor As Boolean End Class <Table("Productos")> Public Class Producto Public Property Id As Integer Public Property BreveDescripcion As String Public Property EnVigor As Boolean Public Overridable Property ReglasEmpresas As ICollection(Of ReglasEmpresa) = New HashSet(Of ReglasEmpresa) End Class <Table("Proveedores")> Public Class Proveedor Public Property Id As Integer <Required()> Public Property Nombre As String Public Overridable Property ReglasEmpresas As ICollection(Of ReglasEmpresa) = New HashSet(Of ReglasEmpresa) End Class End Namespacey el siguiente contexto
Imports System.Data.Entity Imports System.Data.Entity.Infrastructure Imports System.Data.Entity.ModelConfiguration.Conventions Imports BE.Model.Entities Public Class PruebasMVCCtx Inherits DbContext Public Property Productos As DbSet(Of Producto) Public Property Empresas As DbSet(Of Empresa) Public Property Proveedores As DbSet(Of Proveedor) Public Property ReglasEmpresas As DbSet(Of ReglasEmpresa) Public Overridable Sub Commit() MyBase.SaveChanges() End Sub Protected Overrides Sub OnModelCreating(modelBuilder As System.Data.Entity.DbModelBuilder) modelBuilder.Conventions.Remove(Of PluralizingTableNameConvention)() MyBase.OnModelCreating(modelBuilder) End Sub End Class
Hasta aqui entiendo que todo normal.
Mi problema viene cuando intento añadir una RegladeEmpresa... me diapara el siguiente error
Varias instancias de IEntityChangeTracker no pueden hacer referencia a un objeto de entidad.
Mi codigo de ejemplo seria algo como
_reglasEmpresaRepositorio.Add(New ReglasEmpresa() With { .Empresa = _empresaRepositorio.GetById(1), .Producto = _productoRepositorio.GetById(1), .Proveedores = _proveedorRepositorio.GetById(1), .EnVigor = True})Espero que puedan echarme un cable..
Muchas gracias por la atención.

