【问题标题】:Disable name convention for navigation property ef-core2.2禁用导航属性 ef-core2.2 的命名约定
【发布时间】:2018-12-07 03:50:48
【问题描述】:

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

【问题讨论】:

    标签: c# entity-framework ef-core-2.2


    【解决方案1】:

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

    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 问题来更正您的身份生成。

    How does Entity Framework generate a GUID for a primary key value?

    【讨论】:

    • 感谢您的回答,但我不想获得外键,我很感兴趣将 WarehouseId 和 ProductId 保存为自己的类型,因为在某些情况下,我将有一个单独的上下文,仅共享id 类,所以 ef 将等待找到关系。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-03
    • 1970-01-01
    • 2015-11-21
    • 1970-01-01
    • 2017-12-22
    • 1970-01-01
    相关资源
    最近更新 更多