Ich habe Tabellen: Hersteller und Marke und Basis abstrakte Klasse.Net EF Datensätze in der Datenbank (InvalidOperationException) nicht gespeichert
public abstract class Entity<TEntity, TKeyType> : IEntity<TEntity, TKeyType>
where TEntity : class
{
[Key]
public virtual TKeyType ID { get; set; }
public virtual bool IsDeleted { get; set; }
#region Equals
public virtual bool Equals(Entity<TEntity, TKeyType> other)
{
if (ReferenceEquals(this, other))
return true;
if (other == null || !(other is TEntity))
return false;
return ID.Equals(other.ID);
}
public override bool Equals(object obj)
{
var compareTo = obj as Entity<TEntity, TKeyType>;
return Equals(compareTo);
}
public override int GetHashCode()
{
return ID.GetHashCode();
}
#endregion
}
Entity:
public partial class Vendor : Entity<Vendor, long>
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Vendor()
{
Brand = new HashSet<Brand>();
}
[Required]
[StringLength(32)]
public string Name { get; set; }
public Guid Guid { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Brand> Brand { get; set; }
}
und
public partial class Brand : Entity<Brand, long>
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Brand()
{
SubBrand = new HashSet<SubBrand>();
}
[Required]
[StringLength(64)]
public string Name { get; set; }
public Guid Guid { get; set; }
public long VendorID { get; set; }
public virtual Vendor Vendor { get; set; }
}
ODMSDBContext:
in Service existieren Verfahren - Erstellen
public override void Create(IEnumerable<EntityModel> models)
{
var entities = new List<Brand>();
foreach (var model in models)
{
var entityModel = model as BrandModel;
var entity = new Brand
{
Guid = entityModel.Guid,
VendorID = entityModel.VendorID,
Name = entityModel.Name,
SortOrder = entityModel.SortOrder,
ExtraCode = entityModel.ExtraCode
};
entities.Add(entity);
}
_repository.Create(entities);
_repository.Save();
}
und Beispielcode aus Repository (_repository)
public class EntityRepository<TEntity, TKeyType> : IEntityRepository<ODMSDBContext, TEntity, TKeyType>
where TEntity : class, IEntity<TEntity, TKeyType>
{
private readonly ODMSDBContext _context;
private DbSet<TEntity> DbSet => _context.Set<TEntity>();
public EntityRepository(ODMSDBContext context)
{
_context = context;
}
...
public void Create(IEnumerable<TEntity> entities)
{
DbSet.AddRange(entities);
}
public void Save()
{
_context.SaveChanges();
transaction.Commit();
}
}
}
auf Speichern Ich erhalte eine Fehlermeldung:
Exception thrown: 'System.InvalidOperationException' in EntityFramework.dll
Additional information: The operation failed: The relationship could not be > changed because one or more of the foreign-key properties is non-nullable. When a change is made to a relationship, the related foreign-key property is set to a null value. If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted.
vor dem Speichern habe ich Marke überprüft. Anbieter ist in den meisten Fällen Null, aber VendorID ist gefüllt. Ich habe versucht, dies in Verfahren
Vendor = _vendorRepository.GetById(entityModel.VendorID)
erstellen in Service hinzuzufügen, aber nach AddRange Daten aus Lieferantenfeld sind verschwunden%)
upd. Ich habe ein kleines Video erstellt, das seltsames Verhalten beschreibt - http://screencast.com/t/RI32v4gu
Wo ist 'SubBrand' Code? – grek40
@ grek40 Ich denke es ist egal. Code ist ähnlich wie in Brand, nichts Neues. denkst du ist es wichtig? –