我试图在我的域对象中使用一个身份类,但是当我想为创建数据库创建迁移时,ef core 2.2对我说:
System.Reflection.TargetInvocationException:调用的目标引发了异常。 ---> System.InvalidOperationException:“仓库”不能用作实体类型“存在”的属性,因为它已配置为导航。
我的dbcontext是
public class WarehousesContext : BaseContext<WarehousesContext>
{
public WarehousesContext(DbContextOptions<WarehousesContext> options) : base(options)
{
}
public WarehousesContext() : base() { }
public DbSet<Warehouse> Warehouses { get; set; }
public DbSet<Existence> Existences { get; set; }
public DbSet<Entry> Entries { get; set; }
public DbSet<Exit> Exits { get; set; }
public DbSet<Transfer> Transfers { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.HasDefaultSchema("Inventory");
modelBuilder.Entity<Warehouse>().ToTable("Warehouses");
modelBuilder.Entity<Warehouse>().HasKey(w => w.Id);
modelBuilder.Entity<Warehouse>().Property(w => w.Id).HasConversion(v => v.Id, v => new WarehouseId(v));
modelBuilder.Entity<Existence>().ToTable("Existences");
modelBuilder.Entity<Existence>().HasKey(e => e.Id);
modelBuilder.Entity<Existence>().Property(e => e.Id).HasConversion(v => v.Id, v => new ExistenceId(v));
modelBuilder.Entity<Existence>().OwnsOne(e => e.Warehouse);
modelBuilder.Entity<Existence>().OwnsOne(e => e.Product);
}
}
我的存在类是
public class Existence
{
public ExistenceId Id { get; private set; }
public WarehouseId Warehouse { get; private set; }
public ProductId Product { get; private set; }
public decimal Quantity { get; private set; }
public string Batch { get; private set; }
private Existence() { }
public Existence(WarehouseId warehouse, ProductId product, decimal quantity, string batch)
{
Warehouse = warehouse;
Product = product;
Quantity = quantity;
Batch = batch;
}
internal void Add(decimal quantity)
{
Quantity += quantity;
}
internal void Subtract(decimal quantity)
{
Quantity -= quantity;
if (Quantity < 0)
throw new Exception();
}
和我的WarehouseId类
public class WarehouseId
{
public string Id { get; private set; }
public WarehouseId()
{
this.Id = Guid.NewGuid().ToString();
}
public WarehouseId(string id)
{
Id = id;
}
}
我认为问题是我使用“ entityId”模式来命名我的身份类,因此我想知道是否存在某种方式来告诉ef核心“不要在此处尝试使用导航属性对流”
答案 0 :(得分:0)
如下更改您的Existence类(您可以相应地添加方法)
public class Existence
{
public string Id { get; private set; }
[ForeignKey("Warehouse")]
public string WarehouseId { get; private set; }
public ProductId Product { get; private set; }
public decimal Quantity { get; private set; }
public string Batch { get; private set; }
public virtual Warehouse Warehouse{get;set;)
}
public class Warehouse
{
//your other Warehouse properties
//add below line, if one to one relation
public virtual Existence Existence{get; set;}
//or, add below line, if one to many relation
//public virtual IList<Existence> Existence{get; set;}
}
从OnModelCreating方法中删除以下行,
modelBuilder.Entity<Existence>().OwnsOne(e => e.Warehouse);
modelBuilder.Entity<Existence>().OwnsOne(e => e.Product);
,您可以参考下面的问题以更正您的身份生成。
How does Entity Framework generate a GUID for a primary key value?