我有表:供应商和品牌和基地抽象类净EF记录没有保存在数据库中(InvalidOperationException异常)
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
}
实体:
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; }
}
和
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:
public partial class ODMSDBContext : DbContext
{
public ODMSDBContext() : base("name=ODMSConnection") { }
public virtual DbSet<Vendor> Vendor { get; set; }
public virtual DbSet<Brand> Brand { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Brand>()
.Property(e => e.ExtraCode)
.IsUnicode(false);
modelBuilder.Entity<Brand>()
.Property(e => e.Name)
.IsUnicode(false);
modelBuilder.Entity<Brand>()
.HasMany(e => e.SubBrand)
.WithRequired(e => e.Brand)
.HasForeignKey(e => e.BrandID)
.WillCascadeOnDelete(false);
modelBuilder.Entity<Vendor>()
.Property(e => e.Name)
.IsUnicode(false);
modelBuilder.Entity<Vendor>()
.HasMany(e => e.Brand)
.WithRequired(e => e.Vendor)
.HasForeignKey(e => e.VendorID)
.WillCascadeOnDelete(false);
}
}
在服务
存在方法 - 创建
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();
}
,并从信息库(_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();
}
}
}
上保存我得到一个错误:
保存前我保留3210Exception 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.
我选中了品牌。大部分供应商都是空的,但VendorID已填满。 我试图在方法添加此在服务创建
Vendor = _vendorRepository.GetById(entityModel.VendorID)
但之后,从供应商字段的AddRange数据消失%)
UPD。我创建了描述奇怪行为的小视频 - http://screencast.com/t/RI32v4gu
'SubBrand'代码在哪里? – grek40
@ grek40我觉得没关系。代码与品牌类似,没有什么新意。你认为这很重要吗? –