0

我试图在我的域对象中使用身份类,但是当我想为创建数据库创建迁移时,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 core“不要尝试在此处使用导航属性对流”

4

1 回答 1

0

如下更改您的存在类(您可以相应地添加您的方法)

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);

您可以参考下面的 SO 问题来纠正您的身份生成。

Entity Framework 如何为主键值生成 GUID?

于 2018-12-07T04:36:21.777 回答